1
1
import { mkdir , readFile , rm , writeFile } from 'node:fs/promises'
2
- import { tmpdir } from 'node:os'
3
2
import { join } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
4
import { mkdist } from 'mkdist'
5
5
import { afterAll , describe , expect , it } from 'vitest'
6
6
import { vueLoader } from '../src/mkdist'
7
7
8
8
describe ( 'transform typescript script setup' , ( ) => {
9
- const tmpDir = join ( tmpdir ( ) , ' fixtures')
9
+ const tmpDir = fileURLToPath ( new URL ( './.tmp/ fixtures', import . meta . url ) )
10
10
afterAll ( async ( ) => {
11
11
await rm ( tmpDir , { force : true , recursive : true } )
12
12
} )
@@ -196,16 +196,16 @@ describe('transform typescript script setup', () => {
196
196
expect (
197
197
await fixture (
198
198
`<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>` ,
205
205
) ,
206
206
) . toMatchInlineSnapshot ( `
207
207
"<template>
208
- <div :data-test="toValue('hello')" />
208
+ <div :data-test="toValue('hello')" />
209
209
</template>
210
210
211
211
<script setup>
@@ -216,11 +216,46 @@ describe('transform typescript script setup', () => {
216
216
` )
217
217
} )
218
218
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
+
219
246
async function fixture ( src : string ) : Promise < string > {
220
247
await rm ( tmpDir , { force : true , recursive : true } )
221
248
await mkdir ( join ( tmpDir , 'src' ) , { recursive : true } )
222
249
await writeFile ( join ( tmpDir , 'src/index.vue' ) , src )
223
250
await mkdist ( { loaders : [ 'js' , vueLoader ] , rootDir : tmpDir } )
224
251
return await readFile ( join ( tmpDir , 'dist/index.vue' ) , 'utf-8' )
225
252
}
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
+ }
226
261
} )
0 commit comments