Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d5fe4248e2 | |||
| 58140cf3e8 | |||
| 1a39f856af | |||
| 82b769e939 |
@@ -362,11 +362,13 @@ describe('Output Directory Structure', () => {
|
||||
function logHello(name) {
|
||||
console.log(\`Hello \${name}!\`);
|
||||
}
|
||||
_background;
|
||||
const definition = defineBackground({
|
||||
main() {
|
||||
logHello("background");
|
||||
}
|
||||
});
|
||||
_background;
|
||||
chrome;
|
||||
function print(method, ...args) {
|
||||
return;
|
||||
@@ -389,6 +391,7 @@ describe('Output Directory Structure', () => {
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
_background;
|
||||
"
|
||||
`);
|
||||
});
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "wxt",
|
||||
"type": "module",
|
||||
"version": "0.17.12",
|
||||
"version": "0.17.13-alpha1",
|
||||
"description": "Next gen framework for developing web extensions",
|
||||
"engines": {
|
||||
"node": ">=18",
|
||||
|
||||
Generated
+3
-3
@@ -4919,8 +4919,8 @@ packages:
|
||||
loose-envify: 1.4.0
|
||||
dev: false
|
||||
|
||||
/scule@1.0.0:
|
||||
resolution: {integrity: sha512-4AsO/FrViE/iDNEPaAQlb77tf0csuq27EsVpy6ett584EcRTp6pTDLoGWVxCD77y5iU5FauOvhsI4o1APwPoSQ==}
|
||||
/scule@1.3.0:
|
||||
resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==}
|
||||
dev: false
|
||||
|
||||
/semver-diff@4.0.0:
|
||||
@@ -5550,7 +5550,7 @@ packages:
|
||||
mlly: 1.4.2
|
||||
pathe: 1.1.1
|
||||
pkg-types: 1.0.3
|
||||
scule: 1.0.0
|
||||
scule: 1.3.0
|
||||
strip-literal: 1.3.0
|
||||
unplugin: 1.5.0
|
||||
transitivePeerDependencies:
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
} from '~/core/utils/virtual-modules';
|
||||
import { Hookable } from 'hookable';
|
||||
import { toArray } from '~/core/utils/arrays';
|
||||
import { safeVarName } from '~/core/utils/strings';
|
||||
|
||||
export async function createViteBuilder(
|
||||
wxtConfig: ResolvedConfig,
|
||||
@@ -87,14 +88,22 @@ export async function createViteBuilder(
|
||||
plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig));
|
||||
}
|
||||
|
||||
const iifeReturnValueName = safeVarName(entrypoint.name);
|
||||
const libMode: vite.UserConfig = {
|
||||
mode: wxtConfig.mode,
|
||||
plugins,
|
||||
esbuild: {
|
||||
// Add a footer with the returned value so it can return values to `scripting.executeScript`
|
||||
// Footer is added apart of esbuild to make sure it's not minified. It
|
||||
// get's removed if added to `build.rollupOptions.output.footer`
|
||||
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
|
||||
footer: iifeReturnValueName + ';',
|
||||
},
|
||||
build: {
|
||||
lib: {
|
||||
entry,
|
||||
formats: ['iife'],
|
||||
name: '_',
|
||||
name: iifeReturnValueName,
|
||||
fileName: entrypoint.name,
|
||||
},
|
||||
rollupOptions: {
|
||||
|
||||
@@ -5,6 +5,14 @@ export function kebabCaseAlphanumeric(str: string): string {
|
||||
.replace(/\s+/g, '-'); // Replace spaces with hyphens
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a safe variable name for a given string.
|
||||
*/
|
||||
export function safeVarName(str: string): string {
|
||||
// _ prefix to ensure it doesn't start with a number
|
||||
return '_' + kebabCaseAlphanumeric(str).replace('-', '_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes import statements from the top of a file. Keeps import.meta and inline, async `import()`
|
||||
* calls.
|
||||
|
||||
+15
-3
@@ -737,16 +737,24 @@ export interface IsolatedWorldContentScriptDefinition
|
||||
extends IsolatedWorldContentScriptEntrypointOptions {
|
||||
/**
|
||||
* Main function executed when the content script is loaded.
|
||||
*
|
||||
* When running a content script with `browser.scripting.executeScript`,
|
||||
* values returned from this function will be returned in the `executeScript`
|
||||
* result as well. Otherwise returning a value does nothing.
|
||||
*/
|
||||
main(ctx: ContentScriptContext): void | Promise<void>;
|
||||
main(ctx: ContentScriptContext): any | Promise<any>;
|
||||
}
|
||||
|
||||
export interface MainWorldContentScriptDefinition
|
||||
extends MainWorldContentScriptEntrypointOptions {
|
||||
/**
|
||||
* Main function executed when the content script is loaded.
|
||||
*
|
||||
* When running a content script with `browser.scripting.executeScript`,
|
||||
* values returned from this function will be returned in the `executeScript`
|
||||
* result as well. Otherwise returning a value does nothing.
|
||||
*/
|
||||
main(): void | Promise<void>;
|
||||
main(): any | Promise<any>;
|
||||
}
|
||||
|
||||
export type ContentScriptDefinition =
|
||||
@@ -763,8 +771,12 @@ export interface BackgroundDefinition extends BackgroundEntrypointOptions {
|
||||
export interface UnlistedScriptDefinition extends BaseEntrypointOptions {
|
||||
/**
|
||||
* Main function executed when the unlisted script is ran.
|
||||
*
|
||||
* When running a content script with `browser.scripting.executeScript`,
|
||||
* values returned from this function will be returned in the `executeScript`
|
||||
* result as well. Otherwise returning a value does nothing.
|
||||
*/
|
||||
main(): void | Promise<void>;
|
||||
main(): any | Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,16 +2,23 @@ import definition from 'virtual:user-content-script-isolated-world-entrypoint';
|
||||
import { logger } from '../sandbox/utils/logger';
|
||||
import { ContentScriptContext } from 'wxt/client';
|
||||
|
||||
(async () => {
|
||||
const result = (async () => {
|
||||
try {
|
||||
const { main, ...options } = definition;
|
||||
const ctx = new ContentScriptContext(import.meta.env.ENTRYPOINT, options);
|
||||
|
||||
await main(ctx);
|
||||
return await main(ctx);
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`The content script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
|
||||
err,
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
|
||||
// Return the main function's result to the background when executed via the
|
||||
// scripting API. Default export causes the IIFE to return a value.
|
||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
|
||||
// Tested on both Chrome and Firefox
|
||||
export default result;
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import definition from 'virtual:user-content-script-main-world-entrypoint';
|
||||
import { logger } from '../sandbox/utils/logger';
|
||||
|
||||
(async () => {
|
||||
const result = (async () => {
|
||||
try {
|
||||
const { main } = definition;
|
||||
await main();
|
||||
return await main();
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`The content script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
|
||||
err,
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
|
||||
// Return the main function's result to the background when executed via the
|
||||
// scripting API. Default export causes the IIFE to return a value.
|
||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
|
||||
// Tested on both Chrome and Firefox
|
||||
export default result;
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
import definition from 'virtual:user-unlisted-script-entrypoint';
|
||||
import { logger } from '../sandbox/utils/logger';
|
||||
|
||||
(async () => {
|
||||
const result = (async () => {
|
||||
try {
|
||||
await definition.main();
|
||||
return await definition.main();
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
|
||||
err,
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
|
||||
// Return the main function's result to the background when executed via the
|
||||
// scripting API. Default export causes the IIFE to return a value.
|
||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
|
||||
// Tested on both Chrome and Firefox
|
||||
export default result;
|
||||
|
||||
Reference in New Issue
Block a user