webpack.conf.js 3.5 KB

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