webpack.config.js 5.8 KB

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