forked from rolldown/tsdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
708 lines (645 loc) · 18 KB
/
types.ts
File metadata and controls
708 lines (645 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import type { CopyEntry, CopyOptions, CopyOptionsFn } from '../features/copy.ts'
import type {
DepsConfig,
NoExternalFn,
ResolvedDepsConfig,
} from '../features/deps.ts'
import type { DevtoolsOptions } from '../features/devtools.ts'
import type { ExeOptions, SeaConfig } from '../features/exe.ts'
import type {
BuildContext,
RolldownContext,
TsdownHooks,
} from '../features/hooks.ts'
import type {
ChunkAddon,
ChunkAddonFunction,
ChunkAddonObject,
OutExtensionContext,
OutExtensionFactory,
OutExtensionObject,
} from '../features/output.ts'
import type { AttwOptions } from '../features/pkg/attw.ts'
import type { ExportsOptions } from '../features/pkg/exports.ts'
import type { PublintOptions } from '../features/pkg/publint.ts'
import type { ReportOptions } from '../features/report.ts'
import type { RolldownChunk, TsdownBundle } from '../utils/chunks.ts'
import type { Logger, LogLevel } from '../utils/logger.ts'
import type { PackageJsonWithPath, PackageType } from '../utils/package.ts'
import type {
Arrayable,
Awaitable,
MarkPartial,
Overwrite,
} from '../utils/types.ts'
import type { Hookable } from 'hookable'
import type {
ChecksOptions,
ExternalOption,
InputOptions,
InternalModuleFormat,
MinifyOptions,
ModuleFormat,
ModuleTypes,
OutputOptions,
TreeshakingOptions,
} from 'rolldown'
import type { Options as RolldownPluginDtsOptions } from 'rolldown-plugin-dts'
import type { Options as UnusedOptions } from 'unplugin-unused'
export interface DtsOptions extends RolldownPluginDtsOptions {
/**
* When building dual ESM+CJS formats, generate a `.d.cts` re-export stub
* instead of running a full second TypeScript compilation pass.
*
* The stub re-exports everything from the corresponding `.d.mts` file,
* ensuring CJS and ESM consumers share the same type declarations. This
* eliminates the TypeScript "dual module hazard" where separate `.d.cts`
* and `.d.mts` declarations cause `TS2352` ("neither type sufficiently
* overlaps") errors when casting between types derived from the same class.
*
* Only applies when building both `esm` and `cjs` formats simultaneously.
*
* @default false
*/
cjsReexport?: boolean
}
export type Sourcemap = boolean | 'inline' | 'hidden'
export type Format = ModuleFormat
export type NormalizedFormat = InternalModuleFormat
/**
* Extended input option that supports glob negation patterns.
*
* When using object form, values can be:
* - A single glob pattern string
* - An array of glob patterns, including negation patterns (prefixed with `!`)
*
* @example
* ```ts
* entry: {
* // Single pattern
* "utils/*": "./src/utils/*.ts",
* // Array with negation pattern to exclude files
* "hooks/*": ["./src/hooks/*.ts", "!./src/hooks/index.ts"],
* }
* ```
*/
export type TsdownInputOption = Arrayable<
string | Record<string, Arrayable<string>>
>
export type {
AttwOptions,
BuildContext,
ChunkAddon,
ChunkAddonFunction,
ChunkAddonObject,
CopyEntry,
CopyOptions,
CopyOptionsFn,
DepsConfig,
DevtoolsOptions,
ExeOptions,
ExportsOptions,
NoExternalFn,
OutExtensionContext,
OutExtensionFactory,
OutExtensionObject,
PackageJsonWithPath,
PackageType,
PublintOptions,
ReportOptions,
ResolvedDepsConfig,
RolldownChunk,
RolldownContext,
SeaConfig,
TreeshakingOptions,
TsdownBundle,
TsdownHooks,
UnusedOptions,
}
export interface Workspace {
/**
* Workspace directories. Glob patterns are supported.
* - `auto`: Automatically detect `package.json` files in the workspace.
* @default 'auto'
*/
include?: Arrayable<string> | 'auto'
/**
* Exclude directories from workspace.
* Defaults to all `node_modules`, `dist`, `test`, `tests`, `temp`, and `tmp` directories.
*/
exclude?: Arrayable<string>
/**
* Path to the workspace configuration file.
*/
config?: boolean | string
}
export type CIOption = 'ci-only' | 'local-only'
export type WithEnabled<T> =
| boolean
| undefined
| CIOption
| (T & {
/** @default true */
enabled?: boolean | CIOption
})
/**
* Options for tsdown.
*/
export interface UserConfig {
// #region Input Options
/**
* Defaults to `'src/index.ts'` if it exists.
*
* Supports glob patterns with negation to exclude files:
* @example
* ```ts
* entry: {
* "hooks/*": ["./src/hooks/*.ts", "!./src/hooks/index.ts"],
* }
* ```
*/
entry?: TsdownInputOption
/**
* Dependency handling options.
*/
deps?: DepsConfig
/**
* @deprecated Use `deps.neverBundle` instead.
*/
external?: ExternalOption
/**
* @deprecated Use `deps.alwaysBundle` instead.
*/
noExternal?: Arrayable<string | RegExp> | NoExternalFn
/**
* @deprecated Use `deps.onlyBundle` instead.
*/
inlineOnly?: Arrayable<string | RegExp> | false
/**
* @deprecated Use `deps.skipNodeModulesBundle` instead.
* @default false
*/
skipNodeModulesBundle?: boolean
alias?: Record<string, string>
tsconfig?: string | boolean
/**
* Specifies the target runtime platform for the build.
*
* - `node`: Node.js and compatible runtimes (e.g., Deno, Bun).
* For CJS format, this is always set to `node` and cannot be changed.
* - `neutral`: A platform-agnostic target with no specific runtime assumptions.
* - `browser`: Web browsers.
*
* @default 'node'
* @see https://tsdown.dev/options/platform
*/
platform?: 'node' | 'neutral' | 'browser'
/**
* Specifies the compilation target environment(s).
*
* Determines the JavaScript version or runtime(s) for which the code should be compiled.
* If not set, defaults to the value of `engines.node` in your project's `package.json`.
* If no `engines.node` field exists, no syntax transformations are applied.
*
* Accepts a single target (e.g., `'es2020'`, `'node18'`), an array of targets, or `false` to disable all transformations.
*
* @see {@link https://tsdown.dev/options/target#supported-targets} for a list of valid targets and more details.
*
* @example
* ```jsonc
* // Target a single environment
* { "target": "node18" }
* ```
*
* @example
* ```jsonc
* // Target multiple environments
* { "target": ["node18", "es2020"] }
* ```
*
* @example
* ```jsonc
* // Disable all syntax transformations
* { "target": false }
* ```
*/
target?: string | string[] | false
/**
* Compile-time env variables, which can be accessed via `import.meta.env` or `process.env`.
* @example
* ```json
* {
* "DEBUG": true,
* "NODE_ENV": "production"
* }
* ```
*/
env?: Record<string, any>
/**
* Path to env file providing compile-time env variables.
* @example
* `.env`, `.env.production`, etc.
*/
envFile?: string
/**
* When loading env variables from `envFile`, only include variables with these prefixes.
* @default 'TSDOWN_'
*/
envPrefix?: string | string[]
define?: Record<string, string>
/** @default false */
shims?: boolean
/**
* Configure tree shaking options.
* @see {@link https://rolldown.rs/options/treeshake} for more details.
* @default true
*/
treeshake?: boolean | TreeshakingOptions
/**
* Sets how input files are processed.
* For example, use 'js' to treat files as JavaScript or 'base64' for images.
* Lets you import or require files like images or fonts.
* @example
* ```json
* { '.jpg': 'asset', '.png': 'base64' }
* ```
*/
loader?: ModuleTypes
/**
* If enabled, strips the `node:` protocol prefix from import source.
*
* @default false
* @deprecated Use `nodeProtocol: 'strip'` instead.
*
* @example
* // With removeNodeProtocol enabled:
* import('node:fs'); // becomes import('fs')
*/
removeNodeProtocol?: boolean
/**
* - If `true`, add `node:` prefix to built-in modules.
* - If `'strip'`, strips the `node:` protocol prefix from import source.
* - If `false`, does not modify the import source.
*
* @default false
*
* @example
* // With nodeProtocol enabled:
* import('fs'); // becomes import('node:fs')
* // With nodeProtocol set to 'strip':
* import('node:fs'); // becomes import('fs')
* // With nodeProtocol set to false:
* import('node:fs'); // remains import('node:fs')
*
*/
nodeProtocol?: 'strip' | boolean
/**
* Controls which warnings are emitted during the build process. Each option can be set to `true` (emit warning) or `false` (suppress warning).
*/
checks?: ChecksOptions & {
/**
* If the config includes the `cjs` format and
* one of its target >= node 20.19.0 / 22.12.0,
* warn the user about the deprecation of CommonJS.
*
* @default true
*/
legacyCjs?: boolean
}
plugins?: InputOptions['plugins']
/**
* Use with caution; ensure you understand the implications.
*/
inputOptions?:
| InputOptions
| ((
options: InputOptions,
format: NormalizedFormat,
context: { cjsDts: boolean },
) => Awaitable<InputOptions | void | null>)
//#region Output Options
/**
* Output format(s). Available formats are
* - `esm`: ESM
* - `cjs`: CommonJS
* - `iife`: IIFE
* - `umd`: UMD
*
* Defaults to ESM.
*/
format?: Format | Format[] | Partial<Record<Format, Partial<ResolvedConfig>>>
globalName?: string
/** @default 'dist' */
outDir?: string
/**
* Whether to write the files to disk.
* This option is incompatible with watch mode.
* @default true
*/
write?: boolean
/**
* Whether to generate source map files.
*
* Note that this option will always be `true` if you have
* [`declarationMap`](https://www.typescriptlang.org/tsconfig/#declarationMap)
* option enabled in your `tsconfig.json`.
*
* @default false
*/
sourcemap?: Sourcemap
/**
* Clean directories before build.
*
* Default to output directory.
* @default true
*/
clean?: boolean | string[]
/**
* @default false
*/
minify?: boolean | 'dce-only' | MinifyOptions
footer?: ChunkAddon
banner?: ChunkAddon
/**
* Determines whether unbundle mode is enabled.
* When set to true, the output files will mirror the input file structure.
* @default false
*/
unbundle?: boolean
/**
* Specifies the root directory of input files, similar to TypeScript's `rootDir`.
* This determines the output directory structure.
*
* By default, the root is computed as the common base directory of all entry files.
*
* @see https://www.typescriptlang.org/tsconfig/#rootDir
*/
root?: string
/**
* @deprecated Use `unbundle` instead.
* @default true
*/
bundle?: boolean
/**
* Use a fixed extension for output files.
* The extension will always be `.cjs` or `.mjs`.
* Otherwise, it will depend on the package type.
*
* Defaults to `true` if `platform` is set to `node`, `false` otherwise.
*/
fixedExtension?: boolean
/**
* Custom extensions for output files.
* `fixedExtension` will be overridden by this option.
*/
outExtensions?: OutExtensionFactory
/**
* If enabled, appends hash to chunk filenames.
* @default true
*/
hash?: boolean
/**
* @default true
*/
cjsDefault?: boolean
/**
* Use with caution; ensure you understand the implications.
*/
outputOptions?:
| OutputOptions
| ((
options: OutputOptions,
format: NormalizedFormat,
context: { cjsDts: boolean },
) => Awaitable<OutputOptions | void | null>)
//#region CLI Options
/**
* The working directory of the config file.
* - Defaults to `process.cwd()` for root config.
* - Defaults to the package directory for workspace config.
*/
cwd?: string
/**
* The name to show in CLI output. This is useful for monorepos or workspaces.
* When using workspace mode, this option defaults to the package name from package.json.
* In non-workspace mode, this option must be set explicitly for the name to show in the CLI output.
*/
name?: string
/**
* Log level.
* @default 'info'
*/
logLevel?: LogLevel
/**
* If true, fails the build on warnings.
* @default false
*/
failOnWarn?: boolean | CIOption
/**
* Custom logger.
*/
customLogger?: Logger
/**
* Reuse config from Vite or Vitest (experimental)
* @default false
*/
fromVite?: boolean | 'vitest'
/**
* @default false
*/
watch?: boolean | Arrayable<string>
/**
* Files or patterns to not watch while in watch mode.
*/
ignoreWatch?: Arrayable<string | RegExp>
/**
* **[experimental]** Enable devtools.
*
*DevTools is still under development, and this is for early testers only.
*
* This may slow down the build process significantly.
*
* @default false
*/
devtools?: WithEnabled<DevtoolsOptions>
//#region Addons
/**
* You can specify command to be executed after a successful build, specially useful for Watch mode
*/
onSuccess?:
| string
| ((config: ResolvedConfig, signal: AbortSignal) => void | Promise<void>)
/**
* Enables generation of TypeScript declaration files (`.d.ts`).
*
* By default, this option is auto-detected based on your project's `package.json`:
* - If {@link exe} is enabled, declaration file generation is disabled by default.
* - If the `types` field is present, or if the main `exports` contains a `types` entry, declaration file generation is enabled by default.
* - Otherwise, declaration file generation is disabled by default.
*/
dts?: WithEnabled<DtsOptions>
/**
* Enable unused dependencies check with `unplugin-unused`
* Requires `unplugin-unused` to be installed.
* @default false
*/
unused?: WithEnabled<UnusedOptions>
/**
* Run publint after bundling.
* Requires `publint` to be installed.
* @default false
*/
publint?: WithEnabled<PublintOptions>
/**
* Run `arethetypeswrong` after bundling.
* Requires `@arethetypeswrong/core` to be installed.
*
* @default false
* @see https://github.com/arethetypeswrong/arethetypeswrong.github.io
*/
attw?: WithEnabled<AttwOptions>
/**
* Enable size reporting after bundling.
* @default true
*/
report?: WithEnabled<ReportOptions>
/**
* `import.meta.glob` support.
* @see https://vite.dev/guide/features.html#glob-import
* @default true
*/
globImport?: boolean
/**
* Generate package exports for `package.json`.
*
* This will set the `main`, `module`, `types`, `exports` fields in `package.json`
* to point to the generated files.
*/
exports?: WithEnabled<ExportsOptions>
/**
* **[experimental]** CSS options.
* Requires `@tsdown/css` to be installed.
*/
css?: import('@tsdown/css').CssOptions
/**
* @deprecated Use `css.inject` instead.
*/
injectStyle?: boolean
/**
* @deprecated Alias for `copy`, will be removed in the future.
*/
publicDir?: CopyOptions | CopyOptionsFn
/**
* Copy files to another directory.
* @example
* ```ts
* [
* 'src/assets',
* 'src/env.d.ts',
* 'src/styles/**\/*.css',
* { from: 'src/assets', to: 'dist/assets' },
* { from: 'src/styles/**\/*.css', to: 'dist', flatten: true },
* ]
* ```
*/
copy?: CopyOptions | CopyOptionsFn
hooks?:
| Partial<TsdownHooks>
| ((hooks: Hookable<TsdownHooks>) => Awaitable<void>)
/**
* **[experimental]** Bundle as executable using Node.js SEA (Single Executable Applications).
*
* This will bundle the output into a single executable file using Node.js SEA.
* Note that this is only supported on Node.js 25.7.0 and later, and is not supported in Bun or Deno.
*/
exe?: WithEnabled<ExeOptions>
/**
* **[experimental]** Enable workspace mode.
* This allows you to build multiple packages in a monorepo.
*/
workspace?: Workspace | Arrayable<string> | true
}
export interface InlineConfig extends UserConfig {
/**
* Config file path
*/
config?: boolean | string
/**
* Config loader to use. It can only be set via CLI or API.
* @default 'auto'
*/
configLoader?: 'auto' | 'native' | 'unrun'
/**
* Filter configs by cwd or name.
*/
filter?: RegExp | Arrayable<string>
}
export type UserConfigFn = (
inlineConfig: InlineConfig,
context: { ci: boolean; rootConfig?: UserConfig },
) => Awaitable<Arrayable<UserConfig>>
export type UserConfigExport = Awaitable<Arrayable<UserConfig> | UserConfigFn>
export type ResolvedConfig = Overwrite<
MarkPartial<
Omit<
UserConfig,
| 'workspace' // merged
| 'fromVite' // merged
| 'publicDir' // deprecated
| 'bundle' // deprecated
| 'injectStyle' // deprecated, merged to `css`
| 'removeNodeProtocol' // deprecated
| 'external' // deprecated, merged to `deps`
| 'noExternal' // deprecated, merged to `deps`
| 'inlineOnly' // deprecated, merged to `deps`
| 'skipNodeModulesBundle' // deprecated, merged to `deps`
| 'logLevel' // merge to `logger`
| 'failOnWarn' // merge to `logger`
| 'customLogger' // merge to `logger`
| 'envFile' // merged to `env`
| 'envPrefix' // merged to `env`
>,
| 'globalName'
| 'inputOptions'
| 'outputOptions'
| 'minify'
| 'define'
| 'alias'
| 'onSuccess'
| 'outExtensions'
| 'hooks'
| 'copy'
| 'loader'
| 'name'
| 'banner'
| 'footer'
| 'checks'
| 'css'
>,
{
/** Resolved entry map (after glob expansion) */
entry: Record<string, string>
/** Original entry config before glob resolution (for watch mode re-globbing) */
rawEntry?: TsdownInputOption
nameLabel: string | undefined
format: NormalizedFormat
target?: string[]
clean: string[]
pkg?: PackageJsonWithPath
nodeProtocol: 'strip' | boolean
logger: Logger
ignoreWatch: Array<string | RegExp>
deps: ResolvedDepsConfig
/** Resolved root directory of input files */
root: string
dts: false | DtsOptions
report: false | ReportOptions
tsconfig: false | string
exports: false | ExportsOptions
devtools: false | DevtoolsOptions
publint: false | PublintOptions
attw: false | AttwOptions
unused: false | UnusedOptions
exe: false | ExeOptions
}
>