11import { mkdir , readFile , rm , writeFile } from 'node:fs/promises'
2- import { tmpdir } from 'node:os'
32import { join } from 'node:path'
3+ import { fileURLToPath } from 'node:url'
44import { mkdist } from 'mkdist'
55import { afterAll , describe , expect , it } from 'vitest'
66import { vueLoader } from '../src/mkdist'
77
88describe ( '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