webpack.config.js 4.6 KB

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