webpack.config.js 5.5 KB

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