webpack.conf.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const { modifyWebpackConfig, shallowMerge, defaultOptions } = require('@gera2ld/plaid');
  2. const { isProd } = require('@gera2ld/plaid/util');
  3. const webpack = require('webpack');
  4. const HTMLInlineCSSWebpackPlugin = isProd && require('html-inline-css-webpack-plugin').default;
  5. const TerserPlugin = isProd && require('terser-webpack-plugin');
  6. const deepmerge = isProd && require('deepmerge');
  7. const { ListBackgroundScriptsPlugin } = require('./manifest-helper');
  8. const { addWrapperWithGlobals, getUniqIdB64 } = require('./webpack-util');
  9. const ProtectWebpackBootstrapPlugin = require('./webpack-protect-bootstrap-plugin');
  10. const projectConfig = require('./plaid.conf');
  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 = require(`${defaultOptions.distDir}/manifest.json`).version;
  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. parallel: true,
  32. sourceMap: true,
  33. terserOptions: {
  34. compress: {
  35. // `terser` often inlines big one-time functions inside a small "hot" function
  36. reduce_funcs: false,
  37. reduce_vars: false,
  38. },
  39. output: {
  40. ascii_only: true,
  41. },
  42. },
  43. };
  44. const MIN_OPTS_PUBLIC = isProd && {
  45. chunkFilter: ({ name }) => name.startsWith('public/'),
  46. ...MIN_OPTS,
  47. };
  48. const MIN_OPTS_MAIN = isProd && deepmerge.all([{}, MIN_OPTS, {
  49. chunkFilter: ({ name }) => !name.startsWith('public/'),
  50. terserOptions: {
  51. compress: {
  52. ecma: 8, // ES2017 Object.entries and so on
  53. passes: 2, // necessary now since we removed plaid's minimizer
  54. unsafe_arrows: true, // it's 'safe' since we don't rely on function prototypes
  55. },
  56. },
  57. }]);
  58. const pickEnvs = (items) => {
  59. return Object.assign({}, ...items.map(x => ({
  60. [`process.env.${x.key}`]: JSON.stringify(
  61. 'val' in x ? x.val
  62. : process.env[x.key] ?? x.def,
  63. ),
  64. })));
  65. };
  66. const defsObj = {
  67. ...pickEnvs([
  68. { key: 'DEBUG', def: false },
  69. { key: 'VM_VER', val: VM_VER },
  70. { key: 'SYNC_GOOGLE_CLIENT_ID' },
  71. { key: 'SYNC_GOOGLE_CLIENT_SECRET' },
  72. { key: 'SYNC_ONEDRIVE_CLIENT_ID' },
  73. { key: 'SYNC_ONEDRIVE_CLIENT_SECRET' },
  74. ]),
  75. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  76. 'process.env.VAULT_ID_NAME': JSON.stringify(VAULT_ID),
  77. 'process.env.VAULT_ID': VAULT_ID,
  78. 'process.env.HANDSHAKE_ID': HANDSHAKE_ID,
  79. 'process.env.HANDSHAKE_ACK': '1',
  80. };
  81. // avoid running webpack bootstrap in a potentially hacked environment
  82. // after documentElement was replaced which triggered reinjection of content scripts
  83. const skipReinjectionHeader = `if (window['${INIT_FUNC_NAME}'] !== 1)`;
  84. const modify = (page, entry, init) => modifyWebpackConfig(
  85. (config) => {
  86. Object.assign(config, WEBPACK_OPTS);
  87. config.plugins.push(new webpack.DefinePlugin({
  88. ...defsObj,
  89. // Conditional compilation to remove unsafe and unused stuff from `injected`
  90. 'process.env.IS_INJECTED': JSON.stringify(/injected/.test(page) && page),
  91. }));
  92. config.optimization.minimizer.find((m, i, arr) => (
  93. m.constructor.name === 'TerserPlugin' && arr.splice(i, 1)
  94. ));
  95. config.optimization.minimizer.push(...!isProd ? [] : [
  96. new TerserPlugin(MIN_OPTS_PUBLIC),
  97. new TerserPlugin(MIN_OPTS_MAIN),
  98. ]);
  99. if (!entry) init = page;
  100. if (init) init(config);
  101. return config;
  102. }, {
  103. projectConfig: {
  104. ...mergedConfig,
  105. ...entry && { pages: { [page]: { entry } } },
  106. },
  107. },
  108. );
  109. module.exports = Promise.all([
  110. modify((config) => {
  111. addWrapperWithGlobals('common', config, defsObj, getGlobals => ({
  112. header: () => `{ ${getGlobals()}`,
  113. footer: '}',
  114. test: /^(?!injected|public).*\.js$/,
  115. }));
  116. /* Embedding as <style> to ensure uiTheme option doesn't cause FOUC.
  117. * Note that in production build there's no <head> in html but document.head is still
  118. * auto-created per the specification so our styles will be placed correctly anyway. */
  119. if (isProd) {
  120. config.plugins.push(new HTMLInlineCSSWebpackPlugin({
  121. replace: {
  122. target: '<body>',
  123. position: 'before',
  124. },
  125. }));
  126. config.plugins.find(p => (
  127. p.constructor.name === 'MiniCssExtractPlugin'
  128. && Object.assign(p.options, { ignoreOrder: true })
  129. ));
  130. }
  131. config.plugins.push(new ListBackgroundScriptsPlugin({
  132. minify: false, // keeping readable
  133. }));
  134. }),
  135. modify('injected', './src/injected', (config) => {
  136. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  137. addWrapperWithGlobals('injected/content', config, defsObj, getGlobals => ({
  138. header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
  139. footer: '}',
  140. }));
  141. }),
  142. modify('injected-web', './src/injected/web', (config) => {
  143. // TODO: replace WebPack's Object.*, .call(), .apply() with safe calls
  144. config.output.libraryTarget = 'commonjs2';
  145. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  146. addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({
  147. header: () => `${skipReinjectionHeader}
  148. window['${INIT_FUNC_NAME}'] = function (IS_FIREFOX,${HANDSHAKE_ID},${VAULT_ID}) {
  149. const module = { __proto__: null };
  150. ${getGlobals()}`,
  151. footer: `
  152. const { exports } = module;
  153. return exports.__esModule ? exports.default : exports;
  154. };0;`,
  155. }));
  156. }),
  157. ]);