fix(rolldown-compat): Only apply footer to content and unlisted scripts via plugin (#1793)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { TestProject } from '../utils';
|
||||
|
||||
describe('Output Directory Structure', () => {
|
||||
@@ -362,13 +362,11 @@ describe('Output Directory Structure', () => {
|
||||
function logHello(name) {
|
||||
console.log(\`Hello \${name}!\`);
|
||||
}
|
||||
background;
|
||||
const definition = defineBackground({
|
||||
main() {
|
||||
logHello("background");
|
||||
}
|
||||
});
|
||||
background;
|
||||
function initPlugins() {
|
||||
}
|
||||
((_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', () => {
|
||||
const result$1 = result;
|
||||
return result$1;
|
||||
}();
|
||||
background;
|
||||
"
|
||||
`);
|
||||
});
|
||||
|
||||
@@ -25,7 +25,7 @@ import { ViteNodeServer } from 'vite-node/server';
|
||||
import { ViteNodeRunner } from 'vite-node/client';
|
||||
import { installSourcemapsSupport } from 'vite-node/source-map';
|
||||
import { createExtensionEnvironment } from '../../utils/environments';
|
||||
import { relative, join, extname, dirname } from 'node:path';
|
||||
import { dirname, extname, join, relative } from 'node:path';
|
||||
import fs from 'fs-extra';
|
||||
import { normalizePath } from '../../utils/paths';
|
||||
|
||||
@@ -109,6 +109,8 @@ export async function createViteBuilder(
|
||||
const plugins: NonNullable<vite.UserConfig['plugins']> = [
|
||||
wxtPlugins.entrypointGroupGlobals(entrypoint),
|
||||
];
|
||||
const iifeReturnValueName = safeVarName(entrypoint.name);
|
||||
|
||||
if (
|
||||
entrypoint.type === 'content-script-style' ||
|
||||
entrypoint.type === 'unlisted-style'
|
||||
@@ -116,17 +118,16 @@ export async function createViteBuilder(
|
||||
plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig));
|
||||
}
|
||||
|
||||
const iifeReturnValueName = safeVarName(entrypoint.name);
|
||||
const libMode: vite.UserConfig = {
|
||||
if (
|
||||
entrypoint.type === 'content-script' ||
|
||||
entrypoint.type === 'unlisted-script'
|
||||
) {
|
||||
plugins.push(wxtPlugins.iifeFooter(iifeReturnValueName));
|
||||
}
|
||||
|
||||
return {
|
||||
mode: wxtConfig.mode,
|
||||
plugins,
|
||||
esbuild: {
|
||||
// Add a footer with the returned value so it can return values to `scripting.executeScript`
|
||||
// Footer is added a part 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,
|
||||
@@ -162,8 +163,7 @@ export async function createViteBuilder(
|
||||
// See https://github.com/aklinker1/vite-plugin-web-extension/issues/96
|
||||
'process.env.NODE_ENV': JSON.stringify(wxtConfig.mode),
|
||||
},
|
||||
};
|
||||
return libMode;
|
||||
} satisfies vite.UserConfig;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { Plugin } from 'vite';
|
||||
|
||||
/**
|
||||
* Add a footer with the returned value so it can return values to `scripting.executeScript`
|
||||
* Footer is added a part 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
|
||||
*/
|
||||
export function iifeFooter(iifeReturnValueName: string): Plugin {
|
||||
return {
|
||||
name: 'wxt:iife-footer',
|
||||
generateBundle(_, bundle) {
|
||||
for (const chunk of Object.values(bundle)) {
|
||||
if (chunk.type === 'chunk' && chunk.isEntry) {
|
||||
chunk.code += `${iifeReturnValueName};`;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -13,3 +13,4 @@ export * from './defineImportMeta';
|
||||
export * from './removeEntrypointMainFunction';
|
||||
export * from './wxtPluginLoader';
|
||||
export * from './resolveAppConfig';
|
||||
export * from './iifeFooter';
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
EntrypointGroup,
|
||||
ResolvedPublicFile,
|
||||
} from '../../../types';
|
||||
import { getPublicFiles } from '../../utils/fs';
|
||||
import { getPublicFiles } from '../fs';
|
||||
import fs from 'fs-extra';
|
||||
import { dirname, resolve } from 'path';
|
||||
import type { Ora } from 'ora';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/** @module wxt/utils/app-config */
|
||||
// @ts-expect-error: Untyped virtual module
|
||||
import appConfig from 'virtual:app-config';
|
||||
import type { WxtAppConfig } from '../utils/define-app-config';
|
||||
import type { WxtAppConfig } from './define-app-config';
|
||||
|
||||
export function useAppConfig(): WxtAppConfig {
|
||||
return appConfig;
|
||||
|
||||
Reference in New Issue
Block a user