webpack.config.js 5.1 KB

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