webpack.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const fileSystem = require('fs-extra');
  4. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  5. const { VueLoaderPlugin } = require('vue-loader');
  6. const CopyWebpackPlugin = require('copy-webpack-plugin');
  7. const HtmlWebpackPlugin = require('html-webpack-plugin');
  8. const TerserPlugin = require('terser-webpack-plugin');
  9. const env = require('./utils/env');
  10. const ASSET_PATH = process.env.ASSET_PATH || '/';
  11. const alias = {};
  12. // load the secrets
  13. const secretsPath = path.join(__dirname, 'secrets.' + env.NODE_ENV + '.js');
  14. const fileExtensions = [
  15. 'jpg',
  16. 'jpeg',
  17. 'png',
  18. 'gif',
  19. 'eot',
  20. 'otf',
  21. 'svg',
  22. 'ttf',
  23. 'woff',
  24. 'woff2',
  25. ];
  26. if (fileSystem.existsSync(secretsPath)) {
  27. alias['secrets'] = secretsPath;
  28. }
  29. const options = {
  30. mode: process.env.NODE_ENV || 'development',
  31. entry: {
  32. newtab: path.join(__dirname, 'src', 'newtab', 'index.js'),
  33. popup: path.join(__dirname, 'src', 'popup', 'index.js'),
  34. background: path.join(__dirname, 'src', 'background', 'index.js'),
  35. contentScript: path.join(__dirname, 'src', 'content', 'index.js'),
  36. },
  37. chromeExtensionBoilerplate: {
  38. notHotReload: ['contentScript', 'devtools'],
  39. },
  40. output: {
  41. path: path.resolve(__dirname, 'build'),
  42. filename: '[name].bundle.js',
  43. publicPath: ASSET_PATH,
  44. },
  45. module: {
  46. rules: [
  47. {
  48. test: /\.vue$/,
  49. loader: 'vue-loader'
  50. },
  51. {
  52. // look for .css or .scss files
  53. test: /\.css$/,
  54. // in the `src` directory
  55. use: [
  56. {
  57. loader: 'style-loader',
  58. },
  59. {
  60. loader: 'css-loader',
  61. },
  62. {
  63. loader: 'postcss-loader',
  64. },
  65. ],
  66. },
  67. {
  68. test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
  69. loader: 'file-loader',
  70. options: {
  71. name: '[name].[ext]',
  72. },
  73. exclude: /node_modules/,
  74. },
  75. {
  76. test: /\.js$/,
  77. use: [
  78. {
  79. loader: 'source-map-loader',
  80. },
  81. {
  82. loader: 'babel-loader',
  83. },
  84. ],
  85. exclude: /node_modules/,
  86. },
  87. ],
  88. },
  89. resolve: {
  90. alias: alias,
  91. extensions: fileExtensions
  92. .map((extension) => '.' + extension)
  93. .concat(['.js', '.vue', '.css']),
  94. },
  95. plugins: [
  96. new VueLoaderPlugin(),
  97. new webpack.ProgressPlugin(),
  98. // clean the build folder
  99. new CleanWebpackPlugin({
  100. verbose: true,
  101. cleanStaleWebpackAssets: true,
  102. }),
  103. // expose and write the allowed env vars on the compiled bundle
  104. new webpack.EnvironmentPlugin(['NODE_ENV']),
  105. new CopyWebpackPlugin({
  106. patterns: [
  107. {
  108. from: 'src/manifest.json',
  109. to: path.join(__dirname, 'build'),
  110. force: true,
  111. transform: function (content, path) {
  112. // generates the manifest file using the package.json informations
  113. return Buffer.from(
  114. JSON.stringify({
  115. description: process.env.npm_package_description,
  116. version: process.env.npm_package_version,
  117. ...JSON.parse(content.toString()),
  118. })
  119. );
  120. },
  121. },
  122. {
  123. from: 'src/assets/img/icon-128.png',
  124. to: path.join(__dirname, 'build'),
  125. force: true,
  126. },
  127. {
  128. from: 'src/assets/img/icon-34.png',
  129. to: path.join(__dirname, 'build'),
  130. force: true,
  131. },
  132. ],
  133. }),
  134. new HtmlWebpackPlugin({
  135. template: path.join(__dirname, 'src', 'Newtab', 'index.html'),
  136. filename: 'newtab.html',
  137. chunks: ['newtab'],
  138. cache: false,
  139. }),
  140. new HtmlWebpackPlugin({
  141. template: path.join(__dirname, 'src', 'Popup', 'index.html'),
  142. filename: 'popup.html',
  143. chunks: ['popup'],
  144. cache: false,
  145. }),
  146. ],
  147. infrastructureLogging: {
  148. level: 'info',
  149. },
  150. };
  151. if (env.NODE_ENV === 'development') {
  152. options.devtool = 'cheap-module-source-map';
  153. } else {
  154. options.optimization = {
  155. minimize: true,
  156. minimizer: [
  157. new TerserPlugin({
  158. extractComments: false,
  159. }),
  160. ],
  161. };
  162. }
  163. module.exports = options;