1
- import { parse , compileScriptSetup , SFCScriptCompileOptions } from '../src'
1
+ import { parse , SFCScriptCompileOptions } from '../src'
2
2
import { parse as babelParse } from '@babel/parser'
3
3
import { babelParserDefautPlugins } from '@vue/shared'
4
4
5
5
function compile ( src : string , options ?: SFCScriptCompileOptions ) {
6
- const { descriptor } = parse ( src )
7
- return compileScriptSetup ( descriptor , options )
6
+ return parse ( src , options ) . descriptor . scriptTransformed !
8
7
}
9
8
10
9
function assertCode ( code : string ) {
@@ -23,48 +22,50 @@ function assertCode(code: string) {
23
22
24
23
describe ( 'SFC compile <script setup>' , ( ) => {
25
24
test ( 'should hoist imports' , ( ) => {
26
- assertCode ( compile ( `<script setup>import { ref } from 'vue'</script>` ) . code )
25
+ assertCode (
26
+ compile ( `<script setup>import { ref } from 'vue'</script>` ) . content
27
+ )
27
28
} )
28
29
29
30
test ( 'explicit setup signature' , ( ) => {
30
31
assertCode (
31
- compile ( `<script setup="props, { emit }">emit('foo')</script>` ) . code
32
+ compile ( `<script setup="props, { emit }">emit('foo')</script>` ) . content
32
33
)
33
34
} )
34
35
35
36
test ( 'import dedupe between <script> and <script setup>' , ( ) => {
36
- const code = compile ( `
37
+ const { content } = compile ( `
37
38
<script>
38
39
import { x } from './x'
39
40
</script>
40
41
<script setup>
41
42
import { x } from './x'
42
43
x()
43
44
</script>
44
- ` ) . code
45
- assertCode ( code )
46
- expect ( code . indexOf ( `import { x }` ) ) . toEqual (
47
- code . lastIndexOf ( `import { x }` )
45
+ ` )
46
+ assertCode ( content )
47
+ expect ( content . indexOf ( `import { x }` ) ) . toEqual (
48
+ content . lastIndexOf ( `import { x }` )
48
49
)
49
50
} )
50
51
51
52
describe ( 'exports' , ( ) => {
52
53
test ( 'export const x = ...' , ( ) => {
53
- const { code , bindings } = compile (
54
+ const { content , bindings } = compile (
54
55
`<script setup>export const x = 1</script>`
55
56
)
56
- assertCode ( code )
57
+ assertCode ( content )
57
58
expect ( bindings ) . toStrictEqual ( {
58
59
x : 'setup'
59
60
} )
60
61
} )
61
62
62
63
test ( 'export const { x } = ... (destructuring)' , ( ) => {
63
- const { code , bindings } = compile ( `<script setup>
64
+ const { content , bindings } = compile ( `<script setup>
64
65
export const [a = 1, { b } = { b: 123 }, ...c] = useFoo()
65
66
export const { d = 2, _: [e], ...f } = useBar()
66
67
</script>` )
67
- assertCode ( code )
68
+ assertCode ( content )
68
69
expect ( bindings ) . toStrictEqual ( {
69
70
a : 'setup' ,
70
71
b : 'setup' ,
@@ -76,100 +77,100 @@ describe('SFC compile <script setup>', () => {
76
77
} )
77
78
78
79
test ( 'export function x() {}' , ( ) => {
79
- const { code , bindings } = compile (
80
+ const { content , bindings } = compile (
80
81
`<script setup>export function x(){}</script>`
81
82
)
82
- assertCode ( code )
83
+ assertCode ( content )
83
84
expect ( bindings ) . toStrictEqual ( {
84
85
x : 'setup'
85
86
} )
86
87
} )
87
88
88
89
test ( 'export class X() {}' , ( ) => {
89
- const { code , bindings } = compile (
90
+ const { content , bindings } = compile (
90
91
`<script setup>export class X {}</script>`
91
92
)
92
- assertCode ( code )
93
+ assertCode ( content )
93
94
expect ( bindings ) . toStrictEqual ( {
94
95
X : 'setup'
95
96
} )
96
97
} )
97
98
98
99
test ( 'export { x }' , ( ) => {
99
- const { code , bindings } = compile (
100
+ const { content , bindings } = compile (
100
101
`<script setup>
101
102
const x = 1
102
103
const y = 2
103
104
export { x, y }
104
105
</script>`
105
106
)
106
- assertCode ( code )
107
+ assertCode ( content )
107
108
expect ( bindings ) . toStrictEqual ( {
108
109
x : 'setup' ,
109
110
y : 'setup'
110
111
} )
111
112
} )
112
113
113
114
test ( `export { x } from './x'` , ( ) => {
114
- const { code , bindings } = compile (
115
+ const { content , bindings } = compile (
115
116
`<script setup>
116
117
export { x, y } from './x'
117
118
</script>`
118
119
)
119
- assertCode ( code )
120
+ assertCode ( content )
120
121
expect ( bindings ) . toStrictEqual ( {
121
122
x : 'setup' ,
122
123
y : 'setup'
123
124
} )
124
125
} )
125
126
126
127
test ( `export default from './x'` , ( ) => {
127
- const { code , bindings } = compile (
128
+ const { content , bindings } = compile (
128
129
`<script setup>
129
130
export default from './x'
130
131
</script>` ,
131
132
{
132
- parserPlugins : [ 'exportDefaultFrom' ]
133
+ babelParserPlugins : [ 'exportDefaultFrom' ]
133
134
}
134
135
)
135
- assertCode ( code )
136
+ assertCode ( content )
136
137
expect ( bindings ) . toStrictEqual ( { } )
137
138
} )
138
139
139
140
test ( `export { x as default }` , ( ) => {
140
- const { code , bindings } = compile (
141
+ const { content , bindings } = compile (
141
142
`<script setup>
142
143
import x from './x'
143
144
const y = 1
144
145
export { x as default, y }
145
146
</script>`
146
147
)
147
- assertCode ( code )
148
+ assertCode ( content )
148
149
expect ( bindings ) . toStrictEqual ( {
149
150
y : 'setup'
150
151
} )
151
152
} )
152
153
153
154
test ( `export { x as default } from './x'` , ( ) => {
154
- const { code , bindings } = compile (
155
+ const { content , bindings } = compile (
155
156
`<script setup>
156
157
export { x as default, y } from './x'
157
158
</script>`
158
159
)
159
- assertCode ( code )
160
+ assertCode ( content )
160
161
expect ( bindings ) . toStrictEqual ( {
161
162
y : 'setup'
162
163
} )
163
164
} )
164
165
165
166
test ( `export * from './x'` , ( ) => {
166
- const { code , bindings } = compile (
167
+ const { content , bindings } = compile (
167
168
`<script setup>
168
169
export * from './x'
169
170
export const y = 1
170
171
</script>`
171
172
)
172
- assertCode ( code )
173
+ assertCode ( content )
173
174
expect ( bindings ) . toStrictEqual ( {
174
175
y : 'setup'
175
176
// in this case we cannot extract bindings from ./x so it falls back
@@ -178,15 +179,15 @@ describe('SFC compile <script setup>', () => {
178
179
} )
179
180
180
181
test ( 'export default in <script setup>' , ( ) => {
181
- const { code , bindings } = compile (
182
+ const { content , bindings } = compile (
182
183
`<script setup>
183
184
export default {
184
185
props: ['foo']
185
186
}
186
187
export const y = 1
187
188
</script>`
188
189
)
189
- assertCode ( code )
190
+ assertCode ( content )
190
191
expect ( bindings ) . toStrictEqual ( {
191
192
y : 'setup'
192
193
} )
@@ -195,18 +196,18 @@ describe('SFC compile <script setup>', () => {
195
196
196
197
describe ( '<script setup lang="ts">' , ( ) => {
197
198
test ( 'hoist type declarations' , ( ) => {
198
- const { code , bindings } = compile ( `
199
+ const { content , bindings } = compile ( `
199
200
<script setup lang="ts">
200
201
export interface Foo {}
201
202
type Bar = {}
202
203
export const a = 1
203
204
</script>` )
204
- assertCode ( code )
205
+ assertCode ( content )
205
206
expect ( bindings ) . toStrictEqual ( { a : 'setup' } )
206
207
} )
207
208
208
209
test ( 'extract props' , ( ) => {
209
- const { code } = compile ( `
210
+ const { content } = compile ( `
210
211
<script setup="myProps" lang="ts">
211
212
interface Test {}
212
213
@@ -237,59 +238,57 @@ describe('SFC compile <script setup>', () => {
237
238
intersection: Test & {}
238
239
}
239
240
</script>` )
240
- assertCode ( code )
241
- expect ( code ) . toMatch ( `string: { type: String, required: true }` )
242
- expect ( code ) . toMatch ( `number: { type: Number, required: true }` )
243
- expect ( code ) . toMatch ( `boolean: { type: Boolean, required: true }` )
244
- expect ( code ) . toMatch ( `object: { type: Object, required: true }` )
245
- expect ( code ) . toMatch ( `objectLiteral: { type: Object, required: true }` )
246
- expect ( code ) . toMatch ( `fn: { type: Function, required: true }` )
247
- expect ( code ) . toMatch ( `functionRef: { type: Function, required: true }` )
248
- expect ( code ) . toMatch ( `objectRef: { type: Object, required: true }` )
249
- expect ( code ) . toMatch ( `array: { type: Array, required: true }` )
250
- expect ( code ) . toMatch ( `arrayRef: { type: Array, required: true }` )
251
- expect ( code ) . toMatch ( `tuple: { type: Array, required: true }` )
252
- expect ( code ) . toMatch ( `set: { type: Set, required: true }` )
253
- expect ( code ) . toMatch ( `literal: { type: String, required: true }` )
254
- expect ( code ) . toMatch ( `optional: { type: null, required: false }` )
255
- expect ( code ) . toMatch ( `recordRef: { type: Object, required: true }` )
256
- expect ( code ) . toMatch ( `interface: { type: Object, required: true }` )
257
- expect ( code ) . toMatch ( `alias: { type: Array, required: true }` )
258
- expect ( code ) . toMatch ( `union: { type: [String, Number], required: true }` )
259
- expect ( code ) . toMatch (
241
+ assertCode ( content )
242
+ expect ( content ) . toMatch ( `string: { type: String, required: true }` )
243
+ expect ( content ) . toMatch ( `number: { type: Number, required: true }` )
244
+ expect ( content ) . toMatch ( `boolean: { type: Boolean, required: true }` )
245
+ expect ( content ) . toMatch ( `object: { type: Object, required: true }` )
246
+ expect ( content ) . toMatch ( `objectLiteral: { type: Object, required: true }` )
247
+ expect ( content ) . toMatch ( `fn: { type: Function, required: true }` )
248
+ expect ( content ) . toMatch ( `functionRef: { type: Function, required: true }` )
249
+ expect ( content ) . toMatch ( `objectRef: { type: Object, required: true }` )
250
+ expect ( content ) . toMatch ( `array: { type: Array, required: true }` )
251
+ expect ( content ) . toMatch ( `arrayRef: { type: Array, required: true }` )
252
+ expect ( content ) . toMatch ( `tuple: { type: Array, required: true }` )
253
+ expect ( content ) . toMatch ( `set: { type: Set, required: true }` )
254
+ expect ( content ) . toMatch ( `literal: { type: String, required: true }` )
255
+ expect ( content ) . toMatch ( `optional: { type: null, required: false }` )
256
+ expect ( content ) . toMatch ( `recordRef: { type: Object, required: true }` )
257
+ expect ( content ) . toMatch ( `interface: { type: Object, required: true }` )
258
+ expect ( content ) . toMatch ( `alias: { type: Array, required: true }` )
259
+ expect ( content ) . toMatch (
260
+ `union: { type: [String, Number], required: true }`
261
+ )
262
+ expect ( content ) . toMatch (
260
263
`literalUnion: { type: [String, String], required: true }`
261
264
)
262
- expect ( code ) . toMatch (
265
+ expect ( content ) . toMatch (
263
266
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`
264
267
)
265
- expect ( code ) . toMatch ( `intersection: { type: Object, required: true }` )
268
+ expect ( content ) . toMatch ( `intersection: { type: Object, required: true }` )
266
269
} )
267
270
268
271
test ( 'extract emits' , ( ) => {
269
- const { code } = compile ( `
272
+ const { content } = compile ( `
270
273
<script setup="_, { emit: myEmit }" lang="ts">
271
274
declare function myEmit(e: 'foo' | 'bar'): void
272
275
declare function myEmit(e: 'baz', id: number): void
273
276
</script>
274
277
` )
275
- assertCode ( code )
276
- expect ( code ) . toMatch ( `declare function __emit__(e: 'foo' | 'bar'): void` )
277
- expect ( code ) . toMatch (
278
+ assertCode ( content )
279
+ expect ( content ) . toMatch (
280
+ `declare function __emit__(e: 'foo' | 'bar'): void`
281
+ )
282
+ expect ( content ) . toMatch (
278
283
`declare function __emit__(e: 'baz', id: number): void`
279
284
)
280
- expect ( code ) . toMatch (
285
+ expect ( content ) . toMatch (
281
286
`emits: ["foo", "bar", "baz"] as unknown as undefined`
282
287
)
283
288
} )
284
289
} )
285
290
286
291
describe ( 'errors' , ( ) => {
287
- test ( 'must have <script setup>' , ( ) => {
288
- expect ( ( ) => compile ( `<script>foo()</script>` ) ) . toThrow (
289
- `SFC has no <script setup>`
290
- )
291
- } )
292
-
293
292
test ( '<script> and <script setup> must have same lang' , ( ) => {
294
293
expect ( ( ) =>
295
294
compile ( `<script>foo()</script><script setup lang="ts">bar()</script>` )
@@ -342,7 +341,7 @@ describe('SFC compile <script setup>', () => {
342
341
}
343
342
}
344
343
}
345
- </script>` ) . code
344
+ </script>` ) . content
346
345
)
347
346
} )
348
347
@@ -358,7 +357,7 @@ describe('SFC compile <script setup>', () => {
358
357
}
359
358
}
360
359
}
361
- </script>` ) . code
360
+ </script>` ) . content
362
361
)
363
362
} )
364
363
@@ -373,7 +372,7 @@ describe('SFC compile <script setup>', () => {
373
372
}
374
373
}
375
374
}
376
- </script>` ) . code
375
+ </script>` ) . content
377
376
)
378
377
} )
379
378
0 commit comments