webpack.config.js 5.8 KB

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