Skip to content

Commit 5b97062

Browse files
authored
fix(plugins): fix rscManagedPlugin (#1019)
follows `\0` technique described in https://vite.dev/guide/api-plugin#virtual-modules-convention related: vitejs/vite#18223
1 parent 832a67b commit 5b97062

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

packages/waku/src/lib/plugins/vite-plugin-rsc-entries.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ globalThis.__WAKU_CLIENT_IMPORT__ = (id) => loadModule('${opts.ssrDir}/' + id);
4141
configResolved(config) {
4242
entriesFile = joinPath(config.root, opts.srcDir, SRC_ENTRIES);
4343
if (existsSync(CONFIG_FILE)) {
44-
const file = normalizePath(
45-
path.relative(path.dirname(entriesFile), path.resolve(CONFIG_FILE)),
46-
);
44+
const file = normalizePath(path.resolve(CONFIG_FILE));
4745
codeToAppend += `
4846
export const loadConfig = async () => (await import('${file}')).default;
4947
`;
@@ -60,7 +58,7 @@ export const loadConfig = async () => ({});
6058
) {
6159
return codeToPrepend + code;
6260
}
63-
if (stripExt(id) === entriesFile) {
61+
if (stripExt(id).endsWith(entriesFile)) {
6462
return code + codeToAppend;
6563
}
6664
},

packages/waku/src/lib/plugins/vite-plugin-rsc-managed.ts

+20-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
import type { Plugin } from 'vite';
22

33
import { EXTENSIONS, SRC_MAIN, SRC_ENTRIES } from '../constants.js';
4-
import { extname, joinPath } from '../utils/path.js';
4+
import { extname, joinPath, filePathToFileURL } from '../utils/path.js';
55

66
const stripExt = (fname: string) => {
77
const ext = extname(fname);
88
return ext ? fname.slice(0, -ext.length) : fname;
99
};
1010

11-
const getManagedEntries = () => `
11+
const getManagedEntries = (filePath: string, srcDir: string) => `
1212
import { fsRouter } from 'waku/router/server';
1313
1414
export default fsRouter(
15-
import.meta.url,
16-
(file) => import.meta.glob('./pages/**/*.{${EXTENSIONS.map((ext) =>
15+
'${filePathToFileURL(filePath)}',
16+
(file) => import.meta.glob('/${srcDir}/pages/**/*.{${EXTENSIONS.map((ext) =>
1717
ext.replace(/^\./, ''),
18-
).join(',')}}')[\`./pages/\${file}\`]?.(),
18+
).join(',')}}')[\`/${srcDir}/pages/\${file}\`]?.(),
1919
);
2020
`;
2121

2222
const getManagedMain = () => `
23-
import { StrictMode } from 'react';
23+
import { StrictMode, createElement } from 'react';
2424
import { createRoot, hydrateRoot } from 'react-dom/client';
2525
import { Router } from 'waku/router/client';
2626
27-
const rootElement = (
28-
<StrictMode>
29-
<Router />
30-
</StrictMode>
31-
);
27+
const rootElement = createElement(StrictMode, null, createElement(Router));
3228
3329
if (globalThis.__WAKU_HYDRATE__) {
3430
hydrateRoot(document, rootElement);
@@ -46,8 +42,6 @@ export function rscManagedPlugin(opts: {
4642
let entriesFile: string | undefined;
4743
let mainFile: string | undefined;
4844
const mainPath = `${opts.basePath}${opts.srcDir}/${SRC_MAIN}`;
49-
let managedEntries = false;
50-
let managedMain = false;
5145
return {
5246
name: 'rsc-managed-plugin',
5347
enforce: 'pre',
@@ -73,28 +67,23 @@ export function rscManagedPlugin(opts: {
7367
},
7468
async resolveId(id, importer, options) {
7569
const resolved = await this.resolve(id, importer, options);
76-
if ((!resolved || resolved.id === id) && id === entriesFile) {
77-
managedEntries = true;
78-
return entriesFile + '.jsx';
79-
}
80-
if ((!resolved || resolved.id === id) && id === mainFile) {
81-
managedMain = true;
82-
return mainFile + '.jsx';
83-
}
84-
if ((!resolved || resolved.id === id) && stripExt(id) === mainPath) {
85-
managedMain = true;
86-
return mainPath + '.jsx';
70+
if (!resolved || resolved.id === id) {
71+
if (id === entriesFile) {
72+
return '\0' + entriesFile + '.js';
73+
}
74+
if (id === mainFile) {
75+
return '\0' + mainFile + '.js';
76+
}
77+
if (stripExt(id) === mainPath) {
78+
return '\0' + mainPath + '.js';
79+
}
8780
}
88-
return resolved;
8981
},
9082
load(id) {
91-
if (managedEntries && id === entriesFile + '.jsx') {
92-
return getManagedEntries();
83+
if (id === '\0' + entriesFile + '.js') {
84+
return getManagedEntries(entriesFile + '.js', opts.srcDir);
9385
}
94-
if (
95-
managedMain &&
96-
(id === mainFile + '.jsx' || id === mainPath + '.jsx')
97-
) {
86+
if (id === '\0' + mainFile + '.js' || id === '\0' + mainPath + '.js') {
9887
return getManagedMain();
9988
}
10089
},

0 commit comments

Comments
 (0)