1
0

webpack.config.js 6.5 KB

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