diff --git a/app/scripts/app-init.js b/app/scripts/app-init.js index ba66698b7..7b9c1dfd1 100644 --- a/app/scripts/app-init.js +++ b/app/scripts/app-init.js @@ -12,9 +12,7 @@ function tryImport(...fileNames) { function importAllScripts() { const startImportScriptsTime = Date.now(); - // applyLavaMoat has been hard coded to "true" as - // tryImport('./runtime-cjs.js') is giving issue with XMLHttpRequest object which is not avaialble to service worker. - // we need to dynamically inject values of applyLavaMoat once this is fixed. + // value of applyLavaMoat below is dynamically replaced at build time with actual value const applyLavaMoat = true; tryImport('./globalthis.js'); @@ -25,14 +23,16 @@ function importAllScripts() { tryImport('./lockdown-more.js'); tryImport('./policy-load.js'); } else { + tryImport('./init-globals.js'); tryImport('./lockdown-install.js'); - tryImport('./lockdown-more.js'); tryImport('./lockdown-run.js'); + tryImport('./lockdown-more.js'); tryImport('./runtime-cjs.js'); } const fileList = [ // The list of files is injected at build time by replacing comment below with comma separated strings of file names + // https://github.com/MetaMask/metamask-extension/blob/496d9d81c3367931031edc11402552690c771acf/development/build/scripts.js#L406 /** FILE NAMES */ ]; diff --git a/app/scripts/init-globals.js b/app/scripts/init-globals.js new file mode 100644 index 000000000..a26c95266 --- /dev/null +++ b/app/scripts/init-globals.js @@ -0,0 +1,18 @@ +/** + * This script add properties in globalThis and initialises them with undefined. + * This is workaround needed to avoid error in dependencies expecting to be run in a browser + * these dependencies are not available to service worker in MV3. + */ + +// eslint-disable-next-line import/unambiguous +const keys = ['XMLHttpRequest']; + +keys.forEach((key) => { + if (!Reflect.has(globalThis, key)) { + globalThis[key] = undefined; + } +}); + +if (!Reflect.has(globalThis, 'window')) { + globalThis.window = globalThis; +} diff --git a/development/build/scripts.js b/development/build/scripts.js index 21892519e..70db2a694 100644 --- a/development/build/scripts.js +++ b/development/build/scripts.js @@ -365,11 +365,20 @@ function createScriptTasks({ } } -const postProcessServiceWorker = (mv3BrowserPlatforms, fileList) => { +const postProcessServiceWorker = ( + mv3BrowserPlatforms, + fileList, + applyLavaMoat, +) => { mv3BrowserPlatforms.forEach((browser) => { const appInitFile = `./dist/${browser}/app-init.js`; const fileContent = readFileSync('./app/scripts/app-init.js', 'utf8'); - const fileOutput = fileContent.replace('/** FILE NAMES */', fileList); + const fileOutput = fileContent + .replace('/** FILE NAMES */', fileList) + .replace( + 'const applyLavaMoat = true;', + `const applyLavaMoat = ${applyLavaMoat};`, + ); writeFileSync(appInitFile, fileOutput); }); }; @@ -385,6 +394,7 @@ async function bundleMV3AppInitialiser({ testing, policyOnly, shouldLintFenceFiles, + applyLavaMoat, }) { const label = 'app-init'; // TODO: remove this filter for firefox once MV3 is supported in it @@ -409,14 +419,14 @@ async function bundleMV3AppInitialiser({ shouldLintFenceFiles, })(); - postProcessServiceWorker(mv3BrowserPlatforms, fileList); + postProcessServiceWorker(mv3BrowserPlatforms, fileList, applyLavaMoat); let prevChromeFileContent; watch('./dist/chrome/app-init.js', () => { const chromeFileContent = readFileSync('./dist/chrome/app-init.js', 'utf8'); if (chromeFileContent !== prevChromeFileContent) { prevChromeFileContent = chromeFileContent; - postProcessServiceWorker(mv3BrowserPlatforms, fileList); + postProcessServiceWorker(mv3BrowserPlatforms, fileList, applyLavaMoat); } }); @@ -595,6 +605,7 @@ function createFactoredBuild({ testing, policyOnly, shouldLintFenceFiles, + applyLavaMoat, }); } break; diff --git a/development/build/static.js b/development/build/static.js index d1f024fbc..48178ef7e 100644 --- a/development/build/static.js +++ b/development/build/static.js @@ -152,6 +152,10 @@ function getCopyTargets(shouldIncludeLockdown) { : EMPTY_JS_FILE, dest: `lockdown-install.js`, }, + { + src: './app/scripts/init-globals.js', + dest: 'init-globals.js', + }, { src: shouldIncludeLockdown ? `./app/scripts/lockdown-run.js`