webpack.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 MiniCssExtractPlugin = require('mini-css-extract-plugin');
  9. const TerserPlugin = require('terser-webpack-plugin');
  10. const env = require('./utils/env');
  11. const ASSET_PATH = process.env.ASSET_PATH || '/';
  12. const alias = {
  13. '@': path.resolve(__dirname, 'src/'),
  14. secrets: path.join(__dirname, 'secrets.blank.js'),
  15. };
  16. // load the secrets
  17. const secretsPath = path.join(__dirname, `secrets.${env.NODE_ENV}.js`);
  18. const fileExtensions = [
  19. 'jpg',
  20. 'jpeg',
  21. 'png',
  22. 'gif',
  23. 'eot',
  24. 'otf',
  25. 'svg',
  26. 'ttf',
  27. 'woff',
  28. 'woff2',
  29. ];
  30. if (fileSystem.existsSync(secretsPath)) {
  31. alias.secrets = secretsPath;
  32. }
  33. const options = {
  34. mode: process.env.NODE_ENV || 'development',
  35. entry: {
  36. newtab: path.join(__dirname, 'src', 'newtab', 'index.js'),
  37. popup: path.join(__dirname, 'src', 'popup', 'index.js'),
  38. background: path.join(__dirname, 'src', 'background', 'index.js'),
  39. contentScript: path.join(__dirname, 'src', 'content', 'index.js'),
  40. shortcut: path.join(__dirname, 'src', 'content', 'shortcut.js'),
  41. elementSelector: path.join(
  42. __dirname,
  43. 'src',
  44. 'content',
  45. 'element-selector',
  46. 'main.js'
  47. ),
  48. },
  49. chromeExtensionBoilerplate: {
  50. notHotReload: ['contentScript', 'shortcut', 'elementSelector'],
  51. },
  52. output: {
  53. path: path.resolve(__dirname, 'build'),
  54. filename: '[name].bundle.js',
  55. publicPath: ASSET_PATH,
  56. },
  57. module: {
  58. rules: [
  59. {
  60. test: /\.vue$/,
  61. loader: 'vue-loader',
  62. },
  63. {
  64. test: /\.css$/,
  65. use: [
  66. MiniCssExtractPlugin.loader,
  67. {
  68. loader: 'css-loader',
  69. },
  70. {
  71. loader: 'postcss-loader',
  72. },
  73. ],
  74. },
  75. {
  76. test: /\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
  77. type: 'javascript/auto',
  78. // Use `Rule.include` to specify the files of locale messages to be pre-compiled
  79. include: [path.resolve(__dirname, './src/locales')],
  80. loader: '@intlify/vue-i18n-loader',
  81. },
  82. {
  83. test: new RegExp(`.(${fileExtensions.join('|')})$`),
  84. loader: 'file-loader',
  85. type: 'javascript/auto',
  86. options: {
  87. name: '[name].[ext]',
  88. },
  89. exclude: /node_modules/,
  90. },
  91. {
  92. test: /\.js$/,
  93. use: [
  94. {
  95. loader: 'source-map-loader',
  96. },
  97. {
  98. loader: 'babel-loader',
  99. },
  100. ],
  101. exclude: /node_modules/,
  102. },
  103. ],
  104. },
  105. resolve: {
  106. alias,
  107. extensions: fileExtensions
  108. .map((extension) => `.${extension}`)
  109. .concat(['.js', '.vue', '.css']),
  110. },
  111. plugins: [
  112. new MiniCssExtractPlugin(),
  113. new VueLoaderPlugin(),
  114. new webpack.ProgressPlugin(),
  115. // clean the build folder
  116. new CleanWebpackPlugin({
  117. verbose: true,
  118. cleanStaleWebpackAssets: true,
  119. }),
  120. // expose and write the allowed env vars on the compiled bundle
  121. new webpack.EnvironmentPlugin(['NODE_ENV']),
  122. new CopyWebpackPlugin({
  123. patterns: [
  124. {
  125. from: 'src/manifest.json',
  126. to: path.join(__dirname, 'build'),
  127. force: true,
  128. transform(content) {
  129. // generates the manifest file using the package.json informations
  130. return Buffer.from(
  131. JSON.stringify({
  132. description: process.env.npm_package_description,
  133. version: process.env.npm_package_version,
  134. ...JSON.parse(content.toString()),
  135. })
  136. );
  137. },
  138. },
  139. {
  140. from: 'src/assets/images/icon-128.png',
  141. to: path.join(__dirname, 'build'),
  142. force: true,
  143. },
  144. ],
  145. }),
  146. new HtmlWebpackPlugin({
  147. template: path.join(__dirname, 'src', 'newtab', 'index.html'),
  148. filename: 'newtab.html',
  149. chunks: ['newtab'],
  150. cache: false,
  151. }),
  152. new HtmlWebpackPlugin({
  153. template: path.join(__dirname, 'src', 'popup', 'index.html'),
  154. filename: 'popup.html',
  155. chunks: ['popup'],
  156. cache: false,
  157. }),
  158. new webpack.DefinePlugin({
  159. __VUE_OPTIONS_API__: true,
  160. __VUE_PROD_DEVTOOLS__: false,
  161. }),
  162. ],
  163. infrastructureLogging: {
  164. level: 'info',
  165. },
  166. };
  167. if (env.NODE_ENV === 'development') {
  168. options.devtool = 'cheap-module-source-map';
  169. } else {
  170. options.optimization = {
  171. minimize: true,
  172. minimizer: [
  173. new TerserPlugin({
  174. extractComments: false,
  175. }),
  176. ],
  177. // runtimeChunk: 'single',
  178. // splitChunks: {
  179. // chunks: 'all',
  180. // maxInitialRequests: Infinity,
  181. // minSize: 0,
  182. // cacheGroups: {
  183. // vendor: {
  184. // test: /[\\/]node_modules[\\/]/,
  185. // name: 'vendor',
  186. // },
  187. // },
  188. // },
  189. };
  190. }
  191. module.exports = options;