webpack.config.js 4.8 KB

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