webpack.conf.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. const VM_VER = getVersion();
  17. const WEBPACK_OPTS = {
  18. node: {
  19. global: false,
  20. },
  21. performance: {
  22. maxEntrypointSize: 1e6,
  23. maxAssetSize: 0.5e6,
  24. },
  25. };
  26. const MIN_OPTS = {
  27. extractComments: false,
  28. parallel: true,
  29. terserOptions: {
  30. compress: {
  31. // `terser` often inlines big one-time functions inside a small "hot" function
  32. reduce_funcs: false,
  33. reduce_vars: false,
  34. },
  35. output: {
  36. ascii_only: true,
  37. comments: false,
  38. wrap_func_args: false, // disabling a premature optimization designed for old browsers
  39. },
  40. },
  41. };
  42. const MIN_OPTS_PUBLIC = isProd && {
  43. include: 'public/',
  44. ...MIN_OPTS,
  45. };
  46. const MIN_OPTS_MAIN = isProd && deepmerge.all([{}, MIN_OPTS, {
  47. exclude: 'public/',
  48. terserOptions: {
  49. compress: {
  50. ecma: 8, // ES2017 Object.entries and so on
  51. passes: 2, // necessary now since we removed plaid's minimizer
  52. unsafe_arrows: true, // it's 'safe' since we don't rely on function prototypes
  53. },
  54. },
  55. }]);
  56. const pickEnvs = (items) => {
  57. return Object.assign({}, ...items.map(x => ({
  58. [`process.env.${x.key}`]: JSON.stringify(
  59. 'val' in x ? x.val
  60. : process.env[x.key] ?? x.def,
  61. ),
  62. })));
  63. };
  64. const defsObj = {
  65. ...pickEnvs([
  66. { key: 'DEBUG', def: false },
  67. { key: 'VM_VER', val: VM_VER },
  68. { key: 'SYNC_GOOGLE_CLIENT_ID' },
  69. { key: 'SYNC_GOOGLE_CLIENT_SECRET' },
  70. { key: 'SYNC_GOOGLE_DESKTOP_ID' },
  71. { key: 'SYNC_GOOGLE_DESKTOP_SECRET' },
  72. { key: 'SYNC_ONEDRIVE_CLIENT_ID' },
  73. { key: 'SYNC_ONEDRIVE_CLIENT_SECRET' },
  74. { key: 'SYNC_DROPBOX_CLIENT_ID' },
  75. ]),
  76. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  77. 'process.env.VAULT_ID': VAULT_ID,
  78. 'process.env.HANDSHAKE_ID': HANDSHAKE_ID,
  79. 'process.env.HANDSHAKE_ACK': '1',
  80. 'process.env.CODEMIRROR_THEMES': JSON.stringify(getCodeMirrorThemes()),
  81. 'process.env.DEV': JSON.stringify(!isProd),
  82. };
  83. // avoid running webpack bootstrap in a potentially hacked environment
  84. // after documentElement was replaced which triggered reinjection of content scripts
  85. const skipReinjectionHeader = `{
  86. const INIT_FUNC_NAME = '${INIT_FUNC_NAME}';
  87. if (window[INIT_FUNC_NAME] !== 1)`;
  88. const modify = (page, entry, init) => modifyWebpackConfig(
  89. (config) => {
  90. Object.assign(config, WEBPACK_OPTS);
  91. config.output.publicPath = '/';
  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. }),
  125. modify('injected', './src/injected', (config) => {
  126. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  127. addWrapperWithGlobals('injected/content', config, defsObj, getGlobals => ({
  128. header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
  129. footer: '}}',
  130. }));
  131. }),
  132. modify('injected-web', './src/injected/web', (config) => {
  133. config.output.libraryTarget = 'commonjs2';
  134. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  135. addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({
  136. header: () => `${skipReinjectionHeader}
  137. window[INIT_FUNC_NAME] = function (IS_FIREFOX,${HANDSHAKE_ID},${VAULT_ID}) {
  138. const module = { __proto__: null };
  139. ${getGlobals()}`,
  140. footer: `
  141. const { exports } = module;
  142. return exports.__esModule ? exports.default : exports;
  143. }};0;`,
  144. }));
  145. }),
  146. ]);