Skip to content

Commit 0b244b0

Browse files
committed
feat: add enabled option to feature options
1 parent d6aeeac commit 0b244b0

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

src/config/index.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import isInCi from 'is-in-ci'
2+
import { describe, expect, test } from 'vitest'
3+
import { resolveFeatureOption as _resolveFeatureOption } from './index.ts'
4+
5+
const defaultOption = { a: 1 }
6+
interface DefaultOption {
7+
a?: number
8+
}
9+
const resolveFeatureOption = _resolveFeatureOption<DefaultOption>
10+
11+
describe('resolveFeatureOption', () => {
12+
test('literal boolean', () => {
13+
expect(resolveFeatureOption(true, defaultOption)).toBe(defaultOption)
14+
expect(resolveFeatureOption(false, defaultOption)).toBe(false)
15+
})
16+
17+
test('literal CI', () => {
18+
expect(resolveFeatureOption('ci-only', defaultOption)).toBe(
19+
isInCi ? defaultOption : false,
20+
)
21+
expect(resolveFeatureOption('local-only', defaultOption)).toBe(
22+
isInCi ? false : defaultOption,
23+
)
24+
})
25+
26+
test('object with boolean enabled', () => {
27+
{
28+
const value = { a: 42 }
29+
expect(resolveFeatureOption(value, defaultOption)).toBe(value)
30+
}
31+
32+
{
33+
const value = { enabled: true, a: 42 }
34+
expect(resolveFeatureOption(value, defaultOption)).toBe(value)
35+
}
36+
37+
expect(resolveFeatureOption({ enabled: false, a: 42 }, defaultOption)).toBe(
38+
false,
39+
)
40+
})
41+
42+
test('object with CI enabled', () => {
43+
{
44+
const value = { enabled: 'ci-only' as const, a: 42 }
45+
expect(resolveFeatureOption(value, defaultOption)).toBe(
46+
isInCi ? value : false,
47+
)
48+
}
49+
50+
{
51+
const value = { enabled: 'local-only' as const, a: 42 }
52+
expect(resolveFeatureOption(value, defaultOption)).toBe(
53+
isInCi ? false : value,
54+
)
55+
}
56+
})
57+
})

src/config/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
InlineConfig,
2626
ResolvedConfig,
2727
UserConfig,
28+
WithEnabled,
2829
} from './types.ts'
2930

3031
export * from './types.ts'
@@ -391,12 +392,18 @@ export async function mergeUserOptions<T extends object, A extends unknown[]>(
391392
return { ...defaults, ...userOutputOptions }
392393
}
393394

394-
function resolveFeatureOption<T>(
395-
value: boolean | CIOption | T,
395+
export function resolveFeatureOption<T>(
396+
value: Exclude<WithEnabled<T>, undefined>,
396397
defaults: T,
397398
): T | false {
398-
if (value === true) return defaults
399-
if (value === 'ci-only') return isInCi ? defaults : false
400-
if (value === 'local-only') return isInCi ? false : defaults
399+
if (typeof value === 'object' && value !== null) {
400+
return resolveCIOption(value.enabled ?? true) ? value : false
401+
}
402+
return resolveCIOption(value) ? defaults : false
403+
}
404+
405+
function resolveCIOption(value: boolean | CIOption): boolean {
406+
if (value === 'ci-only') return isInCi ? true : false
407+
if (value === 'local-only') return isInCi ? false : true
401408
return value
402409
}

src/config/types.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ export type NoExternalFn = (
9595

9696
export type CIOption = 'ci-only' | 'local-only'
9797

98+
export type WithEnabled<T> =
99+
| boolean
100+
| undefined
101+
| CIOption
102+
| (T & {
103+
/** @default true */
104+
enabled?: boolean | CIOption
105+
})
106+
98107
/**
99108
* Options for tsdown.
100109
*/
@@ -389,7 +398,7 @@ export interface UserConfig {
389398
*
390399
* @default false
391400
*/
392-
debug?: boolean | CIOption | DebugOptions
401+
debug?: WithEnabled<DebugOptions>
393402

394403
//#region Addons
395404

@@ -400,21 +409,21 @@ export interface UserConfig {
400409
* - If the `types` field is present, or if the main `exports` contains a `types` entry, declaration file generation is enabled by default.
401410
* - Otherwise, declaration file generation is disabled by default.
402411
*/
403-
dts?: boolean | CIOption | DtsOptions
412+
dts?: WithEnabled<DtsOptions>
404413

405414
/**
406415
* Enable unused dependencies check with `unplugin-unused`
407416
* Requires `unplugin-unused` to be installed.
408417
* @default false
409418
*/
410-
unused?: boolean | CIOption | UnusedOptions
419+
unused?: WithEnabled<UnusedOptions>
411420

412421
/**
413422
* Run publint after bundling.
414423
* Requires `publint` to be installed.
415424
* @default false
416425
*/
417-
publint?: boolean | CIOption | PublintOptions
426+
publint?: WithEnabled<PublintOptions>
418427

419428
/**
420429
* Run `arethetypeswrong` after bundling.
@@ -423,13 +432,13 @@ export interface UserConfig {
423432
* @default false
424433
* @see https://github.com/arethetypeswrong/arethetypeswrong.github.io
425434
*/
426-
attw?: boolean | CIOption | AttwOptions
435+
attw?: WithEnabled<AttwOptions>
427436

428437
/**
429438
* Enable size reporting after bundling.
430439
* @default true
431440
*/
432-
report?: boolean | CIOption | ReportOptions
441+
report?: WithEnabled<ReportOptions>
433442

434443
/**
435444
* `import.meta.glob` support.
@@ -444,7 +453,7 @@ export interface UserConfig {
444453
* This will set the `main`, `module`, `types`, `exports` fields in `package.json`
445454
* to point to the generated files.
446455
*/
447-
exports?: boolean | CIOption | ExportsOptions
456+
exports?: WithEnabled<ExportsOptions>
448457

449458
/**
450459
* @deprecated Alias for `copy`, will be removed in the future.

tsdown.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export default defineConfig([
1515
],
1616
},
1717
publint: 'ci-only',
18+
attw: {
19+
enabled: 'ci-only',
20+
profile: 'esmOnly',
21+
},
1822
exports: {
1923
customExports(exports) {
2024
exports['./client'] = './client.d.ts'
@@ -33,6 +37,10 @@ export default defineConfig([
3337
inlineOnly: [],
3438
failOnWarn: 'ci-only',
3539
publint: 'ci-only',
40+
attw: {
41+
enabled: 'ci-only',
42+
profile: 'esmOnly',
43+
},
3644
exports: true,
3745
},
3846
])

0 commit comments

Comments
 (0)