mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +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() {
|
function importAllScripts() {
|
||||||
const startImportScriptsTime = Date.now();
|
const startImportScriptsTime = Date.now();
|
||||||
// applyLavaMoat has been hard coded to "true" as
|
// value of applyLavaMoat below is dynamically replaced at build time with actual value
|
||||||
// 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.
|
|
||||||
const applyLavaMoat = true;
|
const applyLavaMoat = true;
|
||||||
|
|
||||||
tryImport('./globalthis.js');
|
tryImport('./globalthis.js');
|
||||||
@ -25,14 +23,16 @@ function importAllScripts() {
|
|||||||
tryImport('./lockdown-more.js');
|
tryImport('./lockdown-more.js');
|
||||||
tryImport('./policy-load.js');
|
tryImport('./policy-load.js');
|
||||||
} else {
|
} else {
|
||||||
|
tryImport('./init-globals.js');
|
||||||
tryImport('./lockdown-install.js');
|
tryImport('./lockdown-install.js');
|
||||||
tryImport('./lockdown-more.js');
|
|
||||||
tryImport('./lockdown-run.js');
|
tryImport('./lockdown-run.js');
|
||||||
|
tryImport('./lockdown-more.js');
|
||||||
tryImport('./runtime-cjs.js');
|
tryImport('./runtime-cjs.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileList = [
|
const fileList = [
|
||||||
// The list of files is injected at build time by replacing comment below with comma separated strings of file names
|
// 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 */
|
/** 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) => {
|
mv3BrowserPlatforms.forEach((browser) => {
|
||||||
const appInitFile = `./dist/${browser}/app-init.js`;
|
const appInitFile = `./dist/${browser}/app-init.js`;
|
||||||
const fileContent = readFileSync('./app/scripts/app-init.js', 'utf8');
|
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);
|
writeFileSync(appInitFile, fileOutput);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -385,6 +394,7 @@ async function bundleMV3AppInitialiser({
|
|||||||
testing,
|
testing,
|
||||||
policyOnly,
|
policyOnly,
|
||||||
shouldLintFenceFiles,
|
shouldLintFenceFiles,
|
||||||
|
applyLavaMoat,
|
||||||
}) {
|
}) {
|
||||||
const label = 'app-init';
|
const label = 'app-init';
|
||||||
// TODO: remove this filter for firefox once MV3 is supported in it
|
// TODO: remove this filter for firefox once MV3 is supported in it
|
||||||
@ -409,14 +419,14 @@ async function bundleMV3AppInitialiser({
|
|||||||
shouldLintFenceFiles,
|
shouldLintFenceFiles,
|
||||||
})();
|
})();
|
||||||
|
|
||||||
postProcessServiceWorker(mv3BrowserPlatforms, fileList);
|
postProcessServiceWorker(mv3BrowserPlatforms, fileList, applyLavaMoat);
|
||||||
|
|
||||||
let prevChromeFileContent;
|
let prevChromeFileContent;
|
||||||
watch('./dist/chrome/app-init.js', () => {
|
watch('./dist/chrome/app-init.js', () => {
|
||||||
const chromeFileContent = readFileSync('./dist/chrome/app-init.js', 'utf8');
|
const chromeFileContent = readFileSync('./dist/chrome/app-init.js', 'utf8');
|
||||||
if (chromeFileContent !== prevChromeFileContent) {
|
if (chromeFileContent !== prevChromeFileContent) {
|
||||||
prevChromeFileContent = chromeFileContent;
|
prevChromeFileContent = chromeFileContent;
|
||||||
postProcessServiceWorker(mv3BrowserPlatforms, fileList);
|
postProcessServiceWorker(mv3BrowserPlatforms, fileList, applyLavaMoat);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -595,6 +605,7 @@ function createFactoredBuild({
|
|||||||
testing,
|
testing,
|
||||||
policyOnly,
|
policyOnly,
|
||||||
shouldLintFenceFiles,
|
shouldLintFenceFiles,
|
||||||
|
applyLavaMoat,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -152,6 +152,10 @@ function getCopyTargets(shouldIncludeLockdown) {
|
|||||||
: EMPTY_JS_FILE,
|
: EMPTY_JS_FILE,
|
||||||
dest: `lockdown-install.js`,
|
dest: `lockdown-install.js`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
src: './app/scripts/init-globals.js',
|
||||||
|
dest: 'init-globals.js',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
src: shouldIncludeLockdown
|
src: shouldIncludeLockdown
|
||||||
? `./app/scripts/lockdown-run.js`
|
? `./app/scripts/lockdown-run.js`
|
||||||
|
Loading…
Reference in New Issue
Block a user