webpack.conf.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const webpack = require('webpack');
  2. const { ListBackgroundScriptsPlugin } = require('./manifest-helper');
  3. const { addWrapperWithGlobals, getCodeMirrorThemes } = require('./webpack-util');
  4. const ProtectWebpackBootstrapPlugin = require('./webpack-protect-bootstrap-plugin');
  5. const { getVersion } = require('./version-helper');
  6. const { configLoader } = require('./config-helper');
  7. const { getBaseConfig, getPageConfig, isProd } = require('./webpack-base');
  8. // Avoiding collisions with globals of a content-mode userscript
  9. const INIT_FUNC_NAME = '**VMInitInjection**';
  10. const VAULT_ID = 'VAULT_ID';
  11. const PAGE_MODE_HANDSHAKE = 'PAGE_MODE_HANDSHAKE';
  12. const VM_VER = getVersion();
  13. global.localStorage = {}; // workaround for node 25 and HtmlWebpackPlugin's `...global`
  14. configLoader
  15. // Default values
  16. .add({
  17. DEBUG: false,
  18. })
  19. // Load from `./.env`
  20. .envFile()
  21. // Load from `process.env`
  22. .env()
  23. // Override values
  24. .add({
  25. VM_VER,
  26. });
  27. const pickEnvs = (items) => {
  28. return Object.assign({}, ...items.map(key => ({
  29. [`process.env.${key}`]: JSON.stringify(configLoader.get(key)),
  30. })));
  31. };
  32. const defsObj = {
  33. ...pickEnvs([
  34. 'DEBUG',
  35. 'VM_VER',
  36. 'SYNC_GOOGLE_CLIENT_ID',
  37. 'SYNC_GOOGLE_CLIENT_SECRET',
  38. 'SYNC_GOOGLE_DESKTOP_ID',
  39. 'SYNC_GOOGLE_DESKTOP_SECRET',
  40. 'SYNC_ONEDRIVE_CLIENT_ID',
  41. 'SYNC_ONEDRIVE_CLIENT_SECRET',
  42. 'SYNC_DROPBOX_CLIENT_ID',
  43. ]),
  44. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  45. 'process.env.CODEMIRROR_THEMES': JSON.stringify(getCodeMirrorThemes()),
  46. 'process.env.DEV': JSON.stringify(!isProd),
  47. 'process.env.TEST': JSON.stringify(process.env.BABEL_ENV === 'test'),
  48. };
  49. // avoid running webpack bootstrap in a potentially hacked environment
  50. // after documentElement was replaced which triggered reinjection of content scripts
  51. const skipReinjectionHeader = `{
  52. const INIT_FUNC_NAME = '${INIT_FUNC_NAME}';
  53. if (window[INIT_FUNC_NAME] !== 1)`;
  54. const buildConfig = (page, entry, init) => {
  55. const config = entry ? getBaseConfig() : getPageConfig();
  56. config.plugins.push(new webpack.DefinePlugin({
  57. ...defsObj,
  58. // Conditional compilation to remove unsafe and unused stuff from `injected`
  59. 'process.env.IS_INJECTED': JSON.stringify(/injected/.test(page) && page),
  60. }));
  61. if (typeof entry === 'string') {
  62. config.entry = { [page]: entry };
  63. }
  64. if (!entry) init = page;
  65. if (init) init(config);
  66. return config;
  67. };
  68. module.exports = [
  69. buildConfig((config) => {
  70. addWrapperWithGlobals('common', config, defsObj, getGlobals => ({
  71. header: () => `{ ${getGlobals()}`,
  72. footer: '}',
  73. test: /^(?!injected|public).*\.js$/,
  74. }));
  75. config.plugins.push(new ListBackgroundScriptsPlugin({
  76. minify: false, // keeping readable
  77. }));
  78. }),
  79. buildConfig('injected', './src/injected', (config) => {
  80. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  81. addWrapperWithGlobals('injected/content', config, defsObj, getGlobals => ({
  82. header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
  83. footer: '}}',
  84. }));
  85. }),
  86. buildConfig('injected-web', './src/injected/web', (config) => {
  87. config.output.libraryTarget = 'commonjs2';
  88. config.plugins.push(new ProtectWebpackBootstrapPlugin());
  89. addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({
  90. header: () => `${skipReinjectionHeader}
  91. window[INIT_FUNC_NAME] = function (IS_FIREFOX,${PAGE_MODE_HANDSHAKE},${VAULT_ID}) {
  92. const module = { __proto__: null };
  93. ${getGlobals()}`,
  94. footer: `
  95. const { exports } = module;
  96. return exports.__esModule ? exports.default : exports;
  97. }};0;`,
  98. }));
  99. }),
  100. ];