webpack.config.js 5.1 KB

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