Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webextension] Add support for background.scripts and background.page #8906

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 84 additions & 40 deletions packages/transformers/webextension/src/WebExtensionTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ async function collectDependencies(
}
}
}

if (isMV2) {
if (program.background?.page) {
program.background.page = asset.addURLDependency(
Expand Down Expand Up @@ -306,50 +307,93 @@ async function collectDependencies(
}
}
} else {
if (program.background?.service_worker) {
program.background.service_worker = asset.addURLDependency(
program.background.service_worker,
{
bundleBehavior: 'isolated',
loc: {
filePath,
...getJSONSourceLocation(
ptrs['/background/service_worker'],
'value',
),
},
env: {
context: 'service-worker',
sourceType:
program.background.type == 'module' ? 'module' : 'script',
if (program.browser_specific_settings?.gecko) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Using browser_specific_settings.gecko to switch between Firefox and Chrome. Firefox add-on need an ID in their manifest.json when submitted to AMO.

All Manifest V3 extensions need an add-on ID in their manifest.json when submitted to AMO.
https://extensionworkshop.com/documentation/develop/extensions-and-the-add-on-id/

But, it's not necessary in debugging. I want to know some useful flags.

// Firefox Addon
if (program.background?.page) {
program.background.page = asset.addURLDependency(
program.background.page,
{
bundleBehavior: 'isolated',
loc: {
filePath,
...getJSONSourceLocation(ptrs['/background/page'], 'value'),
},
},
},
);
}
if (hot) {
// Enable eval HMR for sandbox,
const csp = program.content_security_policy || {};
csp.extension_pages = cspPatchHMR(
csp.extension_pages,
`http://${hmrOptions?.host || 'localhost'}`,
);
// Sandbox allows eval by default
if (csp.sandbox) csp.sandbox = cspPatchHMR(csp.sandbox);
program.content_security_policy = csp;
if (needRuntimeBG) {
if (!program.background) {
program.background = {};
);
if (needRuntimeBG) {
asset.meta.webextBGInsert = program.background.page;
}
if (!program.background.service_worker) {
program.background.service_worker = asset.addURLDependency(
'./runtime/default-bg.js',
{
resolveFrom: __filename,
env: {context: 'service-worker'},
}
if (hot) {
// To enable HMR, we must override the CSP to allow 'unsafe-eval'
const csp = program.content_security_policy || {};
csp.extension_pages = cspPatchHMR(csp.extension_pages);
program.content_security_policy = csp;

if (needRuntimeBG && !program.background?.page) {
if (!program.background) {
program.background = {};
}
if (!program.background.scripts) {
program.background.scripts = [];
}
if (program.background.scripts.length == 0) {
program.background.scripts.push(
asset.addURLDependency('./runtime/default-bg.js', {
resolveFrom: __filename,
}),
);
}
asset.meta.webextBGInsert = program.background.scripts[0];
}
}
} else {
// Chrome Extension
if (program.background?.service_worker) {
program.background.service_worker = asset.addURLDependency(
program.background.service_worker,
{
bundleBehavior: 'isolated',
loc: {
filePath,
...getJSONSourceLocation(
ptrs['/background/service_worker'],
'value',
),
},
);
env: {
context: 'service-worker',
sourceType:
program.background.type == 'module' ? 'module' : 'script',
},
},
);
}
if (hot) {
// Enable eval HMR for sandbox,
const csp = program.content_security_policy || {};
csp.extension_pages = cspPatchHMR(
csp.extension_pages,
`http://${hmrOptions?.host || 'localhost'}`,
);
// Sandbox allows eval by default
if (csp.sandbox) csp.sandbox = cspPatchHMR(csp.sandbox);
program.content_security_policy = csp;
if (needRuntimeBG) {
if (!program.background) {
program.background = {};
}
if (!program.background.service_worker) {
program.background.service_worker = asset.addURLDependency(
'./runtime/default-bg.js',
{
resolveFrom: __filename,
env: {context: 'service-worker'},
},
);
}
asset.meta.webextBGInsert = program.background.service_worker;
}
asset.meta.webextBGInsert = program.background.service_worker;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/transformers/webextension/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,14 @@ export const MV3Schema = ({
type: 'object',
properties: {
service_worker: string,
scripts: arrStr,
page: string,
type: {
type: 'string',
enum: ['classic', 'module'],
},
},
additionalProperties: false,
required: ['service_worker'],
},
content_security_policy: {
type: 'object',
Expand Down