1
1
import { store , File } from './store'
2
- import {
3
- parse ,
4
- compileTemplate ,
5
- compileStyleAsync ,
6
- compileScript ,
7
- rewriteDefault ,
8
- SFCDescriptor ,
9
- BindingMetadata
10
- } from '@vue/compiler-sfc'
2
+ import { SFCDescriptor , BindingMetadata } from '@vue/compiler-sfc'
3
+ import * as defaultCompiler from '@vue/compiler-sfc'
11
4
12
5
export const MAIN_FILE = 'App.vue'
13
6
export const COMP_IDENTIFIER = `__sfc__`
14
7
8
+ /**
9
+ * The default SFC compiler we are using is built from each commit
10
+ * but we may swap it out with a version fetched from CDNs
11
+ */
12
+ let SFCCompiler : typeof defaultCompiler = defaultCompiler
13
+
15
14
// @ts -ignore
16
- export const SANDBOX_VUE_URL = import . meta. env . PROD
15
+ const defaultVueUrl = import . meta. env . PROD
17
16
? '/vue.runtime.esm-browser.js' // to be copied on build
18
17
: '/src/vue-dev-proxy'
19
18
19
+ export let SANDBOX_VUE_URL = defaultVueUrl
20
+
21
+ export async function setVersion ( version : string ) {
22
+ const compilerUrl = `https://unpkg.com/@vue/compiler-sfc@${ version } /dist/compiler-sfc.esm-browser.js`
23
+ const runtimeUrl = `https://cdn.skypack.dev/@vue/runtime-dom@${ version } `
24
+ const [ compiler ] = await Promise . all ( [
25
+ import ( /* @vite -ignore */ compilerUrl ) ,
26
+ import ( /* @vite -ignore */ runtimeUrl )
27
+ ] )
28
+ SFCCompiler = compiler
29
+ SANDBOX_VUE_URL = runtimeUrl
30
+ console . info ( `Now using Vue version: ${ version } ` )
31
+ }
32
+
33
+ export function resetVersion ( ) {
34
+ SFCCompiler = defaultCompiler
35
+ SANDBOX_VUE_URL = defaultVueUrl
36
+ }
37
+
20
38
export async function compileFile ( { filename, code, compiled } : File ) {
21
39
if ( ! code . trim ( ) ) {
22
40
store . errors = [ ]
@@ -30,7 +48,10 @@ export async function compileFile({ filename, code, compiled }: File) {
30
48
}
31
49
32
50
const id = await hashId ( filename )
33
- const { errors, descriptor } = parse ( code , { filename, sourceMap : true } )
51
+ const { errors, descriptor } = SFCCompiler . parse ( code , {
52
+ filename,
53
+ sourceMap : true
54
+ } )
34
55
if ( errors . length ) {
35
56
store . errors = errors
36
57
return
@@ -121,7 +142,7 @@ export async function compileFile({ filename, code, compiled }: File) {
121
142
return
122
143
}
123
144
124
- const styleResult = await compileStyleAsync ( {
145
+ const styleResult = await SFCCompiler . compileStyleAsync ( {
125
146
source : style . content ,
126
147
filename,
127
148
id,
@@ -156,7 +177,7 @@ function doCompileScript(
156
177
) : [ string , BindingMetadata | undefined ] | undefined {
157
178
if ( descriptor . script || descriptor . scriptSetup ) {
158
179
try {
159
- const compiledScript = compileScript ( descriptor , {
180
+ const compiledScript = SFCCompiler . compileScript ( descriptor , {
160
181
id,
161
182
refSugar : true ,
162
183
inlineTemplate : true ,
@@ -173,7 +194,9 @@ function doCompileScript(
173
194
2
174
195
) } */`
175
196
}
176
- code += `\n` + rewriteDefault ( compiledScript . content , COMP_IDENTIFIER )
197
+ code +=
198
+ `\n` +
199
+ SFCCompiler . rewriteDefault ( compiledScript . content , COMP_IDENTIFIER )
177
200
return [ code , compiledScript . bindings ]
178
201
} catch ( e ) {
179
202
store . errors = [ e ]
@@ -190,7 +213,7 @@ function doCompileTemplate(
190
213
bindingMetadata : BindingMetadata | undefined ,
191
214
ssr : boolean
192
215
) {
193
- const templateResult = compileTemplate ( {
216
+ const templateResult = SFCCompiler . compileTemplate ( {
194
217
source : descriptor . template ! . content ,
195
218
filename : descriptor . filename ,
196
219
id,
0 commit comments