mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 09:23:21 +01:00
MV3: fix injection of applyLavamoat variable in service worker (#14920)
This commit is contained in:
parent
4048feeaac
commit
843beb6d20
@ -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 */
|
||||
];
|
||||
|
||||
|
18
app/scripts/init-globals.js
Normal file
18
app/scripts/init-globals.js
Normal file
@ -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;
|
||||
}
|
@ -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;
|
||||
|
@ -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`
|
||||
|
Loading…
Reference in New Issue
Block a user