Skip to content

Commit decd630

Browse files
authored
fix: emit vue .d.ts (#20)
1 parent a19ce7b commit decd630

File tree

4 files changed

+67
-39
lines changed

4 files changed

+67
-39
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"typescript": "5.8.3",
7171
"unbuild": "3.5.0",
7272
"vitest": "3.1.1",
73-
"vue": "3.5.13"
73+
"vue": "3.5.13",
74+
"vue-tsc": "^2.2.8"
7475
},
7576
"resolutions": {
7677
"vue-sfc-transformer": "link:."

pnpm-lock.yaml

+19-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mkdist.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
8383
}
8484

8585
// generate dts
86-
await context.loadFile({
86+
const files = await context.loadFile({
8787
path: `${input.path}.js`,
8888
srcPath: `${input.srcPath}.js`,
8989
extension: '.js',
9090
getContents: () => 'export default {}',
9191
})
92+
addOutput(...files?.filter(f => f.declaration) || [])
9293

9394
const results = await Promise.all(
9495
blocks.map(async (data) => {

test/mkdist.test.ts

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
2-
import { tmpdir } from 'node:os'
32
import { join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
44
import { mkdist } from 'mkdist'
55
import { afterAll, describe, expect, it } from 'vitest'
66
import { vueLoader } from '../src/mkdist'
77

88
describe('transform typescript script setup', () => {
9-
const tmpDir = join(tmpdir(), 'fixtures')
9+
const tmpDir = fileURLToPath(new URL('./.tmp/fixtures', import.meta.url))
1010
afterAll(async () => {
1111
await rm(tmpDir, { force: true, recursive: true })
1212
})
@@ -196,16 +196,16 @@ describe('transform typescript script setup', () => {
196196
expect(
197197
await fixture(
198198
`<template>
199-
<div :data-test="toValue('hello')" />
200-
</template>
201-
<script setup lang="ts">
202-
import { toValue, type Ref } from 'vue'
203-
const msg = 1
204-
</script>`,
199+
<div :data-test="toValue('hello')" />
200+
</template>
201+
<script setup lang="ts">
202+
import { toValue, type Ref } from 'vue'
203+
const msg = 1
204+
</script>`,
205205
),
206206
).toMatchInlineSnapshot(`
207207
"<template>
208-
<div :data-test="toValue('hello')" />
208+
<div :data-test="toValue('hello')" />
209209
</template>
210210
211211
<script setup>
@@ -216,11 +216,46 @@ describe('transform typescript script setup', () => {
216216
`)
217217
})
218218

219+
it('generates declaration', { timeout: 10000 }, async () => {
220+
const src = `
221+
<template>
222+
<div :data-test="toValue('hello')" />
223+
</template>
224+
225+
<script>
226+
export default { name: 'App' }
227+
</script>
228+
229+
<script setup lang="ts">
230+
defineProps<{ msg: string }>()
231+
import { toValue, type Ref } from 'vue'
232+
const msg = 1
233+
</script>`
234+
235+
expect(await declaration(src)).toMatchInlineSnapshot(`
236+
"declare const _default: import("vue").DefineComponent<{
237+
msg: string;
238+
}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{
239+
msg: string;
240+
}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
241+
export default _default;
242+
"
243+
`)
244+
})
245+
219246
async function fixture(src: string): Promise<string> {
220247
await rm(tmpDir, { force: true, recursive: true })
221248
await mkdir(join(tmpDir, 'src'), { recursive: true })
222249
await writeFile(join(tmpDir, 'src/index.vue'), src)
223250
await mkdist({ loaders: ['js', vueLoader], rootDir: tmpDir })
224251
return await readFile(join(tmpDir, 'dist/index.vue'), 'utf-8')
225252
}
253+
254+
async function declaration(src: string): Promise<string> {
255+
await rm(tmpDir, { force: true, recursive: true })
256+
await mkdir(join(tmpDir, 'src'), { recursive: true })
257+
await writeFile(join(tmpDir, 'src/index.vue'), src)
258+
await mkdist({ declaration: true, loaders: ['js', vueLoader], rootDir: tmpDir })
259+
return await readFile(join(tmpDir, 'dist/index.vue.d.ts'), 'utf-8')
260+
}
226261
})

0 commit comments

Comments
 (0)