Skip to content

Commit f80f09b

Browse files
authoredMar 6, 2025
refactor: vite-loader util (#1294)
split from #1281.
1 parent c57a40e commit f80f09b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

‎packages/waku/src/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ async function loadConfig(): Promise<Config> {
220220
if (!existsSync(CONFIG_FILE)) {
221221
return {};
222222
}
223-
const { loadServerFile } = await import('./lib/utils/vite-loader.js');
223+
const { loadServerModule } = await import('./lib/utils/vite-loader.js');
224224
const file = pathToFileURL(path.resolve(CONFIG_FILE)).toString();
225-
return (await loadServerFile(file)).default;
225+
return (await loadServerModule<{ default: Config }>(file)).default;
226226
}

‎packages/waku/src/lib/utils/vite-loader.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import type { RunnableDevEnvironment } from 'vite';
33

44
import { fileURLToFilePath } from '../utils/path.js';
55

6-
export const loadServerFile = async (fileURL: string) => {
6+
export const loadServerModule = async <T>(idOrFileURL: string): Promise<T> => {
7+
if (idOrFileURL === 'waku' || idOrFileURL.startsWith('waku/')) {
8+
// HACK `external: ['waku']` doesn't do the same
9+
return import(idOrFileURL) as T;
10+
}
711
const vite = await createViteServer({
812
server: { middlewareMode: true, watch: null },
913
appType: 'custom',
@@ -21,6 +25,10 @@ export const loadServerFile = async (fileURL: string) => {
2125
);
2226
const mod = await (
2327
vite.environments.config as RunnableDevEnvironment
24-
).runner.import(fileURLToFilePath(fileURL));
25-
return mod;
28+
).runner.import(
29+
idOrFileURL.startsWith('file://')
30+
? fileURLToFilePath(idOrFileURL)
31+
: idOrFileURL,
32+
);
33+
return mod as T;
2634
};

0 commit comments

Comments
 (0)