Skip to content

Commit 190be7f

Browse files
fix(rolldown-compat): Only apply footer to content and unlisted scripts via plugin (#1793)
Co-authored-by: Aaron <[email protected]>
1 parent 0333ce5 commit 190be7f

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect } from 'vitest';
1+
import { describe, expect, it } from 'vitest';
22
import { TestProject } from '../utils';
33

44
describe('Output Directory Structure', () => {
@@ -362,13 +362,11 @@ describe('Output Directory Structure', () => {
362362
function logHello(name) {
363363
console.log(\`Hello \${name}!\`);
364364
}
365-
background;
366365
const definition = defineBackground({
367366
main() {
368367
logHello("background");
369368
}
370369
});
371-
background;
372370
function initPlugins() {
373371
}
374372
((_b = (_a = globalThis.browser) == null ? void 0 : _a.runtime) == null ? void 0 : _b.id) ? globalThis.browser : globalThis.chrome;
@@ -397,7 +395,6 @@ describe('Output Directory Structure', () => {
397395
const result$1 = result;
398396
return result$1;
399397
}();
400-
background;
401398
"
402399
`);
403400
});

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { ViteNodeServer } from 'vite-node/server';
2525
import { ViteNodeRunner } from 'vite-node/client';
2626
import { installSourcemapsSupport } from 'vite-node/source-map';
2727
import { createExtensionEnvironment } from '../../utils/environments';
28-
import { relative, join, extname, dirname } from 'node:path';
28+
import { dirname, extname, join, relative } from 'node:path';
2929
import fs from 'fs-extra';
3030
import { normalizePath } from '../../utils/paths';
3131

@@ -109,24 +109,25 @@ export async function createViteBuilder(
109109
const plugins: NonNullable<vite.UserConfig['plugins']> = [
110110
wxtPlugins.entrypointGroupGlobals(entrypoint),
111111
];
112+
const iifeReturnValueName = safeVarName(entrypoint.name);
113+
112114
if (
113115
entrypoint.type === 'content-script-style' ||
114116
entrypoint.type === 'unlisted-style'
115117
) {
116118
plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig));
117119
}
118120

119-
const iifeReturnValueName = safeVarName(entrypoint.name);
120-
const libMode: vite.UserConfig = {
121+
if (
122+
entrypoint.type === 'content-script' ||
123+
entrypoint.type === 'unlisted-script'
124+
) {
125+
plugins.push(wxtPlugins.iifeFooter(iifeReturnValueName));
126+
}
127+
128+
return {
121129
mode: wxtConfig.mode,
122130
plugins,
123-
esbuild: {
124-
// Add a footer with the returned value so it can return values to `scripting.executeScript`
125-
// Footer is added a part of esbuild to make sure it's not minified. It
126-
// get's removed if added to `build.rollupOptions.output.footer`
127-
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
128-
footer: iifeReturnValueName + ';',
129-
},
130131
build: {
131132
lib: {
132133
entry,
@@ -162,8 +163,7 @@ export async function createViteBuilder(
162163
// See https://github.com/aklinker1/vite-plugin-web-extension/issues/96
163164
'process.env.NODE_ENV': JSON.stringify(wxtConfig.mode),
164165
},
165-
};
166-
return libMode;
166+
} satisfies vite.UserConfig;
167167
};
168168

169169
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Plugin } from 'vite';
2+
3+
/**
4+
* Add a footer with the returned value so it can return values to `scripting.executeScript`
5+
* Footer is added a part of esbuild to make sure it's not minified. It
6+
* get's removed if added to `build.rollupOptions.output.footer`
7+
* See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
8+
*/
9+
export function iifeFooter(iifeReturnValueName: string): Plugin {
10+
return {
11+
name: 'wxt:iife-footer',
12+
generateBundle(_, bundle) {
13+
for (const chunk of Object.values(bundle)) {
14+
if (chunk.type === 'chunk' && chunk.isEntry) {
15+
chunk.code += `${iifeReturnValueName};`;
16+
}
17+
}
18+
},
19+
};
20+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './defineImportMeta';
1313
export * from './removeEntrypointMainFunction';
1414
export * from './wxtPluginLoader';
1515
export * from './resolveAppConfig';
16+
export * from './iifeFooter';

packages/wxt/src/core/utils/building/build-entrypoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
EntrypointGroup,
55
ResolvedPublicFile,
66
} from '../../../types';
7-
import { getPublicFiles } from '../../utils/fs';
7+
import { getPublicFiles } from '../fs';
88
import fs from 'fs-extra';
99
import { dirname, resolve } from 'path';
1010
import type { Ora } from 'ora';

packages/wxt/src/utils/app-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @module wxt/utils/app-config */
22
// @ts-expect-error: Untyped virtual module
33
import appConfig from 'virtual:app-config';
4-
import type { WxtAppConfig } from '../utils/define-app-config';
4+
import type { WxtAppConfig } from './define-app-config';
55

66
export function useAppConfig(): WxtAppConfig {
77
return appConfig;

0 commit comments

Comments
 (0)