webpack.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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', 'devtools'],
  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. // look for .css or .scss files
  56. test: /\.css$/,
  57. // in the `src` directory
  58. use: [
  59. MiniCssExtractPlugin.loader,
  60. {
  61. loader: 'css-loader',
  62. },
  63. {
  64. loader: 'postcss-loader',
  65. },
  66. ],
  67. },
  68. {
  69. test: new RegExp(`.(${fileExtensions.join('|')})$`),
  70. loader: 'file-loader',
  71. options: {
  72. name: '[name].[ext]',
  73. },
  74. exclude: /node_modules/,
  75. },
  76. {
  77. test: /\.js$/,
  78. use: [
  79. {
  80. loader: 'source-map-loader',
  81. },
  82. {
  83. loader: 'babel-loader',
  84. },
  85. ],
  86. exclude: /node_modules/,
  87. },
  88. ],
  89. },
  90. resolve: {
  91. alias,
  92. extensions: fileExtensions
  93. .map((extension) => `.${extension}`)
  94. .concat(['.js', '.vue', '.css']),
  95. },
  96. plugins: [
  97. new MiniCssExtractPlugin(),
  98. new VueLoaderPlugin(),
  99. new webpack.ProgressPlugin(),
  100. // clean the build folder
  101. new CleanWebpackPlugin({
  102. verbose: true,
  103. cleanStaleWebpackAssets: true,
  104. }),
  105. // expose and write the allowed env vars on the compiled bundle
  106. new webpack.EnvironmentPlugin(['NODE_ENV']),
  107. new CopyWebpackPlugin({
  108. patterns: [
  109. {
  110. from: 'src/manifest.json',
  111. to: path.join(__dirname, 'build'),
  112. force: true,
  113. transform(content) {
  114. // generates the manifest file using the package.json informations
  115. return Buffer.from(
  116. JSON.stringify({
  117. description: process.env.npm_package_description,
  118. version: process.env.npm_package_version,
  119. ...JSON.parse(content.toString()),
  120. })
  121. );
  122. },
  123. },
  124. {
  125. from: 'src/assets/img/icon-128.png',
  126. to: path.join(__dirname, 'build'),
  127. force: true,
  128. },
  129. {
  130. from: 'src/assets/img/icon-34.png',
  131. to: path.join(__dirname, 'build'),
  132. force: true,
  133. },
  134. ],
  135. }),
  136. new HtmlWebpackPlugin({
  137. template: path.join(__dirname, 'src', 'newtab', 'index.html'),
  138. filename: 'newtab.html',
  139. chunks: ['newtab'],
  140. cache: false,
  141. }),
  142. new HtmlWebpackPlugin({
  143. template: path.join(__dirname, 'src', 'popup', 'index.html'),
  144. filename: 'popup.html',
  145. chunks: ['popup'],
  146. cache: false,
  147. }),
  148. new webpack.DefinePlugin({
  149. __VUE_OPTIONS_API__: true,
  150. __VUE_PROD_DEVTOOLS__: false,
  151. }),
  152. ],
  153. infrastructureLogging: {
  154. level: 'info',
  155. },
  156. };
  157. if (env.NODE_ENV === 'development') {
  158. options.devtool = 'cheap-module-source-map';
  159. } else {
  160. options.optimization = {
  161. minimize: true,
  162. minimizer: [
  163. new TerserPlugin({
  164. extractComments: false,
  165. }),
  166. ],
  167. runtimeChunk: 'single',
  168. splitChunks: {
  169. chunks: 'all',
  170. maxInitialRequests: Infinity,
  171. minSize: 0,
  172. cacheGroups: {
  173. vendor: {
  174. test: /[\\/]node_modules[\\/]/,
  175. name: 'vendor',
  176. },
  177. },
  178. },
  179. };
  180. }
  181. module.exports = options;