@@ -18,7 +18,7 @@ type ResolveFunction = (
1818 string ,
1919 ResolveContext ,
2020 ResolveFunction ,
21- ) => Promise < { url : string } > ;
21+ ) => { url : string } | Promise < { url : string } > ;
2222
2323type GetSourceContext = {
2424 format : string ,
@@ -70,19 +70,24 @@ export async function resolve(
7070 ) ;
7171 }
7272 }
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+ }
7883 throw new Error (
79- `Cannot import " ${ specifier } " from "${ context . parentURL } ". ` +
84+ `Cannot import ${ reason } from "${ parentURL } ". ` +
8085 'By react-server convention, .server.js files can only be imported from other .server.js files. ' +
8186 'That way nobody accidentally sends these to the client by indirectly importing it.' ,
8287 ) ;
8388 }
8489 }
85- return defaultResolve ( specifier , context , defaultResolve ) ;
90+ return resolved ;
8691}
8792
8893export async function getSource (
@@ -128,7 +133,7 @@ function addExportNames(names, node) {
128133function resolveClientImport (
129134 specifier : string ,
130135 parentURL : string ,
131- ) : Promise < { url: string } > {
136+ ) : { url: string } | Promise < { url : string } > {
132137 // Resolve an import specifier as if it was loaded by the client. This doesn't use
133138 // the overrides that this loader does but instead reverts to the default.
134139 // This resolution algorithm will not necessarily have the same configuration
0 commit comments