webpack.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. loader: 'file-loader',
  107. type: 'javascript/auto',
  108. options: {
  109. name: '[name].[ext]',
  110. },
  111. exclude: /node_modules/,
  112. },
  113. {
  114. test: /\.js$/,
  115. use: [
  116. {
  117. loader: 'source-map-loader',
  118. },
  119. {
  120. loader: 'babel-loader',
  121. },
  122. ],
  123. exclude: /node_modules/,
  124. },
  125. ],
  126. },
  127. resolve: {
  128. alias,
  129. extensions: fileExtensions
  130. .map((extension) => `.${extension}`)
  131. .concat(['.js', '.vue', '.css']),
  132. },
  133. plugins: [
  134. new MiniCssExtractPlugin(),
  135. new VueLoaderPlugin(),
  136. new webpack.DefinePlugin({
  137. BROWSER_TYPE: JSON.stringify(env.BROWSER),
  138. }),
  139. new webpack.ProgressPlugin(),
  140. // clean the build folder
  141. new CleanWebpackPlugin({
  142. verbose: true,
  143. cleanStaleWebpackAssets: true,
  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 (env.NODE_ENV === 'development' && !isChrome) {
  162. manifestObj.content_security_policy =
  163. "script-src 'self' 'unsafe-eval'; object-src 'self'";
  164. }
  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', 'popup', 'index.html'),
  192. filename: 'popup.html',
  193. chunks: ['popup'],
  194. cache: false,
  195. }),
  196. new webpack.DefinePlugin({
  197. __VUE_OPTIONS_API__: true,
  198. __VUE_PROD_DEVTOOLS__: false,
  199. }),
  200. ],
  201. infrastructureLogging: {
  202. level: 'info',
  203. },
  204. };
  205. if (env.NODE_ENV === 'development') {
  206. options.devtool = 'cheap-module-source-map';
  207. } else {
  208. options.optimization = {
  209. minimize: true,
  210. minimizer: [
  211. new TerserPlugin({
  212. extractComments: false,
  213. }),
  214. ],
  215. };
  216. }
  217. module.exports = options;