Skip to content

Commit 2db53a9

Browse files
authored
feat: add experimental ESM output support for node target (#5823)
1 parent bd1eff8 commit 2db53a9

File tree

7 files changed

+58
-28
lines changed

7 files changed

+58
-28
lines changed

e2e/cases/server/ssr-type-module/rsbuild.config.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,14 @@ export default defineConfig({
5555
},
5656
node: {
5757
output: {
58+
module: true,
5859
target: 'node',
5960
},
6061
source: {
6162
entry: {
6263
index: './src/index.server',
6364
},
6465
},
65-
tools: {
66-
rspack: (config) => {
67-
return {
68-
...config,
69-
experiments: {
70-
...config.experiments,
71-
outputModule: true,
72-
},
73-
output: {
74-
...config.output,
75-
chunkFormat: 'module',
76-
chunkLoading: 'import',
77-
library: {
78-
type: 'module',
79-
},
80-
},
81-
};
82-
},
83-
},
8466
},
8567
},
8668
html: {

e2e/cases/server/ssr/rsbuild.config.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default defineConfig({
5555
},
5656
node: {
5757
output: {
58+
module: true,
5859
target: 'node',
5960
},
6061
source: {
@@ -67,19 +68,10 @@ export default defineConfig({
6768
if (process.env.TEST_ESM_LIBRARY) {
6869
return {
6970
...config,
70-
experiments: {
71-
...config.experiments,
72-
outputModule: true,
73-
},
7471
output: {
7572
...config.output,
7673
filename: '[name].mjs',
7774
chunkFilename: '[name].mjs',
78-
chunkFormat: 'module',
79-
chunkLoading: 'import',
80-
library: {
81-
type: 'module',
82-
},
8375
},
8476
};
8577
}

packages/core/src/createRsbuild.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { pluginCleanOutput } from './plugins/cleanOutput';
2323
import { pluginCss } from './plugins/css';
2424
import { pluginDefine } from './plugins/define';
2525
import { pluginEntry } from './plugins/entry';
26+
import { pluginEsm } from './plugins/esm';
2627
import { pluginExternals } from './plugins/externals';
2728
import { pluginFileSize } from './plugins/fileSize';
2829
import { pluginHtml } from './plugins/html';
@@ -94,6 +95,7 @@ function applyDefaultPlugins(
9495
pluginMinimize(),
9596
pluginProgress(),
9697
pluginSwc(),
98+
pluginEsm(),
9799
pluginExternals(),
98100
pluginSplitChunks(),
99101
pluginInlineChunk(),

packages/core/src/defaultConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
173173
legalComments: 'linked',
174174
injectStyles: false,
175175
minify: true,
176+
module: false,
176177
manifest: false,
177178
sourceMap: {
178179
js: undefined,

packages/core/src/plugins/esm.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { RsbuildPlugin } from '../types';
2+
3+
export const pluginEsm = (): RsbuildPlugin => ({
4+
name: 'rsbuild:esm',
5+
6+
setup(api) {
7+
api.modifyBundlerChain((chain, { environment, isServer }) => {
8+
const { config } = environment;
9+
10+
if (!config.output.module) {
11+
return;
12+
}
13+
14+
if (!isServer) {
15+
throw new Error(
16+
'[rsbuild:config] `output.module` is only supported for Node.js targets.',
17+
);
18+
}
19+
20+
chain.output
21+
.module(true)
22+
.chunkFormat('module')
23+
.chunkLoading('import')
24+
.workerChunkLoading('import')
25+
.library({
26+
...chain.output.get('library'),
27+
type: 'module',
28+
});
29+
30+
chain.experiments({
31+
...chain.get('experiments'),
32+
outputModule: true,
33+
});
34+
});
35+
},
36+
});

packages/core/src/types/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,13 @@ export interface OutputConfig {
12301230
* @default false
12311231
*/
12321232
manifest?: ManifestConfig;
1233+
/**
1234+
* Whether to output JavaScript files in ES modules format. This feature is currently
1235+
* experimental and only available when `output.target` is `'node'`.
1236+
* @experimental
1237+
* @default false
1238+
*/
1239+
module?: boolean;
12331240
/**
12341241
* Whether to generate source map files, and which format of source map to generate.
12351242
*
@@ -1308,6 +1315,7 @@ export interface NormalizedOutputConfig extends OutputConfig {
13081315
assetPrefix: string;
13091316
dataUriLimit: number | NormalizedDataUriLimit;
13101317
manifest: ManifestConfig;
1318+
module: boolean;
13111319
minify: Minify;
13121320
inlineScripts: InlineChunkConfig;
13131321
inlineStyles: InlineChunkConfig;

packages/core/tests/__snapshots__/environments.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ exports[`environment config > should normalize environment config correctly 1`]
7272
"legalComments": "linked",
7373
"manifest": false,
7474
"minify": true,
75+
"module": false,
7576
"polyfill": "off",
7677
"sourceMap": {
7778
"css": false,
@@ -217,6 +218,7 @@ exports[`environment config > should normalize environment config correctly 2`]
217218
"legalComments": "linked",
218219
"manifest": false,
219220
"minify": true,
221+
"module": false,
220222
"polyfill": "off",
221223
"sourceMap": {
222224
"css": false,
@@ -362,6 +364,7 @@ exports[`environment config > should print environment config when inspect confi
362364
"legalComments": "linked",
363365
"manifest": false,
364366
"minify": true,
367+
"module": false,
365368
"polyfill": "off",
366369
"sourceMap": {
367370
"css": false,
@@ -503,6 +506,7 @@ exports[`environment config > should print environment config when inspect confi
503506
"legalComments": "linked",
504507
"manifest": false,
505508
"minify": true,
509+
"module": false,
506510
"polyfill": "off",
507511
"sourceMap": {
508512
"css": false,
@@ -664,6 +668,7 @@ exports[`environment config > should support modify environment config by api.mo
664668
"legalComments": "linked",
665669
"manifest": false,
666670
"minify": true,
671+
"module": false,
667672
"polyfill": "off",
668673
"sourceMap": {
669674
"css": false,
@@ -805,6 +810,7 @@ exports[`environment config > should support modify environment config by api.mo
805810
"legalComments": "linked",
806811
"manifest": false,
807812
"minify": true,
813+
"module": false,
808814
"polyfill": "off",
809815
"sourceMap": {
810816
"css": false,
@@ -947,6 +953,7 @@ exports[`environment config > should support modify environment config by api.mo
947953
"legalComments": "linked",
948954
"manifest": false,
949955
"minify": true,
956+
"module": false,
950957
"polyfill": "off",
951958
"sourceMap": {
952959
"css": false,
@@ -1092,6 +1099,7 @@ exports[`environment config > should support modify single environment config by
10921099
"legalComments": "linked",
10931100
"manifest": false,
10941101
"minify": true,
1102+
"module": false,
10951103
"polyfill": "off",
10961104
"sourceMap": {
10971105
"css": false,
@@ -1233,6 +1241,7 @@ exports[`environment config > should support modify single environment config by
12331241
"legalComments": "linked",
12341242
"manifest": false,
12351243
"minify": true,
1244+
"module": false,
12361245
"polyfill": "off",
12371246
"sourceMap": {
12381247
"css": false,

0 commit comments

Comments
 (0)