webpack.conf.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { modifyWebpackConfig, shallowMerge, defaultOptions } = require('@gera2ld/plaid');
  2. const { isProd } = require('@gera2ld/plaid/util');
  3. const webpack = require('webpack');
  4. const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
  5. const TerserPlugin = require('terser-webpack-plugin');
  6. const projectConfig = require('./plaid.conf');
  7. const mergedConfig = shallowMerge(defaultOptions, projectConfig);
  8. const INIT_FUNC_NAME = 'VMInitInjection';
  9. // Copied from gulpfile.js: strip alphabetic suffix
  10. const VM_VER = require('../package.json').version.replace(/-[^.]*/, '');
  11. const pickEnvs = (items) => {
  12. return Object.assign({}, ...items.map(x => ({
  13. [`process.env.${x.key}`]: JSON.stringify(
  14. 'val' in x ? x.val
  15. : process.env[x.key] ?? x.def
  16. ),
  17. })));
  18. };
  19. const definitions = new webpack.DefinePlugin({
  20. ...pickEnvs([
  21. { key: 'DEBUG', def: false },
  22. { key: 'VM_VER', val: VM_VER },
  23. { key: 'SYNC_GOOGLE_CLIENT_ID' },
  24. { key: 'SYNC_GOOGLE_CLIENT_SECRET' },
  25. { key: 'SYNC_ONEDRIVE_CLIENT_ID' },
  26. { key: 'SYNC_ONEDRIVE_CLIENT_SECRET' },
  27. ]),
  28. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  29. });
  30. const minimizerOptions = {
  31. cache: true,
  32. parallel: true,
  33. sourceMap: true,
  34. terserOptions: {
  35. output: {
  36. ascii_only: true,
  37. },
  38. },
  39. };
  40. const minimizer = isProd && [
  41. new TerserPlugin({
  42. chunkFilter: ({ name }) => !name.startsWith('public/'),
  43. ...minimizerOptions,
  44. terserOptions: {
  45. ...minimizerOptions.terserOptions,
  46. compress: {
  47. ecma: 6,
  48. // 'safe' since we don't rely on function prototypes
  49. unsafe_arrows: true,
  50. },
  51. },
  52. }),
  53. new TerserPlugin({
  54. chunkFilter: ({ name }) => name.startsWith('public/'),
  55. ...minimizerOptions,
  56. }),
  57. ];
  58. const modify = (page, entry, init) => modifyWebpackConfig(
  59. (config) => {
  60. config.plugins.push(definitions);
  61. if (!entry) init = page;
  62. if (init) init(config);
  63. return config;
  64. }, {
  65. projectConfig: {
  66. ...mergedConfig,
  67. ...entry && { pages: { [page]: { entry }} },
  68. optimization: {
  69. ...mergedConfig.optimization,
  70. minimizer,
  71. },
  72. },
  73. },
  74. );
  75. // avoid running webpack bootstrap in a potentially hacked environment
  76. // after documentElement was replaced which triggered reinjection of content scripts
  77. const skipReinjectionHeader = `if (window['${INIT_FUNC_NAME}'] !== 1)`;
  78. module.exports = Promise.all([
  79. modify((config) => {
  80. config.output.publicPath = '/';
  81. }),
  82. modify('injected', './src/injected', (config) => {
  83. config.plugins.push(
  84. new WrapperWebpackPlugin({
  85. header: skipReinjectionHeader,
  86. }));
  87. }),
  88. modify('injected-web', './src/injected/web', (config) => {
  89. config.output.libraryTarget = 'commonjs2';
  90. config.plugins.push(
  91. new WrapperWebpackPlugin({
  92. header: `${skipReinjectionHeader}
  93. window['${INIT_FUNC_NAME}'] = function () {
  94. var module = { exports: {} };
  95. `,
  96. footer: `
  97. var exports = module.exports;
  98. return exports.__esModule ? exports['default'] : exports;
  99. };0;`,
  100. }),
  101. );
  102. }),
  103. ]);