webpack.config.js 5.8 KB

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