Skip to content

Commit 2ed952a

Browse files
committed
feat: format default to 'esm'
1 parent e285e79 commit 2ed952a

File tree

14 files changed

+133
-29
lines changed

14 files changed

+133
-29
lines changed

packages/core/src/config.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ const composeExternalsConfig = (
841841

842842
const composeAutoExtensionConfig = (
843843
config: LibConfig,
844+
format: Format,
844845
autoExtension: boolean,
845846
pkgJson?: PkgJson,
846847
): {
@@ -849,7 +850,7 @@ const composeAutoExtensionConfig = (
849850
dtsExtension: string;
850851
} => {
851852
const { jsExtension, dtsExtension } = getDefaultExtension({
852-
format: config.format!,
853+
format,
853854
pkgJson,
854855
autoExtension,
855856
});
@@ -1307,9 +1308,10 @@ const composeBundlelessExternalConfig = (
13071308

13081309
const composeDtsConfig = async (
13091310
libConfig: LibConfig,
1311+
format: Format,
13101312
dtsExtension: string,
13111313
): Promise<EnvironmentConfig> => {
1312-
const { format, autoExternal, banner, footer, redirect } = libConfig;
1314+
const { autoExternal, banner, footer, redirect } = libConfig;
13131315

13141316
let { dts } = libConfig;
13151317

@@ -1332,7 +1334,7 @@ const composeDtsConfig = async (
13321334
build: dts?.build,
13331335
abortOnError: dts?.abortOnError,
13341336
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
1335-
autoExternal: getAutoExternalDefaultValue(format!, autoExternal),
1337+
autoExternal: getAutoExternalDefaultValue(format, autoExternal),
13361338
banner: banner?.dts,
13371339
footer: footer?.dts,
13381340
redirect: redirect?.dts,
@@ -1454,7 +1456,7 @@ async function composeLibRsbuildConfig(
14541456
const cssModulesAuto = config.output?.cssModules?.auto ?? true;
14551457

14561458
const {
1457-
format,
1459+
format = 'esm',
14581460
shims,
14591461
bundle = true,
14601462
banner = {},
@@ -1466,11 +1468,11 @@ async function composeLibRsbuildConfig(
14661468
umdName,
14671469
} = config;
14681470
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
1469-
format!,
1471+
format,
14701472
shims,
14711473
);
14721474
const formatConfig = composeFormatConfig({
1473-
format: format!,
1475+
format: format,
14741476
pkgJson: pkgJson!,
14751477
bundle,
14761478
umdName,
@@ -1480,14 +1482,14 @@ async function composeLibRsbuildConfig(
14801482
pkgJson,
14811483
);
14821484
const userExternalsConfig = composeExternalsConfig(
1483-
format!,
1485+
format,
14841486
config.output?.externals,
14851487
);
14861488
const {
14871489
config: autoExtensionConfig,
14881490
jsExtension,
14891491
dtsExtension,
1490-
} = composeAutoExtensionConfig(config, autoExtension, pkgJson);
1492+
} = composeAutoExtensionConfig(config, format, autoExtension, pkgJson);
14911493
const { entryConfig, outBase } = await composeEntryConfig(
14921494
config.source?.entry!,
14931495
config.bundle,
@@ -1506,11 +1508,11 @@ async function composeLibRsbuildConfig(
15061508
config: targetConfig,
15071509
externalsConfig: targetExternalsConfig,
15081510
target,
1509-
} = composeTargetConfig(config.output?.target, format!);
1511+
} = composeTargetConfig(config.output?.target, format);
15101512
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
15111513
const autoExternalConfig = composeAutoExternalConfig({
15121514
bundle,
1513-
format: format!,
1515+
format: format,
15141516
autoExternal,
15151517
pkgJson,
15161518
userExternals: config.output?.externals,
@@ -1522,15 +1524,15 @@ async function composeLibRsbuildConfig(
15221524
banner?.css,
15231525
footer?.css,
15241526
);
1525-
const assetConfig = composeAssetConfig(bundle, format!);
1527+
const assetConfig = composeAssetConfig(bundle, format);
15261528

15271529
const entryChunkConfig = composeEntryChunkConfig({
15281530
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
15291531
contextToWatch: outBase,
15301532
});
1531-
const dtsConfig = await composeDtsConfig(config, dtsExtension);
1533+
const dtsConfig = await composeDtsConfig(config, format, dtsExtension);
15321534
const externalsWarnConfig = composeExternalsWarnConfig(
1533-
format!,
1535+
format,
15341536
userExternalsConfig?.output?.externals,
15351537
autoExternalConfig?.output?.externals,
15361538
);
@@ -1619,7 +1621,7 @@ export async function composeCreateRsbuildConfig(
16191621
delete userConfig.output.externals;
16201622

16211623
const config: RsbuildConfigWithLibInfo = {
1622-
format: libConfig.format!,
1624+
format: libConfig.format ?? 'esm',
16231625
// The merge order represents the priority of the configuration
16241626
// The priorities from high to low are as follows:
16251627
// 1 - userConfig: users can configure any Rsbuild and Rspack config

packages/core/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export interface LibConfig extends EnvironmentConfig {
215215
id?: string;
216216
/**
217217
* Output format for the generated JavaScript files.
218-
* @defaultValue `undefined`
218+
* @defaultValue `'esm'`
219219
* @see {@link https://lib.rsbuild.dev/config/lib/format}
220220
*/
221221
format?: Format;

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "format-cjs-static-export-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}

tests/integration/format/package.json renamed to tests/integration/format/default/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "format-test",
2+
"name": "format-default-test",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
output: {
8+
distPath: {
9+
root: './dist/bundle-esm',
10+
},
11+
},
12+
}),
13+
generateBundleCjsConfig({
14+
output: {
15+
distPath: {
16+
root: './dist/bundle-cjs',
17+
},
18+
},
19+
}),
20+
generateBundleEsmConfig({
21+
bundle: false,
22+
output: {
23+
distPath: {
24+
root: './dist/bundleless-esm',
25+
},
26+
},
27+
}),
28+
generateBundleCjsConfig({
29+
bundle: false,
30+
output: {
31+
distPath: {
32+
root: './dist/bundleless-cjs',
33+
},
34+
},
35+
}),
36+
],
37+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from './foo';
2+
3+
export const str = 'hello' + foo + ' world';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "format-import-meta-url-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}

tests/integration/format/index.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
1+
import exp from 'node:constants';
12
import path from 'node:path';
23
import { buildAndGetResults } from 'test-helper';
34
import { expect, test } from 'vitest';
45

6+
test('format default to esm', async () => {
7+
const fixturePath = path.resolve(__dirname, 'default');
8+
const { files, contents } = await buildAndGetResults({
9+
fixturePath,
10+
});
11+
12+
expect(files).toMatchInlineSnapshot(`
13+
{
14+
"cjs0": [
15+
"<ROOT>/tests/integration/format/default/dist/bundle-cjs/index.cjs",
16+
],
17+
"cjs1": [
18+
"<ROOT>/tests/integration/format/default/dist/bundleless-cjs/foo.cjs",
19+
"<ROOT>/tests/integration/format/default/dist/bundleless-cjs/index.cjs",
20+
],
21+
"esm0": [
22+
"<ROOT>/tests/integration/format/default/dist/bundle-esm/index.js",
23+
],
24+
"esm1": [
25+
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/foo.js",
26+
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/index.js",
27+
],
28+
}
29+
`);
30+
31+
expect(contents.esm0).toMatchInlineSnapshot(`
32+
{
33+
"<ROOT>/tests/integration/format/default/dist/bundle-esm/index.js": "const foo = 'foo';
34+
const str = 'hello' + foo + ' world';
35+
export { str };
36+
",
37+
}
38+
`);
39+
40+
expect(contents.esm1).toMatchInlineSnapshot(`
41+
{
42+
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/foo.js": "const foo = 'foo';
43+
export { foo };
44+
",
45+
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/index.js": "import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
46+
const str = 'hello' + __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + ' world';
47+
export { str };
48+
",
49+
}
50+
`);
51+
});
52+
553
test('import.meta.url should be preserved', async () => {
654
const fixturePath = path.resolve(__dirname, 'import-meta-url');
755
const { files, entries, entryFiles } = await buildAndGetResults({

0 commit comments

Comments
 (0)