webpack.config.js 5.0 KB

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