@@ -1014,18 +1014,14 @@ const composeBundlelessExternalConfig = (
1014
1014
return cssExternal ;
1015
1015
}
1016
1016
1017
- // Node.js ECMAScript module loader does no extension searching.
1018
- // Add a file extension according to autoExtension config
1019
- // when data.request is a relative path and do not have an extension.
1020
- // If data.request already have an extension, we replace it with new extension
1021
- // This may result in a change in semantics,
1022
- // user should use copy to keep origin file or use another separate entry to deal this
1023
-
1024
1017
if ( jsRedirectPath ) {
1025
1018
try {
1026
1019
resolvedRequest = await resolver ( context , resolvedRequest ) ;
1027
1020
} catch ( e ) {
1028
1021
// Do nothing, fallthrough to other external matches.
1022
+ logger . debug (
1023
+ `Failed to resolve ${ resolvedRequest } with resolver` ,
1024
+ ) ;
1029
1025
}
1030
1026
1031
1027
resolvedRequest = normalizeSlash (
@@ -1040,6 +1036,12 @@ const composeBundlelessExternalConfig = (
1040
1036
}
1041
1037
}
1042
1038
1039
+ // Node.js ECMAScript module loader does no extension searching.
1040
+ // Add a file extension according to autoExtension config
1041
+ // when data.request is a relative path and do not have an extension.
1042
+ // If data.request already have an extension, we replace it with new extension
1043
+ // This may result in a change in semantics,
1044
+ // user should use copy to keep origin file or use another separate entry to deal this
1043
1045
if ( jsRedirectExtension ) {
1044
1046
const ext = extname ( resolvedRequest ) ;
1045
1047
if ( ext ) {
@@ -1053,7 +1055,6 @@ const composeBundlelessExternalConfig = (
1053
1055
return callback ( ) ;
1054
1056
}
1055
1057
} else {
1056
- // TODO: add redirect.extension option
1057
1058
resolvedRequest = `${ resolvedRequest } ${ jsExtension } ` ;
1058
1059
}
1059
1060
}
@@ -1105,17 +1106,15 @@ const composeDtsConfig = async (
1105
1106
} ;
1106
1107
1107
1108
const composeTargetConfig = (
1108
- target : RsbuildConfigOutputTarget ,
1109
+ userTarget : RsbuildConfigOutputTarget ,
1109
1110
format : Format ,
1110
1111
) : {
1111
1112
config : RsbuildConfig ;
1113
+ externalsConfig : RsbuildConfig ;
1112
1114
target : RsbuildConfigOutputTarget ;
1113
1115
} => {
1114
- let defaultTarget = target ;
1115
- if ( ! defaultTarget ) {
1116
- defaultTarget = format === 'mf' ? 'web' : 'node' ;
1117
- }
1118
- switch ( defaultTarget ) {
1116
+ const target = userTarget ?? ( format === 'mf' ? 'web' : 'node' ) ;
1117
+ switch ( target ) {
1119
1118
case 'web' :
1120
1119
return {
1121
1120
config : {
@@ -1126,6 +1125,7 @@ const composeTargetConfig = (
1126
1125
} ,
1127
1126
} ,
1128
1127
target : 'web' ,
1128
+ externalsConfig : { } ,
1129
1129
} ;
1130
1130
case 'node' :
1131
1131
return {
@@ -1135,15 +1135,19 @@ const composeTargetConfig = (
1135
1135
target : [ 'node' ] ,
1136
1136
} ,
1137
1137
} ,
1138
+ output : {
1139
+ target : 'node' ,
1140
+ } ,
1141
+ } ,
1142
+ target : 'node' ,
1143
+ externalsConfig : {
1138
1144
output : {
1139
1145
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
1140
1146
// Simply override the built-in modules to make them external.
1141
1147
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
1142
1148
externals : nodeBuiltInModules ,
1143
- target : 'node' ,
1144
1149
} ,
1145
1150
} ,
1146
- target : 'node' ,
1147
1151
} ;
1148
1152
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
1149
1153
// case 'neutral':
@@ -1155,7 +1159,7 @@ const composeTargetConfig = (
1155
1159
// },
1156
1160
// };
1157
1161
default :
1158
- throw new Error ( `Unsupported platform: ${ defaultTarget } ` ) ;
1162
+ throw new Error ( `Unsupported platform: ${ target } ` ) ;
1159
1163
}
1160
1164
} ;
1161
1165
@@ -1240,7 +1244,7 @@ async function composeLibRsbuildConfig(
1240
1244
externalHelpers ,
1241
1245
pkgJson ,
1242
1246
) ;
1243
- const userExternalConfig = composeExternalsConfig (
1247
+ const userExternalsConfig = composeExternalsConfig (
1244
1248
format ! ,
1245
1249
config . output ?. externals ,
1246
1250
) ;
@@ -1255,10 +1259,11 @@ async function composeLibRsbuildConfig(
1255
1259
cssModulesAuto ,
1256
1260
bundle ,
1257
1261
) ;
1258
- const { config : targetConfig , target } = composeTargetConfig (
1259
- config . output ?. target ,
1260
- format ! ,
1261
- ) ;
1262
+ const {
1263
+ config : targetConfig ,
1264
+ externalsConfig : targetExternalsConfig ,
1265
+ target,
1266
+ } = composeTargetConfig ( config . output ?. target , format ! ) ;
1262
1267
const syntaxConfig = composeSyntaxConfig ( target , config ?. syntax ) ;
1263
1268
const autoExternalConfig = composeAutoExternalConfig ( {
1264
1269
format : format ! ,
@@ -1280,7 +1285,7 @@ async function composeLibRsbuildConfig(
1280
1285
const externalsWarnConfig = composeExternalsWarnConfig (
1281
1286
format ! ,
1282
1287
autoExternalConfig ?. output ?. externals ,
1283
- userExternalConfig ?. output ?. externals ,
1288
+ userExternalsConfig ?. output ?. externals ,
1284
1289
) ;
1285
1290
const minifyConfig = composeMinifyConfig ( config ) ;
1286
1291
const bannerFooterConfig = composeBannerFooterConfig ( banner , footer ) ;
@@ -1295,17 +1300,20 @@ async function composeLibRsbuildConfig(
1295
1300
syntaxConfig ,
1296
1301
externalHelpersConfig ,
1297
1302
autoExtensionConfig ,
1298
-
1299
- // `externalsWarnConfig` should before other externals config.
1303
+ targetConfig ,
1304
+ // #region Externals configs
1305
+ // The order of the externals config should come in the following order:
1306
+ // 1. `externalsWarnConfig` should come before other externals config to touch the externalized modules first.
1307
+ // 2. The externals config in `bundlelessExternalConfig` should present after other externals config as
1308
+ // it relies on other externals config to bail out the externalized modules first then resolve
1309
+ // the correct path for relative imports.
1310
+ // 3. `userExternalsConfig` should present later to override the externals config of the ahead ones.
1300
1311
externalsWarnConfig ,
1301
1312
autoExternalConfig ,
1302
- targetConfig ,
1303
- // The externals config in `bundleConfig` should present after all externals config as
1304
- // it relies on other externals config to bail out the externalized modules first then resolve
1305
- // the correct path for relative imports.
1306
- userExternalConfig ,
1313
+ targetExternalsConfig ,
1314
+ userExternalsConfig ,
1307
1315
bundlelessExternalConfig ,
1308
-
1316
+ // #endregion
1309
1317
entryConfig ,
1310
1318
cssConfig ,
1311
1319
entryChunkConfig ,
0 commit comments