webpack.config.js 4.6 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. },
  40. chromeExtensionBoilerplate: {
  41. notHotReload: ['contentScript'],
  42. },
  43. output: {
  44. path: path.resolve(__dirname, 'build'),
  45. filename: '[name].bundle.js',
  46. publicPath: ASSET_PATH,
  47. },
  48. module: {
  49. rules: [
  50. {
  51. test: /\.vue$/,
  52. loader: 'vue-loader',
  53. },
  54. {
  55. test: /\.css$/,
  56. use: [
  57. MiniCssExtractPlugin.loader,
  58. // {
  59. // loader: 'style-loader',
  60. // },
  61. {
  62. loader: 'css-loader',
  63. },
  64. {
  65. loader: 'postcss-loader',
  66. },
  67. ],
  68. },
  69. {
  70. test: new RegExp(`.(${fileExtensions.join('|')})$`),
  71. loader: 'file-loader',
  72. type: 'javascript/auto',
  73. options: {
  74. name: '[name].[ext]',
  75. },
  76. exclude: /node_modules/,
  77. },
  78. {
  79. test: /\.js$/,
  80. use: [
  81. {
  82. loader: 'source-map-loader',
  83. },
  84. {
  85. loader: 'babel-loader',
  86. },
  87. ],
  88. exclude: /node_modules/,
  89. },
  90. ],
  91. },
  92. resolve: {
  93. alias,
  94. extensions: fileExtensions
  95. .map((extension) => `.${extension}`)
  96. .concat(['.js', '.vue', '.css']),
  97. },
  98. plugins: [
  99. new MiniCssExtractPlugin(),
  100. new VueLoaderPlugin(),
  101. new webpack.ProgressPlugin(),
  102. // clean the build folder
  103. new CleanWebpackPlugin({
  104. verbose: true,
  105. cleanStaleWebpackAssets: true,
  106. }),
  107. // expose and write the allowed env vars on the compiled bundle
  108. new webpack.EnvironmentPlugin(['NODE_ENV']),
  109. new CopyWebpackPlugin({
  110. patterns: [
  111. {
  112. from: 'src/manifest.json',
  113. to: path.join(__dirname, 'build'),
  114. force: true,
  115. transform(content) {
  116. // generates the manifest file using the package.json informations
  117. return Buffer.from(
  118. JSON.stringify({
  119. description: process.env.npm_package_description,
  120. version: process.env.npm_package_version,
  121. ...JSON.parse(content.toString()),
  122. })
  123. );
  124. },
  125. },
  126. {
  127. from: 'src/assets/img/icon-128.png',
  128. to: path.join(__dirname, 'build'),
  129. force: true,
  130. },
  131. {
  132. from: 'src/assets/img/icon-34.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;