Skip to content

Commit 482f682

Browse files
committed
feat: override options by format
1 parent 068061a commit 482f682

File tree

5 files changed

+74
-29
lines changed

5 files changed

+74
-29
lines changed

docs/options/output-format.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ By default, `tsdown` generates JavaScript code in the [ESM](https://nodejs.org/a
66
tsdown --format esm # default
77
```
88

9-
### Available Formats
9+
## Available Formats
1010

1111
- [`esm`](https://nodejs.org/api/esm.html): ECMAScript Module format, ideal for modern JavaScript environments, including browsers and Node.js.
1212
- [`cjs`](https://nodejs.org/api/modules.html): CommonJS format, commonly used in Node.js projects.
@@ -28,3 +28,23 @@ tsdown --format iife
2828

2929
> [!TIP]
3030
> You can specify multiple formats in a single command to generate outputs for different environments. For example, combining `esm` and `cjs` ensures compatibility with both modern and legacy systems.
31+
32+
## Overriding Configuration by Format
33+
34+
You can override specific configuration options for each output format by setting `format` as an object in your config file. This allows you to tailor settings such as `target` or other options for each format individually.
35+
36+
```ts
37+
export default defineConfig({
38+
entry: ['./src/index.js'],
39+
format: {
40+
esm: {
41+
target: ['es2015'],
42+
},
43+
cjs: {
44+
target: ['node20'],
45+
},
46+
},
47+
})
48+
```
49+
50+
In this example, the ESM output will target ES2015, while the CJS output will target Node.js 20. This approach gives you fine-grained control over the build process for different module formats.

docs/zh-CN/options/output-format.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
tsdown --format esm # 默认
77
```
88

9-
### 可用格式
9+
## 可用格式
1010

1111
- [`esm`](https://nodejs.org/api/esm.html):ECMAScript 模块格式,适用于包括浏览器和 Node.js 在内的现代 JavaScript 环境。
1212
- [`cjs`](https://nodejs.org/api/modules.html):CommonJS 格式,常用于 Node.js 项目。
@@ -28,3 +28,23 @@ tsdown --format iife
2828

2929
> [!TIP]
3030
> 您可以在单个命令中指定多个格式,以生成适用于不同环境的输出。例如,结合使用 `esm``cjs` 格式可以确保同时兼容现代和传统系统。
31+
32+
## 按格式覆盖配置
33+
34+
您可以在配置文件中将 `format` 设置为对象,从而为每种输出格式单独覆盖特定配置选项。这允许您为每个格式分别定制如 `target` 等设置。
35+
36+
```ts
37+
export default defineConfig({
38+
entry: ['./src/index.js'],
39+
format: {
40+
esm: {
41+
target: ['es2015'],
42+
},
43+
cjs: {
44+
target: ['node20'],
45+
},
46+
},
47+
})
48+
```
49+
50+
在此示例中,ESM 输出将以 ES2015 为目标,CJS 输出将以 Node.js 20 为目标。通过这种方式,您可以对不同模块格式的构建过程进行精细化控制。

src/config/options.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { resolveTsconfig } from '../features/tsconfig.ts'
1111
import {
1212
matchPattern,
1313
pkgExists,
14+
resolveComma,
1415
resolveRegex,
1516
toArray,
1617
} from '../utils/general.ts'
@@ -20,6 +21,7 @@ import type { Awaitable } from '../utils/types.ts'
2021
import { loadViteConfig } from './file.ts'
2122
import type {
2223
CIOption,
24+
Format,
2325
InlineConfig,
2426
ResolvedConfig,
2527
UserConfig,
@@ -259,15 +261,23 @@ export async function resolveUserConfig(
259261
write,
260262
}
261263

262-
return normalizeFormat(format).map((format, idx): ResolvedConfig => {
264+
const objectFormat = typeof format === 'object' && !Array.isArray(format)
265+
const formats = objectFormat
266+
? (Object.keys(format) as Format[])
267+
: resolveComma(toArray<Format>(format, 'es'))
268+
return formats.map((fmt, idx): ResolvedConfig => {
263269
const once = idx === 0
270+
const overrides = objectFormat ? format[fmt] : undefined
264271
return {
265272
...config,
266-
format,
267273
// only copy once
268274
copy: once ? config.copy : undefined,
269275
// don't register hooks repeatedly
270276
hooks: once ? config.hooks : undefined,
277+
// only execute once
278+
onSuccess: once ? config.onSuccess : undefined,
279+
format: normalizeFormat(fmt),
280+
...overrides,
271281
}
272282
})
273283
}

src/config/types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export interface UserConfig {
257257
//#region Output Options
258258

259259
/** @default 'es' */
260-
format?: Format | Format[]
260+
format?: Format | Format[] | Partial<Record<Format, Partial<ResolvedConfig>>>
261261
globalName?: string
262262
/** @default 'dist' */
263263
outDir?: string
@@ -393,13 +393,6 @@ export interface UserConfig {
393393
*/
394394
ignoreWatch?: Arrayable<string | RegExp>
395395

396-
/**
397-
* You can specify command to be executed after a successful build, specially useful for Watch mode
398-
*/
399-
onSuccess?:
400-
| string
401-
| ((config: ResolvedConfig, signal: AbortSignal) => void | Promise<void>)
402-
403396
/**
404397
* **[experimental]** Enable debug mode.
405398
*
@@ -413,6 +406,13 @@ export interface UserConfig {
413406

414407
//#region Addons
415408

409+
/**
410+
* You can specify command to be executed after a successful build, specially useful for Watch mode
411+
*/
412+
onSuccess?:
413+
| string
414+
| ((config: ResolvedConfig, signal: AbortSignal) => void | Promise<void>)
415+
416416
/**
417417
* Enables generation of TypeScript declaration files (`.d.ts`).
418418
*

src/utils/package.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { readFile } from 'node:fs/promises'
22
import { up as findPackage } from 'empathic/package'
33
import { createDebug } from 'obug'
44
import type { Format, NormalizedFormat } from '../config/index.ts'
5-
import { resolveComma, toArray } from './general.ts'
65
import type { PackageJson } from 'pkg-types'
76

87
const debug = createDebug('tsdown:package')
@@ -34,20 +33,16 @@ export function getPackageType(pkg: PackageJson | undefined): PackageType {
3433
}
3534
}
3635

37-
export function normalizeFormat(format: Format | Format[]): NormalizedFormat[] {
38-
return resolveComma(toArray<Format>(format, 'es')).map(
39-
(format): NormalizedFormat => {
40-
switch (format) {
41-
case 'es':
42-
case 'esm':
43-
case 'module':
44-
return 'es'
45-
case 'cjs':
46-
case 'commonjs':
47-
return 'cjs'
48-
default:
49-
return format
50-
}
51-
},
52-
)
36+
export function normalizeFormat(format: Format): NormalizedFormat {
37+
switch (format) {
38+
case 'es':
39+
case 'esm':
40+
case 'module':
41+
return 'es'
42+
case 'cjs':
43+
case 'commonjs':
44+
return 'cjs'
45+
default:
46+
return format
47+
}
5348
}

0 commit comments

Comments
 (0)