Skip to content

Commit 1d38598

Browse files
committed
feat: add typeAssert util back
1 parent 4f900ff commit 1d38598

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"rolldown-plugin-require-cjs": "catalog:dev",
136136
"typescript": "catalog:dev",
137137
"unocss": "catalog:docs",
138+
"unplugin-ast": "catalog:dev",
138139
"unplugin-lightningcss": "catalog:peer",
139140
"unplugin-unused": "catalog:peer",
140141
"vite": "catalog:docs",

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ catalogs:
3131
rolldown-plugin-dts-snapshot: ^0.3.2
3232
rolldown-plugin-require-cjs: ^0.3.3
3333
typescript: ~5.9.3
34+
unplugin-ast: ^0.15.4
3435
vitest: ^4.0.16
3536
docs:
3637
'@shikijs/vitepress-twoslash': ^3.20.0

src/features/pkg/exports.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path'
33
import { RE_DTS } from 'rolldown-plugin-dts/filename'
44
import { detectIndentation } from '../../utils/format.ts'
55
import { stripExtname } from '../../utils/fs.ts'
6-
import { matchPattern, slash } from '../../utils/general.ts'
6+
import { matchPattern, slash, typeAssert } from '../../utils/general.ts'
77
import type { NormalizedFormat, ResolvedConfig } from '../../config/types.ts'
88
import type {
99
ChunksByFormat,
@@ -59,9 +59,10 @@ export async function writeExports(
5959
options: ResolvedConfig,
6060
chunks: ChunksByFormat,
6161
): Promise<void> {
62-
const pkg = options.pkg!
63-
const exports = options.exports as ExportsOptions
62+
typeAssert(options.pkg)
63+
typeAssert(options.exports)
6464

65+
const { pkg, exports } = options
6566
const { publishExports, ...generated } = await generateExports(
6667
pkg,
6768
chunks,

src/utils/general.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,10 @@ export function promiseWithResolvers<T>(): {
100100
const promise = new Promise<T>((res) => {
101101
resolve = res
102102
})
103-
return { promise, resolve: resolve! }
103+
typeAssert(resolve!)
104+
return { promise, resolve }
104105
}
106+
107+
export function typeAssert<T>(
108+
value: T,
109+
): asserts value is Exclude<T, false | null | undefined> {}

tsdown.config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { DtsSnapshot } from 'rolldown-plugin-dts-snapshot'
22
import { RequireCJS } from 'rolldown-plugin-require-cjs'
3+
import { isCallOf } from 'unplugin-ast/ast-kit'
4+
import AST from 'unplugin-ast/rolldown'
5+
import { RemoveNode } from 'unplugin-ast/transformers'
36
import { defineConfig } from './src/config.ts'
47

58
export default defineConfig([
@@ -32,7 +35,14 @@ export default defineConfig([
3235
return exports
3336
},
3437
},
35-
plugins: [RequireCJS(), DtsSnapshot()],
38+
plugins: [
39+
RequireCJS(),
40+
DtsSnapshot(),
41+
AST({
42+
exclude: ['**/*.d.ts'],
43+
transformer: [RemoveNode((node) => isCallOf(node, 'typeAssert'))],
44+
}),
45+
],
3646
onSuccess() {
3747
console.info('🙏 Build succeeded!')
3848
},

0 commit comments

Comments
 (0)