Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ const composeExternalsConfig = (

const composeAutoExtensionConfig = (
config: LibConfig,
format: Format,
autoExtension: boolean,
pkgJson?: PkgJson,
): {
Expand All @@ -849,7 +850,7 @@ const composeAutoExtensionConfig = (
dtsExtension: string;
} => {
const { jsExtension, dtsExtension } = getDefaultExtension({
format: config.format!,
format,
pkgJson,
autoExtension,
});
Expand Down Expand Up @@ -1307,9 +1308,10 @@ const composeBundlelessExternalConfig = (

const composeDtsConfig = async (
libConfig: LibConfig,
format: Format,
dtsExtension: string,
): Promise<EnvironmentConfig> => {
const { format, autoExternal, banner, footer, redirect } = libConfig;
const { autoExternal, banner, footer, redirect } = libConfig;

let { dts } = libConfig;

Expand All @@ -1332,7 +1334,7 @@ const composeDtsConfig = async (
build: dts?.build,
abortOnError: dts?.abortOnError,
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
autoExternal: getAutoExternalDefaultValue(format!, autoExternal),
autoExternal: getAutoExternalDefaultValue(format, autoExternal),
banner: banner?.dts,
footer: footer?.dts,
redirect: redirect?.dts,
Expand Down Expand Up @@ -1454,7 +1456,7 @@ async function composeLibRsbuildConfig(
const cssModulesAuto = config.output?.cssModules?.auto ?? true;

const {
format,
format = 'esm',
shims,
bundle = true,
banner = {},
Expand All @@ -1466,11 +1468,11 @@ async function composeLibRsbuildConfig(
umdName,
} = config;
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
format!,
format,
shims,
);
const formatConfig = composeFormatConfig({
format: format!,
format,
pkgJson: pkgJson!,
bundle,
umdName,
Expand All @@ -1480,14 +1482,14 @@ async function composeLibRsbuildConfig(
pkgJson,
);
const userExternalsConfig = composeExternalsConfig(
format!,
format,
config.output?.externals,
);
const {
config: autoExtensionConfig,
jsExtension,
dtsExtension,
} = composeAutoExtensionConfig(config, autoExtension, pkgJson);
} = composeAutoExtensionConfig(config, format, autoExtension, pkgJson);
const { entryConfig, outBase } = await composeEntryConfig(
config.source?.entry!,
config.bundle,
Expand All @@ -1506,11 +1508,11 @@ async function composeLibRsbuildConfig(
config: targetConfig,
externalsConfig: targetExternalsConfig,
target,
} = composeTargetConfig(config.output?.target, format!);
} = composeTargetConfig(config.output?.target, format);
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
const autoExternalConfig = composeAutoExternalConfig({
bundle,
format: format!,
format,
autoExternal,
pkgJson,
userExternals: config.output?.externals,
Expand All @@ -1522,15 +1524,15 @@ async function composeLibRsbuildConfig(
banner?.css,
footer?.css,
);
const assetConfig = composeAssetConfig(bundle, format!);
const assetConfig = composeAssetConfig(bundle, format);

const entryChunkConfig = composeEntryChunkConfig({
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
contextToWatch: outBase,
});
const dtsConfig = await composeDtsConfig(config, dtsExtension);
const dtsConfig = await composeDtsConfig(config, format, dtsExtension);
const externalsWarnConfig = composeExternalsWarnConfig(
format!,
format,
userExternalsConfig?.output?.externals,
autoExternalConfig?.output?.externals,
);
Expand Down Expand Up @@ -1619,7 +1621,7 @@ export async function composeCreateRsbuildConfig(
delete userConfig.output.externals;

const config: RsbuildConfigWithLibInfo = {
format: libConfig.format!,
format: libConfig.format ?? 'esm',
// The merge order represents the priority of the configuration
// The priorities from high to low are as follows:
// 1 - userConfig: users can configure any Rsbuild and Rspack config
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export interface LibConfig extends EnvironmentConfig {
id?: string;
/**
* Output format for the generated JavaScript files.
* @defaultValue `undefined`
* @defaultValue `'esm'`
* @see {@link https://lib.rsbuild.dev/config/lib/format}
*/
format?: Format;
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/format/cjs-static-export/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "format-cjs-static-export-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "format-test",
"name": "format-default-test",
"version": "1.0.0",
"private": true,
"type": "module"
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/format/default/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/bundle-esm',
},
},
}),
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/bundle-cjs',
},
},
}),
generateBundleEsmConfig({
bundle: false,
output: {
distPath: {
root: './dist/bundleless-esm',
},
},
}),
generateBundleCjsConfig({
bundle: false,
output: {
distPath: {
root: './dist/bundleless-cjs',
},
},
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/format/default/src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
3 changes: 3 additions & 0 deletions tests/integration/format/default/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from './foo';

export const str = 'hello' + foo + ' world';
6 changes: 6 additions & 0 deletions tests/integration/format/import-meta-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "format-import-meta-url-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
47 changes: 47 additions & 0 deletions tests/integration/format/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,53 @@ import path from 'node:path';
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';

test('format default to esm', async () => {
const fixturePath = path.resolve(__dirname, 'default');
const { files, contents } = await buildAndGetResults({
fixturePath,
});

expect(files).toMatchInlineSnapshot(`
{
"cjs0": [
"<ROOT>/tests/integration/format/default/dist/bundle-cjs/index.cjs",
],
"cjs1": [
"<ROOT>/tests/integration/format/default/dist/bundleless-cjs/foo.cjs",
"<ROOT>/tests/integration/format/default/dist/bundleless-cjs/index.cjs",
],
"esm0": [
"<ROOT>/tests/integration/format/default/dist/bundle-esm/index.js",
],
"esm1": [
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/foo.js",
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/index.js",
],
}
`);

expect(contents.esm0).toMatchInlineSnapshot(`
{
"<ROOT>/tests/integration/format/default/dist/bundle-esm/index.js": "const foo = 'foo';
const str = 'hello' + foo + ' world';
export { str };
",
}
`);

expect(contents.esm1).toMatchInlineSnapshot(`
{
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/foo.js": "const foo = 'foo';
export { foo };
",
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/index.js": "import * as __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__ from "./foo.js";
const str = 'hello' + __WEBPACK_EXTERNAL_MODULE__foo_js_fdf5aa2d__.foo + ' world';
export { str };
",
}
`);
});

test('import.meta.url should be preserved', async () => {
const fixturePath = path.resolve(__dirname, 'import-meta-url');
const { files, entries, entryFiles } = await buildAndGetResults({
Expand Down
13 changes: 6 additions & 7 deletions tests/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function getCwdByExample(exampleName: string) {

export function generateBundleEsmConfig(config: LibConfig = {}): LibConfig {
const esmBasicConfig: LibConfig = {
format: 'esm',
output: {
distPath: {
root: './dist/esm',
Expand Down Expand Up @@ -107,19 +106,19 @@ export async function getResults(
let key = '';

const formatCount: Record<Format, number> = rslibConfig.lib.reduce(
(acc, { format }) => {
acc[format!] = (acc[format!] ?? 0) + 1;
(acc, { format = 'esm' }) => {
acc[format] = (acc[format] ?? 0) + 1;
return acc;
},
{} as Record<Format, number>,
);

for (const libConfig of rslibConfig.lib) {
const { format } = libConfig;
const currentFormatCount = formatCount[format!];
const currentFormatIndex = formatIndex[format!]++;
const { format = 'esm' } = libConfig;
const currentFormatCount = formatCount[format];
const currentFormatIndex = formatIndex[format]++;

key = currentFormatCount === 1 ? format! : `${format}${currentFormatIndex}`;
key = currentFormatCount === 1 ? format : `${format}${currentFormatIndex}`;

let globFolder = '';
if (type === 'js' || type === 'css') {
Expand Down
3 changes: 1 addition & 2 deletions website/docs/en/config/lib/format.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# lib.format

- **Type:** `'esm' | 'cjs' | 'umd' | 'mf'`
- **Default:** `undefined`
- **Required**: true
- **Default:** `'esm'`

Specify the output format for the generated JavaScript output files.

Expand Down
3 changes: 1 addition & 2 deletions website/docs/zh/config/lib/format.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# lib.format

- **类型:** `'esm' | 'cjs' | 'umd' | 'mf'`
- **默认值:** `undefined`
- **必填:** 是
- **默认值:** `'esm'`

指定生成的 JavaScript 产物的输出格式。

Expand Down
Loading