Skip to content

Commit 87d5b07

Browse files
committed
fix
1 parent 0c21893 commit 87d5b07

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

packages/core/src/config.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,18 +1015,14 @@ const composeBundlelessExternalConfig = (
10151015
return cssExternal;
10161016
}
10171017

1018-
// Node.js ECMAScript module loader does no extension searching.
1019-
// Add a file extension according to autoExtension config
1020-
// when data.request is a relative path and do not have an extension.
1021-
// If data.request already have an extension, we replace it with new extension
1022-
// This may result in a change in semantics,
1023-
// user should use copy to keep origin file or use another separate entry to deal this
1024-
10251018
if (jsRedirectPath) {
10261019
try {
10271020
resolvedRequest = await resolver(context, resolvedRequest);
10281021
} catch (e) {
10291022
// Do nothing, fallthrough to other external matches.
1023+
logger.debug(
1024+
`Failed to resolve ${resolvedRequest} with resolver`,
1025+
);
10301026
}
10311027

10321028
resolvedRequest = normalizeSlash(
@@ -1041,6 +1037,12 @@ const composeBundlelessExternalConfig = (
10411037
}
10421038
}
10431039

1040+
// Node.js ECMAScript module loader does no extension searching.
1041+
// Add a file extension according to autoExtension config
1042+
// when data.request is a relative path and do not have an extension.
1043+
// If data.request already have an extension, we replace it with new extension
1044+
// This may result in a change in semantics,
1045+
// user should use copy to keep origin file or use another separate entry to deal this
10441046
if (jsRedirectExtension) {
10451047
const ext = extname(resolvedRequest);
10461048
if (ext) {
@@ -1054,7 +1056,6 @@ const composeBundlelessExternalConfig = (
10541056
return callback();
10551057
}
10561058
} else {
1057-
// TODO: add redirect.extension option
10581059
resolvedRequest = `${resolvedRequest}${jsExtension}`;
10591060
}
10601061
}
@@ -1106,17 +1107,15 @@ const composeDtsConfig = async (
11061107
};
11071108

11081109
const composeTargetConfig = (
1109-
target: RsbuildConfigOutputTarget,
1110+
userTarget: RsbuildConfigOutputTarget,
11101111
format: Format,
11111112
): {
11121113
config: RsbuildConfig;
1114+
externalsConfig: RsbuildConfig;
11131115
target: RsbuildConfigOutputTarget;
11141116
} => {
1115-
let defaultTarget = target;
1116-
if (!defaultTarget) {
1117-
defaultTarget = format === 'mf' ? 'web' : 'node';
1118-
}
1119-
switch (defaultTarget) {
1117+
const target = userTarget ?? (format === 'mf' ? 'web' : 'node');
1118+
switch (target) {
11201119
case 'web':
11211120
return {
11221121
config: {
@@ -1127,6 +1126,7 @@ const composeTargetConfig = (
11271126
},
11281127
},
11291128
target: 'web',
1129+
externalsConfig: {},
11301130
};
11311131
case 'node':
11321132
return {
@@ -1136,15 +1136,19 @@ const composeTargetConfig = (
11361136
target: ['node'],
11371137
},
11381138
},
1139+
output: {
1140+
target: 'node',
1141+
},
1142+
},
1143+
target: 'node',
1144+
externalsConfig: {
11391145
output: {
11401146
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
11411147
// Simply override the built-in modules to make them external.
11421148
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
11431149
externals: nodeBuiltInModules,
1144-
target: 'node',
11451150
},
11461151
},
1147-
target: 'node',
11481152
};
11491153
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
11501154
// case 'neutral':
@@ -1156,7 +1160,7 @@ const composeTargetConfig = (
11561160
// },
11571161
// };
11581162
default:
1159-
throw new Error(`Unsupported platform: ${defaultTarget}`);
1163+
throw new Error(`Unsupported platform: ${target}`);
11601164
}
11611165
};
11621166

@@ -1241,7 +1245,7 @@ async function composeLibRsbuildConfig(
12411245
externalHelpers,
12421246
pkgJson,
12431247
);
1244-
const userExternalConfig = composeExternalsConfig(
1248+
const userExternalsConfig = composeExternalsConfig(
12451249
format!,
12461250
config.output?.externals,
12471251
);
@@ -1256,10 +1260,11 @@ async function composeLibRsbuildConfig(
12561260
cssModulesAuto,
12571261
bundle,
12581262
);
1259-
const { config: targetConfig, target } = composeTargetConfig(
1260-
config.output?.target,
1261-
format!,
1262-
);
1263+
const {
1264+
config: targetConfig,
1265+
externalsConfig: targetExternalsConfig,
1266+
target,
1267+
} = composeTargetConfig(config.output?.target, format!);
12631268
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
12641269
const autoExternalConfig = composeAutoExternalConfig({
12651270
format: format!,
@@ -1283,7 +1288,7 @@ async function composeLibRsbuildConfig(
12831288
const externalsWarnConfig = composeExternalsWarnConfig(
12841289
format!,
12851290
autoExternalConfig?.output?.externals,
1286-
userExternalConfig?.output?.externals,
1291+
userExternalsConfig?.output?.externals,
12871292
);
12881293
const minifyConfig = composeMinifyConfig(config);
12891294
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
@@ -1298,17 +1303,20 @@ async function composeLibRsbuildConfig(
12981303
syntaxConfig,
12991304
externalHelpersConfig,
13001305
autoExtensionConfig,
1301-
1302-
// `externalsWarnConfig` should before other externals config.
1306+
targetConfig,
1307+
// #region Externals configs
1308+
// The order of the externals config should come in the following order:
1309+
// 1. `externalsWarnConfig` should come before other externals config to touch the externalized modules first.
1310+
// 2. The externals config in `bundlelessExternalConfig` should present after other externals config as
1311+
// it relies on other externals config to bail out the externalized modules first then resolve
1312+
// the correct path for relative imports.
1313+
// 3. `userExternalsConfig` should present later to override the externals config of the ahead ones.
13031314
externalsWarnConfig,
13041315
autoExternalConfig,
1305-
targetConfig,
1306-
// The externals config in `bundleConfig` should present after all externals config as
1307-
// it relies on other externals config to bail out the externalized modules first then resolve
1308-
// the correct path for relative imports.
1309-
userExternalConfig,
1316+
targetExternalsConfig,
1317+
userExternalsConfig,
13101318
bundlelessExternalConfig,
1311-
1319+
// #endregion
13121320
entryConfig,
13131321
cssConfig,
13141322
assetConfig,

packages/core/src/types/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@ export type Shims = {
7676

7777
export type JsRedirect = {
7878
/**
79-
* Whether to automatically redirect the import paths of JavaScript output files,
80-
* compilerOptions.paths in tsconfig.json will be applied by default.
79+
* Whether to automatically redirect the import paths of JavaScript output files.
8180
* @defaultValue `true`
8281
*/
8382
path?: boolean;
8483
/**
85-
* Whether to automatically add the file extension based on the JavaScript output files.
84+
* Whether to automatically add the file extension to import paths based on the JavaScript output files.
8685
* @defaultValue `true`
8786
*/
8887
extension?: boolean;
@@ -95,8 +94,9 @@ type DtsRedirect = {
9594
};
9695

9796
export type Redirect = {
98-
/** Controls the redirect of the import paths of JavaScript output files. */
97+
/** Controls the redirect of the import paths of output JavaScript files. */
9998
js?: JsRedirect;
99+
/** Whether to redirect the import path of the style file. */
100100
style?: boolean;
101101
// TODO: support other redirects
102102
// asset?: boolean;

0 commit comments

Comments
 (0)