Skip to content

Commit 34e6f46

Browse files
committed
fix
1 parent 3bea21b commit 34e6f46

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
@@ -1014,18 +1014,14 @@ const composeBundlelessExternalConfig = (
10141014
return cssExternal;
10151015
}
10161016

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-
10241017
if (jsRedirectPath) {
10251018
try {
10261019
resolvedRequest = await resolver(context, resolvedRequest);
10271020
} catch (e) {
10281021
// Do nothing, fallthrough to other external matches.
1022+
logger.debug(
1023+
`Failed to resolve ${resolvedRequest} with resolver`,
1024+
);
10291025
}
10301026

10311027
resolvedRequest = normalizeSlash(
@@ -1040,6 +1036,12 @@ const composeBundlelessExternalConfig = (
10401036
}
10411037
}
10421038

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
10431045
if (jsRedirectExtension) {
10441046
const ext = extname(resolvedRequest);
10451047
if (ext) {
@@ -1053,7 +1055,6 @@ const composeBundlelessExternalConfig = (
10531055
return callback();
10541056
}
10551057
} else {
1056-
// TODO: add redirect.extension option
10571058
resolvedRequest = `${resolvedRequest}${jsExtension}`;
10581059
}
10591060
}
@@ -1105,17 +1106,15 @@ const composeDtsConfig = async (
11051106
};
11061107

11071108
const composeTargetConfig = (
1108-
target: RsbuildConfigOutputTarget,
1109+
userTarget: RsbuildConfigOutputTarget,
11091110
format: Format,
11101111
): {
11111112
config: RsbuildConfig;
1113+
externalsConfig: RsbuildConfig;
11121114
target: RsbuildConfigOutputTarget;
11131115
} => {
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) {
11191118
case 'web':
11201119
return {
11211120
config: {
@@ -1126,6 +1125,7 @@ const composeTargetConfig = (
11261125
},
11271126
},
11281127
target: 'web',
1128+
externalsConfig: {},
11291129
};
11301130
case 'node':
11311131
return {
@@ -1135,15 +1135,19 @@ const composeTargetConfig = (
11351135
target: ['node'],
11361136
},
11371137
},
1138+
output: {
1139+
target: 'node',
1140+
},
1141+
},
1142+
target: 'node',
1143+
externalsConfig: {
11381144
output: {
11391145
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
11401146
// Simply override the built-in modules to make them external.
11411147
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
11421148
externals: nodeBuiltInModules,
1143-
target: 'node',
11441149
},
11451150
},
1146-
target: 'node',
11471151
};
11481152
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
11491153
// case 'neutral':
@@ -1155,7 +1159,7 @@ const composeTargetConfig = (
11551159
// },
11561160
// };
11571161
default:
1158-
throw new Error(`Unsupported platform: ${defaultTarget}`);
1162+
throw new Error(`Unsupported platform: ${target}`);
11591163
}
11601164
};
11611165

@@ -1240,7 +1244,7 @@ async function composeLibRsbuildConfig(
12401244
externalHelpers,
12411245
pkgJson,
12421246
);
1243-
const userExternalConfig = composeExternalsConfig(
1247+
const userExternalsConfig = composeExternalsConfig(
12441248
format!,
12451249
config.output?.externals,
12461250
);
@@ -1255,10 +1259,11 @@ async function composeLibRsbuildConfig(
12551259
cssModulesAuto,
12561260
bundle,
12571261
);
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!);
12621267
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
12631268
const autoExternalConfig = composeAutoExternalConfig({
12641269
format: format!,
@@ -1280,7 +1285,7 @@ async function composeLibRsbuildConfig(
12801285
const externalsWarnConfig = composeExternalsWarnConfig(
12811286
format!,
12821287
autoExternalConfig?.output?.externals,
1283-
userExternalConfig?.output?.externals,
1288+
userExternalsConfig?.output?.externals,
12841289
);
12851290
const minifyConfig = composeMinifyConfig(config);
12861291
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
@@ -1295,17 +1300,20 @@ async function composeLibRsbuildConfig(
12951300
syntaxConfig,
12961301
externalHelpersConfig,
12971302
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.
13001311
externalsWarnConfig,
13011312
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,
13071315
bundlelessExternalConfig,
1308-
1316+
// #endregion
13091317
entryConfig,
13101318
cssConfig,
13111319
entryChunkConfig,

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)