Skip to content

Commit 106d137

Browse files
committed
feat: Add iifeName option.
1 parent bb67910 commit 106d137

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

packages/wxt/e2e/tests/output-structure.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,69 @@ describe('Output Directory Structure', () => {
383383
`);
384384
});
385385

386+
it('should support a custom IIFE name', async () => {
387+
const project = new TestProject();
388+
project.addFile(
389+
'entrypoints/background.js',
390+
`export default defineBackground(() => {});`,
391+
);
392+
393+
await project.build({
394+
iifeName: (entryPointName) => `custom_iife_name_${entryPointName}`,
395+
vite: () => ({
396+
build: {
397+
// Make output for snapshot readable
398+
minify: false,
399+
},
400+
}),
401+
});
402+
403+
const contents = await project.serializeFile(
404+
'.output/chrome-mv3/background.js',
405+
);
406+
expect(contents).toMatchInlineSnapshot(`
407+
".output/chrome-mv3/background.js
408+
----------------------------------------
409+
var custom_iife_name_background = (function() {
410+
"use strict";
411+
function defineBackground(arg) {
412+
if (arg == null || typeof arg === "function") return { main: arg };
413+
return arg;
414+
}
415+
const definition = defineBackground(() => {
416+
});
417+
function initPlugins() {
418+
}
419+
globalThis.browser?.runtime?.id ? globalThis.browser : globalThis.chrome;
420+
function print(method, ...args) {
421+
return;
422+
}
423+
const logger = {
424+
debug: (...args) => print(console.debug, ...args),
425+
log: (...args) => print(console.log, ...args),
426+
warn: (...args) => print(console.warn, ...args),
427+
error: (...args) => print(console.error, ...args)
428+
};
429+
let result;
430+
try {
431+
initPlugins();
432+
result = definition.main();
433+
if (result instanceof Promise) {
434+
console.warn(
435+
"The background's main() function return a promise, but it must be synchronous"
436+
);
437+
}
438+
} catch (err) {
439+
logger.error("The background crashed on startup!");
440+
throw err;
441+
}
442+
const result$1 = result;
443+
return result$1;
444+
})();
445+
"
446+
`);
447+
});
448+
386449
it('should generate IIFE background script when type=undefined', async () => {
387450
const project = new TestProject();
388451
project.addFile(

packages/wxt/src/core/builders/vite/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
} from '../../utils/virtual-modules';
2121
import { Hookable } from 'hookable';
2222
import { toArray } from '../../utils/arrays';
23-
import { safeVarName } from '../../utils/strings';
2423
import { ViteNodeServer } from 'vite-node/server';
2524
import { ViteNodeRunner } from 'vite-node/client';
2625
import { installSourcemapsSupport } from 'vite-node/source-map';
@@ -109,7 +108,7 @@ export async function createViteBuilder(
109108
const plugins: NonNullable<vite.UserConfig['plugins']> = [
110109
wxtPlugins.entrypointGroupGlobals(entrypoint),
111110
];
112-
const iifeReturnValueName = safeVarName(entrypoint.name);
111+
const iifeReturnValueName = wxtConfig.iifeName(entrypoint.name);
113112

114113
if (
115114
entrypoint.type === 'content-script-style' ||

packages/wxt/src/core/resolve-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { safeStringToNumber } from './utils/number';
2929
import { loadEnv } from './utils/env';
3030
import { getPort } from 'get-port-please';
3131
import { fileURLToPath, pathToFileURL } from 'node:url';
32+
import { safeVarName } from './utils/strings';
3233

3334
/**
3435
* Given an inline config, discover the config file if necessary, merge the results, resolve any
@@ -236,6 +237,7 @@ export async function resolveConfig(
236237
},
237238
hooks: mergedConfig.hooks ?? {},
238239
vite: mergedConfig.vite ?? (() => ({})),
240+
iifeName: mergedConfig.iifeName ?? safeVarName,
239241
builtinModules,
240242
userModules,
241243
plugins: [],

packages/wxt/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ export interface InlineConfig {
374374
* "wxt-module-analytics").
375375
*/
376376
modules?: string[];
377+
/**
378+
* The name to use for each entrypoint IIFE global variable.
379+
* If not set, a safe variable name will be generated from the entrypoint name.
380+
*/
381+
iifeName?: (entrypointName: string) => string;
377382
}
378383

379384
// TODO: Extract to @wxt/vite-builder and use module augmentation to include the vite field
@@ -1400,6 +1405,7 @@ export interface ResolvedConfig {
14001405
* ["@wxt-dev/module-vue/plugin", "wxt-module-google-analytics/plugin"]
14011406
*/
14021407
plugins: string[];
1408+
iifeName: (entryPointName: string) => string;
14031409
}
14041410

14051411
export interface FsCache {

0 commit comments

Comments
 (0)