Skip to content

Commit f137b01

Browse files
committed
refactor: move to module runner
Related: #1214. Related: #1044.
1 parent 944e2c5 commit f137b01

File tree

4 files changed

+23
-51
lines changed

4 files changed

+23
-51
lines changed

packages/wxt/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@
5858
"publish-browser-extension": "^2.3.0 || ^3.0.2",
5959
"scule": "^1.3.0",
6060
"unimport": "^3.13.1 || ^4.0.0 || ^5.0.0",
61-
"vite": "^5.4.19 || ^6.3.4 || ^7.0.0",
62-
"vite-node": "^2.1.4 || ^3.1.2",
61+
"vite": "^6.3.4 || ^7.0.0",
6362
"web-ext-run": "^0.2.4"
6463
},
6564
"peerDependencies": {
66-
"vite": "^5.4.19 || ^6.3.4 || ^7.0.0"
65+
"vite": "^6.3.4 || ^7.0.0"
6766
},
6867
"devDependencies": {
6968
"@aklinker1/check": "^2.1.0",

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

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import {
2121
import { Hookable } from 'hookable';
2222
import { toArray } from '../../utils/arrays';
2323
import { safeVarName } from '../../utils/strings';
24-
import { ViteNodeServer } from 'vite-node/server';
25-
import { ViteNodeRunner } from 'vite-node/client';
26-
import { installSourcemapsSupport } from 'vite-node/source-map';
2724
import { createExtensionEnvironment } from '../../utils/environments';
2825
import { dirname, extname, join, relative } from 'node:path';
2926
import fs from 'fs-extra';
@@ -71,7 +68,6 @@ export async function createViteBuilder(
7168

7269
// TODO: Remove once https://github.com/wxt-dev/wxt/pull/1411 is merged
7370
config.legacy ??= {};
74-
// @ts-ignore: Untyped option:
7571
config.legacy.skipWebSocketTokenCheck = true;
7672

7773
const server = getWxtDevServer?.();
@@ -91,7 +87,7 @@ export async function createViteBuilder(
9187
);
9288
if (
9389
wxtConfig.analysis.enabled &&
94-
// If included, vite-node entrypoint loader will increment the
90+
// If included, entrypoint loader will increment the
9591
// bundleAnalysis's internal build index tracker, which we don't want
9692
!baseConfigOptions?.excludeAnalysisPlugin
9793
) {
@@ -224,8 +220,7 @@ export async function createViteBuilder(
224220
},
225221
};
226222
};
227-
228-
const createViteNodeImporter = async (paths: string[]) => {
223+
const createImporterConfig = async (paths: string[]) => {
229224
const baseConfig = await getBaseConfig({
230225
excludeAnalysisPlugin: true,
231226
});
@@ -238,30 +233,7 @@ export async function createViteBuilder(
238233
wxtPlugins.removeEntrypointMainFunction(wxtConfig, path),
239234
),
240235
};
241-
const config = vite.mergeConfig(baseConfig, envConfig);
242-
const server = await vite.createServer(config);
243-
await server.pluginContainer.buildStart({});
244-
const node = new ViteNodeServer(
245-
// @ts-ignore: Some weird type error...
246-
server,
247-
);
248-
installSourcemapsSupport({
249-
getSourceMap: (source) => node.getSourceMap(source),
250-
});
251-
const runner = new ViteNodeRunner({
252-
root: server.config.root,
253-
base: server.config.base,
254-
// when having the server and runner in a different context,
255-
// you will need to handle the communication between them
256-
// and pass to this function
257-
fetchModule(id) {
258-
return node.fetchModule(id);
259-
},
260-
resolveId(id, importer) {
261-
return node.resolveId(id, importer);
262-
},
263-
});
264-
return { runner, server };
236+
return vite.mergeConfig(baseConfig, envConfig);
265237
};
266238

267239
const requireDefaultExport = (path: string, mod: any) => {
@@ -284,26 +256,30 @@ export async function createViteBuilder(
284256
version: vite.version,
285257
async importEntrypoint(path) {
286258
const env = createExtensionEnvironment();
287-
const { runner, server } = await createViteNodeImporter([path]);
288-
const res = await env.run(() => runner.executeFile(path));
289-
await server.close();
290-
requireDefaultExport(path, res);
291-
return res.default;
259+
const importerConfig = await createImporterConfig([path]);
260+
261+
const { module } = await env.run(() =>
262+
vite.runnerImport<{ default: any }>(path, importerConfig),
263+
);
264+
requireDefaultExport(path, module);
265+
return module.default;
292266
},
293267
async importEntrypoints(paths) {
268+
const importerConfig = await createImporterConfig(paths);
269+
294270
const env = createExtensionEnvironment();
295-
const { runner, server } = await createViteNodeImporter(paths);
296-
const res = await env.run(() =>
271+
return await env.run(() =>
297272
Promise.all(
298273
paths.map(async (path) => {
299-
const mod = await runner.executeFile(path);
300-
requireDefaultExport(path, mod);
301-
return mod.default;
274+
const { module } = await vite.runnerImport<{ default: any }>(
275+
path,
276+
importerConfig,
277+
);
278+
requireDefaultExport(path, module);
279+
return module.default;
302280
}),
303281
),
304282
);
305-
await server.close();
306-
return res;
307283
},
308284
async build(group) {
309285
let entryConfig;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function removeEntrypointMainFunction(
1919
handler(code, id) {
2020
if (id === absPath) {
2121
const newCode = removeMainFunctionCode(code);
22-
config.logger.debug('vite-node transformed entrypoint', path);
22+
config.logger.debug('transformed entrypoint', path);
2323
config.logger.debug(`Original:\n---\n${code}\n---`);
2424
config.logger.debug(`Transformed:\n---\n${newCode.code}\n---`);
2525
return newCode;

pnpm-lock.yaml

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)