1
1
import type { Plugin } from 'vite' ;
2
2
3
3
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' ;
5
5
6
6
const stripExt = ( fname : string ) => {
7
7
const ext = extname ( fname ) ;
8
8
return ext ? fname . slice ( 0 , - ext . length ) : fname ;
9
9
} ;
10
10
11
- const getManagedEntries = ( ) => `
11
+ const getManagedEntries = ( filePath : string , srcDir : string ) => `
12
12
import { fsRouter } from 'waku/router/server';
13
13
14
14
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 ) =>
17
17
ext . replace ( / ^ \. / , '' ) ,
18
- ) . join ( ',' ) } }')[\`. /pages/\${file}\`]?.(),
18
+ ) . join ( ',' ) } }')[\`/ ${ srcDir } /pages/\${file}\`]?.(),
19
19
);
20
20
` ;
21
21
22
22
const getManagedMain = ( ) => `
23
- import { StrictMode } from 'react';
23
+ import { StrictMode, createElement } from 'react';
24
24
import { createRoot, hydrateRoot } from 'react-dom/client';
25
25
import { Router } from 'waku/router/client';
26
26
27
- const rootElement = (
28
- <StrictMode>
29
- <Router />
30
- </StrictMode>
31
- );
27
+ const rootElement = createElement(StrictMode, null, createElement(Router));
32
28
33
29
if (globalThis.__WAKU_HYDRATE__) {
34
30
hydrateRoot(document, rootElement);
@@ -46,8 +42,6 @@ export function rscManagedPlugin(opts: {
46
42
let entriesFile : string | undefined ;
47
43
let mainFile : string | undefined ;
48
44
const mainPath = `${ opts . basePath } ${ opts . srcDir } /${ SRC_MAIN } ` ;
49
- let managedEntries = false ;
50
- let managedMain = false ;
51
45
return {
52
46
name : 'rsc-managed-plugin' ,
53
47
enforce : 'pre' ,
@@ -73,28 +67,23 @@ export function rscManagedPlugin(opts: {
73
67
} ,
74
68
async resolveId ( id , importer , options ) {
75
69
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
+ }
87
80
}
88
- return resolved ;
89
81
} ,
90
82
load ( id ) {
91
- if ( managedEntries && id === entriesFile + '.jsx ' ) {
92
- return getManagedEntries ( ) ;
83
+ if ( id === '\0' + entriesFile + '.js ' ) {
84
+ return getManagedEntries ( entriesFile + '.js' , opts . srcDir ) ;
93
85
}
94
- if (
95
- managedMain &&
96
- ( id === mainFile + '.jsx' || id === mainPath + '.jsx' )
97
- ) {
86
+ if ( id === '\0' + mainFile + '.js' || id === '\0' + mainPath + '.js' ) {
98
87
return getManagedMain ( ) ;
99
88
}
100
89
} ,
0 commit comments