1
1
<script setup lang="ts">
2
2
import Header from ' ./Header.vue'
3
- import { Repl , ReplStore , SFCOptions } from ' @vue/repl'
4
- import type Monaco from ' @vue/repl/monaco-editor'
5
- import type CodeMirror from ' @vue/repl/codemirror-editor'
6
- import { ref , watchEffect , onMounted } from ' vue'
7
- import { shallowRef } from ' vue'
8
-
9
- const EditorComponent = shallowRef <typeof Monaco | typeof CodeMirror >()
10
-
11
- if (import .meta .env .DEV ) {
12
- import (' @vue/repl/codemirror-editor' ).then (
13
- mod => (EditorComponent .value = mod .default ),
14
- )
15
- } else {
16
- import (' @vue/repl/monaco-editor' ).then (
17
- mod => (EditorComponent .value = mod .default ),
18
- )
19
- }
3
+ import { Repl , useStore , SFCOptions , useVueImportMap } from ' @vue/repl'
4
+ import Monaco from ' @vue/repl/monaco-editor'
5
+ import { ref , watchEffect , onMounted , computed } from ' vue'
20
6
21
7
const replRef = ref <InstanceType <typeof Repl >>()
22
8
@@ -26,78 +12,80 @@ const setVH = () => {
26
12
window .addEventListener (' resize' , setVH )
27
13
setVH ()
28
14
29
- const useProdMode = ref (false )
30
15
const useSSRMode = ref (false )
31
16
17
+ const { productionMode, vueVersion, importMap } = useVueImportMap ({
18
+ runtimeDev: import .meta .env .PROD
19
+ ? ` ${location .origin }/vue.runtime.esm-browser.js `
20
+ : ` ${location .origin }/src/vue-dev-proxy ` ,
21
+ runtimeProd: import .meta .env .PROD
22
+ ? ` ${location .origin }/vue.runtime.esm-browser.prod.js `
23
+ : ` ${location .origin }/src/vue-dev-proxy-prod ` ,
24
+ serverRenderer: import .meta .env .PROD
25
+ ? ` ${location .origin }/server-renderer.esm-browser.js `
26
+ : ` ${location .origin }/src/vue-server-renderer-dev-proxy ` ,
27
+ })
28
+
32
29
let hash = location .hash .slice (1 )
33
30
if (hash .startsWith (' __DEV__' )) {
34
31
hash = hash .slice (7 )
35
- useProdMode .value = false
32
+ productionMode .value = false
36
33
}
37
34
if (hash .startsWith (' __PROD__' )) {
38
35
hash = hash .slice (8 )
39
- useProdMode .value = true
36
+ productionMode .value = true
40
37
}
41
38
if (hash .startsWith (' __SSR__' )) {
42
39
hash = hash .slice (7 )
43
40
useSSRMode .value = true
44
41
}
45
42
46
- const store = new ReplStore ({
47
- serializedState: hash ,
48
- productionMode: useProdMode .value ,
49
- defaultVueRuntimeURL: import .meta .env .PROD
50
- ? ` ${location .origin }/vue.runtime.esm-browser.js `
51
- : ` ${location .origin }/src/vue-dev-proxy ` ,
52
- defaultVueRuntimeProdURL: import .meta .env .PROD
53
- ? ` ${location .origin }/vue.runtime.esm-browser.prod.js `
54
- : ` ${location .origin }/src/vue-dev-proxy-prod ` ,
55
- defaultVueServerRendererURL: import .meta .env .PROD
56
- ? ` ${location .origin }/server-renderer.esm-browser.js `
57
- : ` ${location .origin }/src/vue-server-renderer-dev-proxy ` ,
58
- })
59
-
60
43
// enable experimental features
61
- const sfcOptions: SFCOptions = {
62
- script: {
63
- inlineTemplate: useProdMode .value ,
64
- isProd: useProdMode .value ,
65
- propsDestructure: true ,
66
- },
67
- style: {
68
- isProd: useProdMode .value ,
69
- },
70
- template: {
71
- isProd: useProdMode .value ,
72
- compilerOptions: {
73
- isCustomElement : (tag : string ) => tag === ' mjx-container' ,
44
+ const sfcOptions = computed (
45
+ (): SFCOptions => ({
46
+ script: {
47
+ inlineTemplate: productionMode .value ,
48
+ isProd: productionMode .value ,
49
+ propsDestructure: true ,
50
+ },
51
+ style: {
52
+ isProd: productionMode .value ,
53
+ },
54
+ template: {
55
+ isProd: productionMode .value ,
56
+ compilerOptions: {
57
+ isCustomElement : (tag : string ) => tag === ' mjx-container' ,
58
+ },
74
59
},
60
+ }),
61
+ )
62
+
63
+ const store = useStore (
64
+ {
65
+ builtinImportMap: importMap ,
66
+ vueVersion ,
67
+ sfcOptions ,
75
68
},
76
- }
69
+ hash ,
70
+ )
71
+ // @ts-expect-error
72
+ globalThis .store = store
77
73
78
74
// persist state
79
75
watchEffect (() => {
80
76
const newHash = store
81
77
.serialize ()
82
78
.replace (/ ^ #/ , useSSRMode .value ? ` #__SSR__ ` : ` # ` )
83
- .replace (/ ^ #/ , useProdMode .value ? ` #__PROD__ ` : ` # ` )
79
+ .replace (/ ^ #/ , productionMode .value ? ` #__PROD__ ` : ` # ` )
84
80
history .replaceState ({}, ' ' , newHash )
85
81
})
86
82
87
83
function toggleProdMode() {
88
- const isProd = (useProdMode .value = ! useProdMode .value )
89
- sfcOptions .script ! .inlineTemplate =
90
- sfcOptions .script ! .isProd =
91
- sfcOptions .template ! .isProd =
92
- sfcOptions .style ! .isProd =
93
- isProd
94
- store .toggleProduction ()
95
- store .setFiles (store .getFiles ())
84
+ productionMode .value = ! productionMode .value
96
85
}
97
86
98
87
function toggleSSR() {
99
88
useSSRMode .value = ! useSSRMode .value
100
- store .setFiles (store .getFiles ())
101
89
}
102
90
103
91
function reloadPage() {
@@ -111,31 +99,32 @@ function toggleTheme(isDark: boolean) {
111
99
onMounted (() => {
112
100
const cls = document .documentElement .classList
113
101
toggleTheme (cls .contains (' dark' ))
102
+
103
+ // @ts-expect-error process shim for old versions of @vue/compiler-sfc dependency
104
+ window .process = { env: {} }
114
105
})
115
106
</script >
116
107
117
108
<template >
118
109
<Header
119
110
:store =" store"
120
- :prod =" useProdMode "
111
+ :prod =" productionMode "
121
112
:ssr =" useSSRMode"
122
113
@toggle-theme =" toggleTheme"
123
114
@toggle-prod =" toggleProdMode"
124
115
@toggle-ssr =" toggleSSR"
125
116
@reload-page =" reloadPage"
126
117
/>
127
118
<Repl
128
- v-if =" EditorComponent"
129
119
ref =" replRef"
130
120
:theme =" theme"
131
- :editor =" EditorComponent "
121
+ :editor =" Monaco "
132
122
@keydown.ctrl.s.prevent
133
123
@keydown.meta.s.prevent
134
124
:ssr =" useSSRMode"
135
125
:store =" store"
136
126
:showCompileOutput =" true"
137
127
:autoResize =" true"
138
- :sfcOptions =" sfcOptions"
139
128
:clearConsole =" false"
140
129
:preview-options =" {
141
130
customCode: {
0 commit comments