webpack.config.js 6.8 KB

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