@@ -19,25 +19,22 @@ async function transformTS(src: string) {
19
19
export async function compileFile (
20
20
store : Store ,
21
21
{ filename, code, compiled } : File
22
- ) {
22
+ ) : Promise < ( string | Error ) [ ] > {
23
23
if ( ! code . trim ( ) ) {
24
- store . state . errors = [ ]
25
- return
24
+ return [ ]
26
25
}
27
26
28
27
if ( filename . endsWith ( '.css' ) ) {
29
28
compiled . css = code
30
- store . state . errors = [ ]
31
- return
29
+ return [ ]
32
30
}
33
31
34
32
if ( filename . endsWith ( '.js' ) || filename . endsWith ( '.ts' ) ) {
35
33
if ( filename . endsWith ( '.ts' ) ) {
36
34
code = await transformTS ( code )
37
35
}
38
36
compiled . js = compiled . ssr = code
39
- store . state . errors = [ ]
40
- return
37
+ return [ ]
41
38
}
42
39
43
40
if ( filename . endsWith ( '.json' ) ) {
@@ -46,17 +43,14 @@ export async function compileFile(
46
43
parsed = JSON . parse ( code )
47
44
} catch ( err : any ) {
48
45
console . error ( `Error parsing ${ filename } ` , err . message )
49
- store . state . errors = [ err . message ]
50
- return
46
+ return [ err . message ]
51
47
}
52
48
compiled . js = compiled . ssr = `export default ${ JSON . stringify ( parsed ) } `
53
- store . state . errors = [ ]
54
- return
49
+ return [ ]
55
50
}
56
51
57
52
if ( ! filename . endsWith ( '.vue' ) ) {
58
- store . state . errors = [ ]
59
- return
53
+ return [ ]
60
54
}
61
55
62
56
const id = hashId ( filename )
@@ -65,28 +59,25 @@ export async function compileFile(
65
59
sourceMap : true ,
66
60
} )
67
61
if ( errors . length ) {
68
- store . state . errors = errors
69
- return
62
+ return errors
70
63
}
71
64
72
65
if (
73
66
descriptor . styles . some ( ( s ) => s . lang ) ||
74
67
( descriptor . template && descriptor . template . lang )
75
68
) {
76
- store . state . errors = [
69
+ return [
77
70
`lang="x" pre-processors for <template> or <style> are currently not ` +
78
71
`supported.` ,
79
72
]
80
- return
81
73
}
82
74
83
75
const scriptLang =
84
76
( descriptor . script && descriptor . script . lang ) ||
85
77
( descriptor . scriptSetup && descriptor . scriptSetup . lang )
86
78
const isTS = scriptLang === 'ts'
87
79
if ( scriptLang && ! isTS ) {
88
- store . state . errors = [ `Only lang="ts" is supported for <script> blocks.` ]
89
- return
80
+ return [ `Only lang="ts" is supported for <script> blocks.` ]
90
81
}
91
82
92
83
const hasScoped = descriptor . styles . some ( ( s ) => s . scoped )
@@ -98,34 +89,37 @@ export async function compileFile(
98
89
ssrCode += code
99
90
}
100
91
101
- const clientScriptResult = await doCompileScript (
102
- store ,
103
- descriptor ,
104
- id ,
105
- false ,
106
- isTS
107
- )
108
- if ( ! clientScriptResult ) {
109
- return
92
+ let clientScript : string
93
+ let bindings : BindingMetadata | undefined
94
+ try {
95
+ ; [ clientScript , bindings ] = await doCompileScript (
96
+ store ,
97
+ descriptor ,
98
+ id ,
99
+ false ,
100
+ isTS
101
+ )
102
+ } catch ( e : any ) {
103
+ return [ e . stack . split ( '\n' ) . slice ( 0 , 12 ) . join ( '\n' ) ]
110
104
}
111
- const [ clientScript , bindings ] = clientScriptResult
105
+
112
106
clientCode += clientScript
113
107
114
108
// script ssr needs to be performed if :
115
109
// 1.using <script setup> where the render fn is inlined.
116
110
// 2.using cssVars, as it do not need to be injected during SSR.
117
111
if ( descriptor . scriptSetup || descriptor . cssVars . length > 0 ) {
118
- const ssrScriptResult = await doCompileScript (
119
- store ,
120
- descriptor ,
121
- id ,
122
- true ,
123
- isTS
124
- )
125
- if ( ssrScriptResult ) {
112
+ try {
113
+ const ssrScriptResult = await doCompileScript (
114
+ store ,
115
+ descriptor ,
116
+ id ,
117
+ true ,
118
+ isTS
119
+ )
126
120
ssrCode += ssrScriptResult [ 0 ]
127
- } else {
128
- ssrCode = `/* SSR compile error: ${ store . state . errors [ 0 ] } */`
121
+ } catch ( e ) {
122
+ ssrCode = `/* SSR compile error: ${ e } */`
129
123
}
130
124
} else {
131
125
// the script result will be identical.
@@ -146,8 +140,8 @@ export async function compileFile(
146
140
false ,
147
141
isTS
148
142
)
149
- if ( ! clientTemplateResult ) {
150
- return
143
+ if ( Array . isArray ( clientTemplateResult ) ) {
144
+ return clientTemplateResult
151
145
}
152
146
clientCode += `;${ clientTemplateResult } `
153
147
@@ -159,11 +153,11 @@ export async function compileFile(
159
153
true ,
160
154
isTS
161
155
)
162
- if ( ssrTemplateResult ) {
156
+ if ( typeof ssrTemplateResult === 'string' ) {
163
157
// ssr compile failure is fine
164
158
ssrCode += `;${ ssrTemplateResult } `
165
159
} else {
166
- ssrCode = `/* SSR compile error: ${ store . state . errors [ 0 ] } */`
160
+ ssrCode = `/* SSR compile error: ${ ssrTemplateResult [ 0 ] } */`
167
161
}
168
162
}
169
163
@@ -186,10 +180,7 @@ export async function compileFile(
186
180
let css = ''
187
181
for ( const style of descriptor . styles ) {
188
182
if ( style . module ) {
189
- store . state . errors = [
190
- `<style module> is not supported in the playground.` ,
191
- ]
192
- return
183
+ return [ `<style module> is not supported in the playground.` ]
193
184
}
194
185
195
186
const styleResult = await store . compiler . compileStyleAsync ( {
@@ -217,8 +208,7 @@ export async function compileFile(
217
208
compiled . css = '/* No <style> tags present */'
218
209
}
219
210
220
- // clear errors
221
- store . state . errors = [ ]
211
+ return [ ]
222
212
}
223
213
224
214
async function doCompileScript (
@@ -227,51 +217,46 @@ async function doCompileScript(
227
217
id : string ,
228
218
ssr : boolean ,
229
219
isTS : boolean
230
- ) : Promise < [ string , BindingMetadata | undefined ] | undefined > {
220
+ ) : Promise < [ code : string , bindings : BindingMetadata | undefined ] > {
231
221
if ( descriptor . script || descriptor . scriptSetup ) {
232
- try {
233
- const expressionPlugins : CompilerOptions [ 'expressionPlugins' ] = isTS
234
- ? [ 'typescript' ]
235
- : undefined
236
- const compiledScript = store . compiler . compileScript ( descriptor , {
237
- inlineTemplate : true ,
238
- ...store . options ?. script ,
239
- id,
240
- templateOptions : {
241
- ...store . options ?. template ,
242
- ssr,
243
- ssrCssVars : descriptor . cssVars ,
244
- compilerOptions : {
245
- ...store . options ?. template ?. compilerOptions ,
246
- expressionPlugins,
247
- } ,
222
+ const expressionPlugins : CompilerOptions [ 'expressionPlugins' ] = isTS
223
+ ? [ 'typescript' ]
224
+ : undefined
225
+ const compiledScript = store . compiler . compileScript ( descriptor , {
226
+ inlineTemplate : true ,
227
+ ...store . options ?. script ,
228
+ id,
229
+ templateOptions : {
230
+ ...store . options ?. template ,
231
+ ssr,
232
+ ssrCssVars : descriptor . cssVars ,
233
+ compilerOptions : {
234
+ ...store . options ?. template ?. compilerOptions ,
235
+ expressionPlugins,
248
236
} ,
249
- } )
250
- let code = ''
251
- if ( compiledScript . bindings ) {
252
- code += `\n/* Analyzed bindings: ${ JSON . stringify (
253
- compiledScript . bindings ,
254
- null ,
255
- 2
256
- ) } */`
257
- }
258
- code +=
259
- `\n` +
260
- store . compiler . rewriteDefault (
261
- compiledScript . content ,
262
- COMP_IDENTIFIER ,
263
- expressionPlugins
264
- )
265
-
266
- if ( ( descriptor . script || descriptor . scriptSetup ) ! . lang === 'ts' ) {
267
- code = await transformTS ( code )
268
- }
237
+ } ,
238
+ } )
239
+ let code = ''
240
+ if ( compiledScript . bindings ) {
241
+ code += `\n/* Analyzed bindings: ${ JSON . stringify (
242
+ compiledScript . bindings ,
243
+ null ,
244
+ 2
245
+ ) } */`
246
+ }
247
+ code +=
248
+ `\n` +
249
+ store . compiler . rewriteDefault (
250
+ compiledScript . content ,
251
+ COMP_IDENTIFIER ,
252
+ expressionPlugins
253
+ )
269
254
270
- return [ code , compiledScript . bindings ]
271
- } catch ( e : any ) {
272
- store . state . errors = [ e . stack . split ( '\n' ) . slice ( 0 , 12 ) . join ( '\n' ) ]
273
- return
255
+ if ( ( descriptor . script || descriptor . scriptSetup ) ! . lang === 'ts' ) {
256
+ code = await transformTS ( code )
274
257
}
258
+
259
+ return [ code , compiledScript . bindings ]
275
260
} else {
276
261
return [ `\nconst ${ COMP_IDENTIFIER } = {}` , undefined ]
277
262
}
@@ -285,7 +270,7 @@ async function doCompileTemplate(
285
270
ssr : boolean ,
286
271
isTS : boolean
287
272
) {
288
- const templateResult = store . compiler . compileTemplate ( {
273
+ let { code , errors } = store . compiler . compileTemplate ( {
289
274
isProd : false ,
290
275
...store . options ?. template ,
291
276
source : descriptor . template ! . content ,
@@ -301,15 +286,14 @@ async function doCompileTemplate(
301
286
expressionPlugins : isTS ? [ 'typescript' ] : undefined ,
302
287
} ,
303
288
} )
304
- if ( templateResult . errors . length ) {
305
- store . state . errors = templateResult . errors
306
- return
289
+ if ( errors . length ) {
290
+ return errors
307
291
}
308
292
309
293
const fnName = ssr ? `ssrRender` : `render`
310
294
311
- let code =
312
- `\n${ templateResult . code . replace (
295
+ code =
296
+ `\n${ code . replace (
313
297
/ \n e x p o r t ( f u n c t i o n | c o n s t ) ( r e n d e r | s s r R e n d e r ) / ,
314
298
`$1 ${ fnName } `
315
299
) } ` + `\n${ COMP_IDENTIFIER } .${ fnName } = ${ fnName } `
0 commit comments