webpack.conf.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const { modifyWebpackConfig, shallowMerge, defaultOptions } = require('@gera2ld/plaid');
  2. const { isProd } = require('@gera2ld/plaid/util');
  3. const webpack = require('webpack');
  4. const TerserPlugin = isProd && require('terser-webpack-plugin');
  5. const deepmerge = isProd && require('deepmerge');
  6. const { ListBackgroundScriptsPlugin } = require('./manifest-helper');
  7. const { addWrapperWithGlobals, getCodeMirrorThemes, getUniqIdB64 } = require('./webpack-util');
  8. const ProtectWebpackBootstrapPlugin = require('./webpack-protect-bootstrap-plugin');
  9. const projectConfig = require('./plaid.conf');
  10. const { getVersion } = require('./version-helper');
  11. const mergedConfig = shallowMerge(defaultOptions, projectConfig);
  12. // Avoiding collisions with globals of a content-mode userscript
  13. const INIT_FUNC_NAME = `Violentmonkey:${getUniqIdB64()}`;
  14. const VAULT_ID = '__VAULT_ID__';
  15. const HANDSHAKE_ID = '__HANDSHAKE_ID__';
  16. // eslint-disable-next-line import/no-dynamic-require
  17. const VM_VER = getVersion();
  18. const WEBPACK_OPTS = {
  19. node: {
  20. global: false,
  21. process: false,
  22. setImmediate: false,
  23. },
  24. performance: {
  25. maxEntrypointSize: 1e6,
  26. maxAssetSize: 0.5e6,
  27. },
  28. };
  29. const MIN_OPTS = {
  30. cache: true,
  31. extractComments: false,
  32. parallel: true,
  33. sourceMap: true,
  34. terserOptions: {
  35. compress: {
  36. // `terser` often inlines big one-time functions inside a small "hot" function
  37. reduce_funcs: false,
  38. reduce_vars: false,
  39. },
  40. output: {
  41. ascii_only: true,
  42. comments: false,
  43. wrap_func_args: false, // disabling a premature optimization designed for old browsers
  44. },
  45. },
  46. };
  47. const MIN_OPTS_PUBLIC = isProd && {
  48. include: 'public/',
  49. ...MIN_OPTS,
  50. };
  51. const MIN_OPTS_MAIN = isProd && deepmerge.all([{}, MIN_OPTS, {
  52. exclude: 'public/',
  53. terserOptions: {
  54. compress: {
  55. ecma: 8, // ES2017 Object.entries and so on
  56. passes: 2, // necessary now since we removed plaid's minimizer
  57. unsafe_arrows: true, // it's 'safe' since we don't rely on function prototypes
  58. },
  59. },
  60. }]);
  61. const pickEnvs = (items) => {
  62. return Object.assign({}, ...items.map(x => ({
  63. [`process.env.${x.key}`]: JSON.stringify(
  64. 'val' in x ? x.val
  65. : process.env[x.key] ?? x.def,
  66. ),
  67. })));
  68. };
  69. const defsObj = {
  70. ...pickEnvs([
  71. { key: 'DEBUG', def: false },
  72. { key: 'VM_VER', val: VM_VER },
  73. { key: 'SYNC_GOOGLE_CLIENT_ID' },
  74. { key: 'SYNC_GOOGLE_CLIENT_SECRET' },
  75. { key: 'SYNC_ONEDRIVE_CLIENT_ID' },
  76. { key: 'SYNC_ONEDRIVE_CLIENT_SECRET' },
  77. ]),
  78. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  79. 'process.env.VAULT_ID': VAULT_ID,
  80. 'process.env.HANDSHAKE_ID': HANDSHAKE_ID,
  81. 'process.env.HANDSHAKE_ACK': '1',
  82. 'process.env.CODEMIRROR_THEMES': JSON.stringify(getCodeMirrorThemes()),
  83. };
  84. // avoid running webpack bootstrap in a potentially hacked environment
  85. // after documentElement was replaced which triggered reinjection of content scripts
  86. const skipReinjectionHeader = `{
  87. const INIT_FUNC_NAME = '${INIT_FUNC_NAME}';
  88. if (window[INIT_FUNC_NAME] !== 1)`;
  89. const modify = (page, entry, init) => modifyWebpackConfig(
  90. (config) => {
  91. Object.assign(config, WEBPACK_OPTS);
  92. config.plugins.push(new webpack.DefinePlugin({
  93. ...defsObj,
  94. // Conditional compilation to remove unsafe and unused stuff from `injected`
  95. 'process.env.IS_INJECTED': JSON.stringify(/injected/.test(page) && page),
  96. }));
  97. config.optimization.minimizer.find((m, i, arr) => (
  98. m.constructor.name === 'TerserPlugin' && arr.splice(i, 1)
  99. ));
  100. config.optimization.minimizer.push(...!isProd ? [] : [
  101. new TerserPlugin(MIN_OPTS_PUBLIC),
  102. new TerserPlugin(MIN_OPTS_MAIN),
  103. ]);
  104. if (!entry) init = page;
  105. if (init) init(config);
  106. return config;
  107. }, {
  108. projectConfig: {
  109. ...mergedConfig,
  110. ...entry && { pages: { [page]: { entry } } },
  111. },
  112. },
  113. );
  114. module.exports = Promise.all([
  115. modify((config) => {
  116. addWrapperWithGlobals('common', config, defsObj, getGlobals => ({
  117. header: () => `{ ${getGlobals()}`,
  118. footer: '}',
  119. test: /^(?!injected|public).*\.js$/,
  120. }));
  121. config.plugins.push(new ListBackgroundScriptsPlugin({
  122. minify: false, // keeping readable
  123. }));
  124. config.module.rules.find(r => r.loader === 'vue-loader')
  125. .options.compiler = require('vue-template-babel-compiler');
  126. }),
  127. modify('injected', './src/injected', (config) => {
  128. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  129. addWrapperWithGlobals('injected/content', config, defsObj, getGlobals => ({
  130. header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
  131. footer: '}}',
  132. }));
  133. }),
  134. modify('injected-web', './src/injected/web', (config) => {
  135. // TODO: replace WebPack's Object.*, .call(), .apply() with safe calls
  136. config.output.libraryTarget = 'commonjs2';
  137. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  138. addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({
  139. header: () => `${skipReinjectionHeader}
  140. window[INIT_FUNC_NAME] = function (IS_FIREFOX,${HANDSHAKE_ID},${VAULT_ID}) {
  141. const module = { __proto__: null };
  142. ${getGlobals()}`,
  143. footer: `
  144. const { exports } = module;
  145. return exports.__esModule ? exports.default : exports;
  146. }};0;`,
  147. }));
  148. }),
  149. ]);