@@ -18,7 +18,7 @@ type ResolveFunction = (
18
18
string ,
19
19
ResolveContext ,
20
20
ResolveFunction ,
21
- ) => Promise < { url : string } > ;
21
+ ) => { url : string } | Promise < { url : string } > ;
22
22
23
23
type GetSourceContext = {
24
24
format : string ,
@@ -70,19 +70,24 @@ export async function resolve(
70
70
) ;
71
71
}
72
72
}
73
- // We intentionally check the specifier here instead of the resolved file.
74
- // This allows package exports to configure non-server aliases that resolve to server files
75
- // depending on environment. It's probably a bad idea to export a server file as "main" though.
76
- if ( specifier . endsWith ( '.server.js' ) ) {
77
- if ( context . parentURL && ! context . parentURL . endsWith ( '.server.js' ) ) {
73
+ const resolved = await defaultResolve ( specifier , context , defaultResolve ) ;
74
+ if ( resolved . url . endsWith ( '.server.js' ) ) {
75
+ const parentURL = context . parentURL ;
76
+ if ( parentURL && ! parentURL . endsWith ( '.server.js' ) ) {
77
+ let reason ;
78
+ if ( specifier . endsWith ( '.server.js' ) ) {
79
+ reason = `"${ specifier } "` ;
80
+ } else {
81
+ reason = `"${ specifier } " (which expands to "${ resolved . url } ")` ;
82
+ }
78
83
throw new Error (
79
- `Cannot import " ${ specifier } " from "${ context . parentURL } ". ` +
84
+ `Cannot import ${ reason } from "${ parentURL } ". ` +
80
85
'By react-server convention, .server.js files can only be imported from other .server.js files. ' +
81
86
'That way nobody accidentally sends these to the client by indirectly importing it.' ,
82
87
) ;
83
88
}
84
89
}
85
- return defaultResolve ( specifier , context , defaultResolve ) ;
90
+ return resolved ;
86
91
}
87
92
88
93
export async function getSource (
@@ -128,7 +133,7 @@ function addExportNames(names, node) {
128
133
function resolveClientImport (
129
134
specifier : string ,
130
135
parentURL : string ,
131
- ) : Promise < { url: string } > {
136
+ ) : { url: string } | Promise < { url : string } > {
132
137
// Resolve an import specifier as if it was loaded by the client. This doesn't use
133
138
// the overrides that this loader does but instead reverts to the default.
134
139
// This resolution algorithm will not necessarily have the same configuration
0 commit comments