Skip to content

Commit

Permalink
refactor: make manifest content scripts relative to their dev location
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Mar 23, 2024
1 parent 81176b0 commit d93059c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 67 deletions.
12 changes: 6 additions & 6 deletions app/manifest/v2/_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
{
"matches": ["file://*/*", "http://*/*", "https://*/*"],
"js": [
"disable-console.js",
"lockdown-install.js",
"lockdown-run.js",
"lockdown-more.js",
"contentscript.js",
"inpage.js"
"scripts/disable-console.js",
"scripts/lockdown-install.js",
"scripts/lockdown-run.js",
"scripts/lockdown-more.js",
"scripts/contentscript.js",
"scripts/inpage.js"
],
"run_at": "document_start",
"all_frames": true
Expand Down
10 changes: 5 additions & 5 deletions app/manifest/v3/_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
{
"matches": ["file://*/*", "http://*/*", "https://*/*"],
"js": [
"disable-console.js",
"lockdown-install.js",
"lockdown-run.js",
"lockdown-more.js",
"contentscript.js"
"scripts/disable-console.js",
"scripts/lockdown-install.js",
"scripts/lockdown-run.js",
"scripts/lockdown-more.js",
"scripts/contentscript.js"
],
"run_at": "document_start",
"all_frames": true
Expand Down
24 changes: 12 additions & 12 deletions app/scripts/app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ function importAllScripts() {
throw new Error('Missing APPLY_LAVAMOAT environment variable');
}

loadFile('./sentry-install.js');
loadFile('./scripts/sentry-install.js');

// eslint-disable-next-line no-undef
const isWorker = !self.document;
if (!isWorker) {
loadFile('./snow.js');
loadFile('./scripts/snow.js');
}

loadFile('./use-snow.js');
loadFile('./scripts/use-snow.js');

// Always apply LavaMoat in e2e test builds, so that we can capture initialization stats
if (testMode || applyLavaMoat) {
loadFile('./runtime-lavamoat.js');
loadFile('./lockdown-more.js');
loadFile('./policy-load.js');
loadFile('./scripts/runtime-lavamoat.js');
loadFile('./scripts/lockdown-more.js');
loadFile('./scripts/policy-load.js');
} else {
loadFile('./init-globals.js');
loadFile('./lockdown-install.js');
loadFile('./lockdown-run.js');
loadFile('./lockdown-more.js');
loadFile('./runtime-cjs.js');
loadFile('./scripts/init-globals.js');
loadFile('./scripts/lockdown-install.js');
loadFile('./scripts/lockdown-run.js');
loadFile('./scripts/lockdown-more.js');
loadFile('./scripts/runtime-cjs.js');
}

// This environment variable is set to a string of comma-separated relative file paths.
Expand Down Expand Up @@ -145,7 +145,7 @@ const registerInPageContentScript = async () => {
{
id: 'inpage',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['inpage.js'],
js: ['scripts/inpage.js'],
runAt: 'document_start',
world: 'MAIN',
},
Expand Down
56 changes: 32 additions & 24 deletions development/build/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const {

// map dist files to bag of needed native APIs against LM scuttling
const scuttlingConfigBase = {
'sentry-install.js': {
'scripts/sentry-install.js': {
// globals sentry need to function
window: '',
navigator: '',
Expand Down Expand Up @@ -96,8 +96,8 @@ const mv3ScuttlingConfig = { ...scuttlingConfigBase };

const standardScuttlingConfig = {
...scuttlingConfigBase,
'sentry-install.js': {
...scuttlingConfigBase['sentry-install.js'],
'scripts/sentry-install.js': {
...scuttlingConfigBase['scripts/sentry-install.js'],
document: '',
},
};
Expand Down Expand Up @@ -400,7 +400,7 @@ function createScriptTasks({
browserPlatforms,
buildTarget,
buildType,
destFilepath: `${label}.js`,
destFilepath: `scripts/${label}.js`,
entryFilepath: `./app/scripts/${label}.js`,
ignoredFiles,
label,
Expand All @@ -424,7 +424,7 @@ function createScriptTasks({
browserPlatforms,
buildTarget,
buildType,
destFilepath: `${label}.js`,
destFilepath: `scripts/${label}.js`,
entryFilepath: `./app/scripts/${label}.js`,
ignoredFiles,
label,
Expand Down Expand Up @@ -452,7 +452,7 @@ function createScriptTasks({
buildTarget,
buildType,
browserPlatforms,
destFilepath: `${inpage}.js`,
destFilepath: `scripts/${inpage}.js`,
entryFilepath: `./app/scripts/${inpage}.js`,
label: inpage,
ignoredFiles,
Expand All @@ -467,25 +467,29 @@ function createScriptTasks({
if (process.env.ENABLE_MV3) {
return;
}
// stringify inpage.js into itself, and then make it inject itself into the page
// stringify scripts/inpage.js into itself, and then make it inject itself into the page
browserPlatforms.forEach((browser) => {
makeSelfInjecting(
path.join(__dirname, `../../dist/${browser}/${inpage}.js`),
path.join(__dirname, `../../dist/${browser}/scripts/${inpage}.js`),
);
});
// delete the inpage.js source map, as it no longer represents inpage.js
// and so `yarn source-map-explorer` can't handle it. It's also not
// useful anyway, as inpage.js is injected as a `script.textContent`,
// and not tracked in Sentry or browsers devtools anyway.
// delete the scripts/inpage.js source map, as it no longer represents
// scripts/inpage.js and so `yarn source-map-explorer` can't handle it.
// It's also not useful anyway, as scripts/inpage.js is injected as a
// `script.textContent`, and not tracked in Sentry or browsers devtools
// anyway.
unlinkSync(
path.join(__dirname, `../../dist/sourcemaps/${inpage}.js.map`),
path.join(
__dirname,
`../../dist/sourcemaps/scripts/${inpage}.js.map`,
),
);
},
createNormalBundle({
buildTarget,
buildType,
browserPlatforms,
destFilepath: `${contentscript}.js`,
destFilepath: `scripts/${contentscript}.js`,
entryFilepath: `./app/scripts/${contentscript}.js`,
label: contentscript,
ignoredFiles,
Expand Down Expand Up @@ -559,7 +563,7 @@ async function createManifestV3AppInitializationBundle({
browserPlatforms: mv3BrowserPlatforms,
buildTarget,
buildType,
destFilepath: 'app-init.js',
destFilepath: 'scripts/app-init.js',
entryFilepath: './app/scripts/app-init.js',
extraEnvironmentVariables,
ignoredFiles,
Expand Down Expand Up @@ -718,7 +722,7 @@ function createFactoredBuild({
// add lavamoat policy loader file to packer output
moduleGroupPackerStream.push(
new Vinyl({
path: 'policy-load.js',
path: 'scripts/policy-load.js',
contents: lavapack.makePolicyLoaderStream(lavamoatOpts),
}),
);
Expand Down Expand Up @@ -1280,18 +1284,22 @@ function renderJavaScriptLoader({
);

const securityScripts = applyLavaMoat
? ['./runtime-lavamoat.js', './lockdown-more.js', './policy-load.js']
? [
'./scripts/runtime-lavamoat.js',
'./scripts/lockdown-more.js',
'./scripts/policy-load.js',
]
: [
'./lockdown-install.js',
'./lockdown-run.js',
'./lockdown-more.js',
'./runtime-cjs.js',
'./scripts/lockdown-install.js',
'./scripts/lockdown-run.js',
'./scripts/lockdown-more.js',
'./scripts/runtime-cjs.js',
];

const requiredScripts = [
'./snow.js',
'./use-snow.js',
'./sentry-install.js',
'./scripts/snow.js',
'./scripts/use-snow.js',
'./scripts/sentry-install.js',
...securityScripts,
...jsBundles,
];
Expand Down
18 changes: 9 additions & 9 deletions development/build/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,46 @@ function getCopyTargets(
src: shouldIncludeSnow
? `./node_modules/@lavamoat/snow/snow.prod.js`
: EMPTY_JS_FILE,
dest: `snow.js`,
dest: `scripts/snow.js`,
},
{
src: shouldIncludeSnow ? `./app/scripts/use-snow.js` : EMPTY_JS_FILE,
dest: `use-snow.js`,
dest: `scripts/use-snow.js`,
},
{
src: shouldIncludeLockdown
? getPathInsideNodeModules('ses', 'dist/lockdown.umd.min.js')
: EMPTY_JS_FILE,
dest: `lockdown-install.js`,
dest: `scripts/lockdown-install.js`,
},
{
src: './app/scripts/init-globals.js',
dest: 'init-globals.js',
dest: 'scripts/init-globals.js',
},
{
src: './app/scripts/load-app.js',
dest: 'load-app.js',
dest: 'scripts/load-app.js',
},
{
src: shouldIncludeLockdown
? `./app/scripts/lockdown-run.js`
: EMPTY_JS_FILE,
dest: `lockdown-run.js`,
dest: `scripts/lockdown-run.js`,
},
{
src: shouldIncludeLockdown
? `./app/scripts/lockdown-more.js`
: EMPTY_JS_FILE,
dest: `lockdown-more.js`,
dest: `scripts/lockdown-more.js`,
},
{
src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime-cjs.js'),
dest: `runtime-cjs.js`,
dest: `scripts/runtime-cjs.js`,
pattern: '',
},
{
src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime.js'),
dest: `runtime-lavamoat.js`,
dest: `scripts/runtime-lavamoat.js`,
pattern: '',
},
{
Expand Down
6 changes: 3 additions & 3 deletions development/sourcemap-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const fsAsync = pify(fs);
//
// Utility to help check if sourcemaps are working
//
// searches `dist/chrome/inpage.js` for "new Error" statements
// searches `dist/chrome/scripts/inpage.js` for "new Error" statements
// and prints their source lines using the sourcemaps.
// if not working it may error or print minified garbage
//
Expand All @@ -25,8 +25,8 @@ async function start() {
`common-0.js`,
`background-0.js`,
`ui-0.js`,
`contentscript.js`,
// `inpage.js`, skipped because the validator can't sample the inlined `inpage.js` script
`scripts/contentscript.js`,
// `scripts/inpage.js`, skipped because the validator can't sample the inlined `scripts/inpage.js` script
];
let valid = true;

Expand Down
16 changes: 8 additions & 8 deletions test/e2e/mv3-perf-stats/bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ const { exitWithError } = require('../../../development/lib/exit-with-error');
*/

const backgroundFiles = [
'runtime-lavamoat.js',
'lockdown-more.js',
'sentry-install.js',
'policy-load.js',
'scripts/runtime-lavamoat.js',
'scripts/lockdown-more.js',
'scripts/sentry-install.js',
'scripts/policy-load.js',
];

const uiFiles = [
'sentry-install.js',
'runtime-lavamoat.js',
'lockdown-more.js',
'policy-load.js',
'scripts/sentry-install.js',
'scripts/runtime-lavamoat.js',
'scripts/lockdown-more.js',
'scripts/policy-load.js',
];

const BackgroundFileRegex = /background-[0-9]*.js/u;
Expand Down

0 comments on commit d93059c

Please sign in to comment.