@@ -437,17 +437,26 @@ export default async function getBaseWebpackConfig(
437437 }
438438 }
439439
440+ // RSC loaders, prefer ESM, set `esm` to true
440441 const swcServerLayerLoader = getSwcLoader ( {
441442 serverComponents : true ,
442443 isReactServerLayer : true ,
444+ esm : true ,
443445 } )
444446 const swcClientLayerLoader = getSwcLoader ( {
445447 serverComponents : true ,
446448 isReactServerLayer : false ,
449+ esm : true ,
450+ } )
451+ // Default swc loaders for pages doesn't prefer ESM.
452+ const swcDefaultLoader = getSwcLoader ( {
453+ serverComponents : true ,
454+ isReactServerLayer : false ,
455+ esm : false ,
447456 } )
448457
449458 const defaultLoaders = {
450- babel : useSWCLoader ? swcClientLayerLoader : babelLoader ! ,
459+ babel : useSWCLoader ? swcDefaultLoader : babelLoader ! ,
451460 }
452461
453462 const swcLoaderForServerLayer = hasAppDir
@@ -621,7 +630,7 @@ export default async function getBaseWebpackConfig(
621630 }
622631 : undefined ) ,
623632 // default main fields use pages dir ones, and customize app router ones in loaders.
624- mainFields : getMainField ( 'pages' , compilerType ) ,
633+ mainFields : getMainField ( compilerType , false ) ,
625634 ...( isEdgeServer && {
626635 conditionNames : edgeConditionNames ,
627636 } ) ,
@@ -736,8 +745,13 @@ export default async function getBaseWebpackConfig(
736745 const shouldIncludeExternalDirs =
737746 config . experimental . externalDir || ! ! config . transpilePackages
738747
739- function createLoaderRuleExclude ( skipNodeModules : boolean ) {
740- return ( excludePath : string ) => {
748+ const codeCondition = {
749+ test : / \. ( t s x | t s | j s | c j s | m j s | j s x ) $ / ,
750+ ...( shouldIncludeExternalDirs
751+ ? // Allowing importing TS/TSX files from outside of the root dir.
752+ { }
753+ : { include : [ dir , ...babelIncludeRegexes ] } ) ,
754+ exclude : ( excludePath : string ) => {
741755 if ( babelIncludeRegexes . some ( ( r ) => r . test ( excludePath ) ) ) {
742756 return false
743757 }
@@ -748,17 +762,8 @@ export default async function getBaseWebpackConfig(
748762 )
749763 if ( shouldBeBundled ) return false
750764
751- return skipNodeModules && excludePath . includes ( 'node_modules' )
752- }
753- }
754-
755- const codeCondition = {
756- test : / \. ( t s x | t s | j s | c j s | m j s | j s x ) $ / ,
757- ...( shouldIncludeExternalDirs
758- ? // Allowing importing TS/TSX files from outside of the root dir.
759- { }
760- : { include : [ dir , ...babelIncludeRegexes ] } ) ,
761- exclude : createLoaderRuleExclude ( true ) ,
765+ return excludePath . includes ( 'node_modules' )
766+ } ,
762767 }
763768
764769 let webpackConfig : webpack . Configuration = {
@@ -1281,7 +1286,7 @@ export default async function getBaseWebpackConfig(
12811286 ] ,
12821287 } ,
12831288 resolve : {
1284- mainFields : getMainField ( 'app' , compilerType ) ,
1289+ mainFields : getMainField ( compilerType , true ) ,
12851290 conditionNames : reactServerCondition ,
12861291 // If missing the alias override here, the default alias will be used which aliases
12871292 // react to the direct file path, not the package name. In that case the condition
@@ -1416,15 +1421,15 @@ export default async function getBaseWebpackConfig(
14161421 issuerLayer : [ WEBPACK_LAYERS . appPagesBrowser ] ,
14171422 use : swcLoaderForClientLayer ,
14181423 resolve : {
1419- mainFields : getMainField ( 'app' , compilerType ) ,
1424+ mainFields : getMainField ( compilerType , true ) ,
14201425 } ,
14211426 } ,
14221427 {
14231428 test : codeCondition . test ,
14241429 issuerLayer : [ WEBPACK_LAYERS . serverSideRendering ] ,
14251430 use : swcLoaderForClientLayer ,
14261431 resolve : {
1427- mainFields : getMainField ( 'app' , compilerType ) ,
1432+ mainFields : getMainField ( compilerType , true ) ,
14281433 } ,
14291434 } ,
14301435 ]
0 commit comments