Skip to content

Commit a08d30c

Browse files
committed
feat: advanced esm
1 parent 0a56962 commit a08d30c

File tree

28 files changed

+254
-77
lines changed

28 files changed

+254
-77
lines changed

examples/express-plugin/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export default defineConfig({
88
output: {
99
distPath: './dist/esm',
1010
},
11+
experiments: {
12+
advancedEsm: true,
13+
},
1114
},
1215
{
1316
format: 'cjs',

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default defineConfig({
77
{
88
format: 'esm',
99
dts: true,
10+
experiments: {
11+
advancedEsm: true,
12+
},
1013
output: {
1114
distPath: './dist/esm',
1215
},

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export default defineConfig({
88
{
99
format: 'esm',
1010
dts: true,
11+
experiments: {
12+
advancedEsm: true,
13+
},
1114
},
1215
],
1316
output: {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import { pluginUnpluginVue } from 'rsbuild-plugin-unplugin-vue';
33

44
export default defineConfig({
55
plugins: [pluginUnpluginVue()],
6-
lib: [{ format: 'esm' }],
6+
lib: [
7+
{
8+
format: 'esm',
9+
experiments: {
10+
advancedEsm: true,
11+
},
12+
},
13+
],
714
output: {
815
target: 'web',
916
},

packages/core/src/config.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,13 @@ const composeFormatConfig = ({
596596
bundle = true,
597597
umdName,
598598
pkgJson,
599+
advancedEsm,
599600
}: {
600601
format: Format;
601602
pkgJson: PkgJson;
602603
bundle?: boolean;
603604
umdName?: Rspack.LibraryName;
605+
advancedEsm: boolean;
604606
}): EnvironmentConfig => {
605607
const jsParserOptions: Record<string, Rspack.JavascriptParserOptions> = {
606608
cjs: {
@@ -620,13 +622,17 @@ const composeFormatConfig = ({
620622
},
621623
};
622624

625+
const experimentalEsmOutput = bundle && format === 'esm' && advancedEsm;
626+
623627
// The built-in Rslib plugin will apply to all formats except the `mf` format.
624628
// The `mf` format functions more like an application than a library and requires additional webpack runtime.
625629
const plugins = [
626630
new rspack.experiments.RslibPlugin({
627631
interceptApiPlugin: true,
632+
forceNodeShims: true,
628633
}),
629-
];
634+
experimentalEsmOutput && new rspack.experiments.EsmLibraryPlugin(),
635+
].filter(Boolean);
630636

631637
switch (format) {
632638
case 'esm':
@@ -643,8 +649,10 @@ const composeFormatConfig = ({
643649
},
644650
},
645651
optimization: {
646-
concatenateModules: true,
652+
// experimentalEsmOutput don't need concatenateModules
653+
concatenateModules: !experimentalEsmOutput,
647654
sideEffects: 'flag',
655+
runtimeChunk: experimentalEsmOutput ? 'single' : undefined,
648656
avoidEntryIife: true,
649657
splitChunks: {
650658
// Splitted "sync" chunks will make entry modules can't be inlined.
@@ -653,10 +661,12 @@ const composeFormatConfig = ({
653661
},
654662
output: {
655663
module: true,
656-
chunkFormat: 'module',
657-
library: {
658-
type: 'modern-module',
659-
},
664+
chunkFormat: experimentalEsmOutput ? false : 'module',
665+
library: experimentalEsmOutput
666+
? undefined
667+
: {
668+
type: 'modern-module',
669+
},
660670
chunkLoading: 'import',
661671
workerChunkLoading: 'import',
662672
},
@@ -1719,7 +1729,9 @@ async function composeLibRsbuildConfig(
17191729
externalHelpers = false,
17201730
redirect = {},
17211731
umdName,
1732+
experiments,
17221733
} = config;
1734+
const advancedEsm = experiments?.advancedEsm;
17231735
const { rsbuildConfig: bundleConfig } = composeBundleConfig(bundle);
17241736
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
17251737
format,
@@ -1730,6 +1742,7 @@ async function composeLibRsbuildConfig(
17301742
pkgJson: pkgJson!,
17311743
bundle,
17321744
umdName,
1745+
advancedEsm: advancedEsm ?? false,
17331746
});
17341747
const externalHelpersConfig = composeExternalHelpersConfig(
17351748
externalHelpers,
@@ -1929,6 +1942,7 @@ export async function composeCreateRsbuildConfig(
19291942
shims: true,
19301943
umdName: true,
19311944
outBase: true,
1945+
experiments: true,
19321946
}),
19331947
),
19341948
};

packages/core/src/types/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ export type Redirect = {
241241
dts?: DtsRedirect;
242242
};
243243

244+
export type LibExperiments = {
245+
/**
246+
* Whether to enable Rspack advanced ESM output.
247+
* @defaultValue `false`
248+
* @see {@link https://rslib.rs/config/lib/experiments#experimentsadvancedesm}
249+
*/
250+
advancedEsm?: boolean;
251+
};
252+
244253
export interface LibConfig extends EnvironmentConfig {
245254
/**
246255
* The unique identifier of the library.
@@ -343,6 +352,12 @@ export interface LibConfig extends EnvironmentConfig {
343352
* @inheritdoc
344353
*/
345354
output?: RslibOutputConfig;
355+
/**
356+
* Options for experimental features.
357+
* @defaultValue `{}`
358+
* @see {@link https://rslib.rs/config/lib/experiments}
359+
*/
360+
experiments?: LibExperiments;
346361
}
347362

348363
export type LibOnlyConfig = Omit<LibConfig, keyof EnvironmentConfig>;

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
951951
nodeEnv: false,
952952
concatenateModules: true,
953953
sideEffects: 'flag',
954+
runtimeChunk: undefined,
954955
avoidEntryIife: true
955956
},
956957
plugins: [
@@ -975,7 +976,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
975976
name: 'RslibPlugin',
976977
_args: [
977978
{
978-
interceptApiPlugin: true
979+
interceptApiPlugin: true,
980+
forceNodeShims: true
979981
}
980982
]
981983
},
@@ -1673,7 +1675,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
16731675
name: 'RslibPlugin',
16741676
_args: [
16751677
{
1676-
interceptApiPlugin: true
1678+
interceptApiPlugin: true,
1679+
forceNodeShims: true
16771680
}
16781681
]
16791682
},
@@ -2276,7 +2279,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
22762279
name: 'RslibPlugin',
22772280
_args: [
22782281
{
2279-
interceptApiPlugin: true
2282+
interceptApiPlugin: true,
2283+
forceNodeShims: true
22802284
}
22812285
]
22822286
},
@@ -2878,7 +2882,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
28782882
name: 'RslibPlugin',
28792883
_args: [
28802884
{
2881-
interceptApiPlugin: true
2885+
interceptApiPlugin: true,
2886+
forceNodeShims: true
28822887
}
28832888
]
28842889
},
@@ -3020,7 +3025,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
30203025
use: [
30213026
/* config.module.rule('css').use('mini-css-extract') */
30223027
{
3023-
loader: '<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/cssExtractLoader.js'
3028+
loader: '/Users/bytedance/libs/rspack-esm-lib-plugin/packages/rspack/dist/cssExtractLoader.js'
30243029
},
30253030
/* config.module.rule('css').use('css') */
30263031
{
@@ -3690,6 +3695,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
36903695
"optimization": {
36913696
"avoidEntryIife": true,
36923697
"concatenateModules": true,
3698+
"runtimeChunk": undefined,
36933699
"sideEffects": "flag",
36943700
"splitChunks": {
36953701
"chunks": "async",
@@ -3708,6 +3714,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
37083714
RslibPlugin {
37093715
"_args": [
37103716
{
3717+
"forceNodeShims": true,
37113718
"interceptApiPlugin": true,
37123719
},
37133720
],
@@ -3990,6 +3997,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
39903997
RslibPlugin {
39913998
"_args": [
39923999
{
4000+
"forceNodeShims": true,
39934001
"interceptApiPlugin": true,
39944002
},
39954003
],
@@ -4233,6 +4241,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
42334241
RslibPlugin {
42344242
"_args": [
42354243
{
4244+
"forceNodeShims": true,
42364245
"interceptApiPlugin": true,
42374246
},
42384247
],
@@ -4475,6 +4484,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
44754484
RslibPlugin {
44764485
"_args": [
44774486
{
4487+
"forceNodeShims": true,
44784488
"interceptApiPlugin": true,
44794489
},
44804490
],

0 commit comments

Comments
 (0)