1
0

webpack.config.js 6.3 KB

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