const { modifyWebpackConfig, shallowMerge, defaultOptions } = require('@gera2ld/plaid'); const { isProd } = require('@gera2ld/plaid/util'); const fs = require('fs'); const webpack = require('webpack'); const WrapperWebpackPlugin = require('wrapper-webpack-plugin'); const HTMLInlineCSSWebpackPlugin = isProd && require('html-inline-css-webpack-plugin').default; const projectConfig = require('./plaid.conf'); const mergedConfig = shallowMerge(defaultOptions, projectConfig); const INIT_FUNC_NAME = 'VMInitInjection'; // Copied from gulpfile.js: strip alphabetic suffix const VM_VER = require('../package.json').version.replace(/-[^.]*/, ''); const pickEnvs = (items) => { return Object.assign({}, ...items.map(x => ({ [`process.env.${x.key}`]: JSON.stringify( 'val' in x ? x.val : process.env[x.key] ?? x.def ), }))); }; const definitions = new webpack.DefinePlugin({ ...pickEnvs([ { key: 'DEBUG', def: false }, { key: 'VM_VER', val: VM_VER }, { key: 'SYNC_GOOGLE_CLIENT_ID' }, { key: 'SYNC_GOOGLE_CLIENT_SECRET' }, { key: 'SYNC_ONEDRIVE_CLIENT_ID' }, { key: 'SYNC_ONEDRIVE_CLIENT_SECRET' }, ]), 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME), }); // avoid running webpack bootstrap in a potentially hacked environment // after documentElement was replaced which triggered reinjection of content scripts const skipReinjectionHeader = `if (window['${INIT_FUNC_NAME}'] !== 1)`; // {entryName: path} const entryGlobals = { common: './src/common/safe-globals.js', injected: './src/injected/safe-injected-globals.js', }; /** * Adds a watcher for files in entryGlobals to properly recompile the project on changes. */ const addWrapper = (config, name, callback) => { if (!callback) { callback = name; name = ''; } const globals = Object.entries(entryGlobals).filter(([key]) => name === key || !name); const dirs = globals.map(([key]) => key).join('|'); config.module.rules.push({ test: new RegExp(`/(${dirs})/index\\.js$`.replace(/\//g, /[/\\]/.source)), use: [{ loader: './scripts/fake-dep-loader.js', options: { files: globals.map(([, path]) => path), }, }], }); const reader = () => ( globals.map(([, path]) => ( fs.readFileSync(path, { encoding: 'utf8' }) .replace(/export\s+(?=const\s)/g, '') )) ).join('\n'); config.plugins.push(new WrapperWebpackPlugin(callback(reader))); }; const modify = (page, entry, init) => modifyWebpackConfig( (config) => { config.node = { process: false, setImmediate: false, }; config.plugins.push(definitions); if (!entry) init = page; if (init) init(config); return config; }, { projectConfig: { ...mergedConfig, ...entry && { pages: { [page]: { entry }} }, }, }, ); module.exports = Promise.all([ modify((config) => { addWrapper(config, 'common', getGlobals => ({ header: () => `{ ${getGlobals()}`, footer: '}', test: /^(?!injected|public).*\.js$/, })); /* Embedding as