Skip to content

Commit 7dd9a48

Browse files
authored
feat!: target default to node (#398)
1 parent b821370 commit 7dd9a48

File tree

83 files changed

+366
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+366
-129
lines changed

examples/express-plugin/rslib.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@ export default defineConfig({
2727
},
2828
},
2929
],
30-
output: {
31-
target: 'node',
32-
},
3330
});

examples/module-federation/mf-react-component/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ export default defineConfig({
5555
],
5656
},
5757
],
58+
output: {
59+
target: 'web',
60+
},
5861
plugins: [pluginReact()],
5962
});

examples/react-component-bundle-false/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export default defineConfig({
3535
},
3636
},
3737
],
38+
output: {
39+
target: 'web',
40+
},
3841
plugins: [
3942
pluginReact({
4043
swcReactOptions: {

examples/react-component-bundle/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default defineConfig({
3333
},
3434
},
3535
],
36+
output: {
37+
target: 'web',
38+
},
3639
plugins: [
3740
pluginReact({
3841
swcReactOptions: {

examples/react-component-umd/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default defineConfig({
1717
},
1818
},
1919
],
20+
output: {
21+
target: 'web',
22+
},
2023
plugins: [
2124
pluginReact({
2225
swcReactOptions: {

packages/core/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default defineConfig({
2121
},
2222
},
2323
output: {
24+
// TODO: Remove this after bumping Rslib
2425
target: 'node',
2526
externals: {
2627
picocolors: '../compiled/picocolors/index.js',

packages/core/src/config.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ export async function createConstantRsbuildConfig(): Promise<RsbuildConfig> {
453453
},
454454
},
455455
output: {
456+
target: 'node',
456457
filenameHash: false,
457458
distPath: {
458459
js: './',
@@ -558,7 +559,6 @@ const composeFormatConfig = ({
558559
},
559560
output: {
560561
asyncChunks: false,
561-
562562
library: umdName
563563
? {
564564
type: 'umd',
@@ -713,8 +713,8 @@ const composeAutoExtensionConfig = (
713713
};
714714

715715
const composeSyntaxConfig = (
716+
target: RsbuildConfigOutputTarget,
716717
syntax?: Syntax,
717-
target?: RsbuildConfigOutputTarget,
718718
): RsbuildConfig => {
719719
// Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
720720
if (syntax) {
@@ -941,31 +941,40 @@ const composeDtsConfig = async (
941941
};
942942

943943
const composeTargetConfig = (
944-
target: RsbuildConfigOutputTarget = 'web',
945-
): RsbuildConfig => {
944+
target: RsbuildConfigOutputTarget = 'node',
945+
): {
946+
config: RsbuildConfig;
947+
target: RsbuildConfigOutputTarget;
948+
} => {
946949
switch (target) {
947950
case 'web':
948951
return {
949-
tools: {
950-
rspack: {
951-
target: ['web'],
952+
config: {
953+
tools: {
954+
rspack: {
955+
target: ['web'],
956+
},
952957
},
953958
},
959+
target: 'web',
954960
};
955961
case 'node':
956962
return {
957-
tools: {
958-
rspack: {
959-
target: ['node'],
963+
config: {
964+
tools: {
965+
rspack: {
966+
target: ['node'],
967+
},
968+
},
969+
output: {
970+
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
971+
// Simply override the built-in modules to make them external.
972+
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
973+
externals: nodeBuiltInModules,
974+
target: 'node',
960975
},
961976
},
962-
output: {
963-
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
964-
// Simply override the built-in modules to make them external.
965-
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
966-
externals: nodeBuiltInModules,
967-
target: 'node',
968-
},
977+
target: 'node',
969978
};
970979
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
971980
// case 'neutral':
@@ -1067,11 +1076,10 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
10671076
cssModulesAuto,
10681077
bundle,
10691078
);
1070-
const targetConfig = composeTargetConfig(config.output?.target);
1071-
const syntaxConfig = composeSyntaxConfig(
1072-
config?.syntax,
1079+
const { config: targetConfig, target } = composeTargetConfig(
10731080
config.output?.target,
10741081
);
1082+
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
10751083
const autoExternalConfig = composeAutoExternalConfig({
10761084
autoExternal,
10771085
pkgJson,

packages/core/src/utils/syntax.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export const LATEST_TARGET_VERSIONS: Record<
2626
};
2727

2828
const calcEsnextBrowserslistByTarget = (target: RsbuildConfigOutputTarget) => {
29-
if (!target) {
30-
return [...LATEST_TARGET_VERSIONS.node, ...LATEST_TARGET_VERSIONS.web];
31-
}
32-
3329
if (target === 'node') {
3430
return LATEST_TARGET_VERSIONS.node;
3531
}
@@ -195,7 +191,7 @@ export function transformSyntaxToRspackTarget(
195191

196192
export function transformSyntaxToBrowserslist(
197193
syntax: Syntax,
198-
target?: NonNullable<RsbuildConfig['output']>['target'],
194+
target: NonNullable<RsbuildConfig['output']>['target'],
199195
): NonNullable<NonNullable<RsbuildConfig['output']>['overrideBrowserslist']> {
200196
const handleSyntaxItem = (
201197
syntaxItem: EcmaScriptVersion | string,

0 commit comments

Comments
 (0)