|
1 | 1 | <template>
|
2 |
| - <iframe |
3 |
| - id="preview" |
4 |
| - ref="iframe" |
5 |
| - sandbox="allow-forms allow-modals allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-top-navigation-by-user-activation" |
6 |
| - :srcdoc="srcdoc" |
7 |
| - ></iframe> |
| 2 | + <div class="preview-container" ref="container"> |
| 3 | +</div> |
8 | 4 | <Message :err="runtimeError" />
|
9 | 5 | <Message v-if="!runtimeError" :warn="runtimeWarning" />
|
10 | 6 | </template>
|
11 | 7 |
|
12 | 8 | <script setup lang="ts">
|
13 | 9 | import Message from '../Message.vue'
|
14 |
| -import { ref, onMounted, onUnmounted, watchEffect } from 'vue' |
| 10 | +import { ref, onMounted, onUnmounted, watchEffect, watch } from 'vue' |
15 | 11 | import type { WatchStopHandle } from 'vue'
|
16 | 12 | import srcdoc from './srcdoc.html?raw'
|
17 | 13 | import { PreviewProxy } from './PreviewProxy'
|
18 |
| -import { MAIN_FILE, SANDBOX_VUE_URL } from '../sfcCompiler' |
| 14 | +import { MAIN_FILE, vueRuntimeUrl } from '../sfcCompiler' |
19 | 15 | import { compileModulesForPreview } from './moduleCompiler'
|
| 16 | +import { store } from '../store' |
20 | 17 |
|
21 |
| -const iframe = ref() |
| 18 | +const container = ref() |
22 | 19 | const runtimeError = ref()
|
23 | 20 | const runtimeWarning = ref()
|
24 | 21 |
|
| 22 | +let sandbox: HTMLIFrameElement |
25 | 23 | let proxy: PreviewProxy
|
26 |
| -let updateHandle: WatchStopHandle |
| 24 | +let stopUpdateWatcher: WatchStopHandle |
27 | 25 |
|
28 |
| -async function updatePreview() { |
29 |
| - runtimeError.value = null |
30 |
| - runtimeWarning.value = null |
| 26 | +// create sandbox on mount |
| 27 | +onMounted(createSandbox) |
| 28 | +
|
| 29 | +// reset sandbox when import map changes |
| 30 | +watch(() => store.importMap, (importMap, prev) => { |
| 31 | + if (!importMap) { |
| 32 | + if (prev) { |
| 33 | + // import-map.json deleted |
| 34 | + createSandbox() |
| 35 | + } |
| 36 | + return |
| 37 | + } |
31 | 38 | try {
|
32 |
| - const modules = compileModulesForPreview() |
33 |
| - console.log(`successfully compiled ${modules.length} modules.`) |
34 |
| - // reset modules |
35 |
| - await proxy.eval([ |
36 |
| - `window.__modules__ = {};window.__css__ = ''`, |
37 |
| - ...modules, |
38 |
| - ` |
39 |
| -import { createApp as _createApp } from "${SANDBOX_VUE_URL}" |
| 39 | + const map = JSON.parse(importMap) |
| 40 | + if (!map.imports) { |
| 41 | + store.errors = [ |
| 42 | + `import-map.json is missing "imports" field.` |
| 43 | + ] |
| 44 | + return |
| 45 | + } |
| 46 | + if (map.imports.vue) { |
| 47 | + store.errors = [ |
| 48 | + 'Select Vue versions using the top-right dropdown.\n' + |
| 49 | + 'Specifying it in the import map has no effect.' |
| 50 | + ] |
| 51 | + } |
| 52 | + createSandbox() |
| 53 | + } catch (e) { |
| 54 | + store.errors = [e] |
| 55 | + return |
| 56 | + } |
| 57 | +}) |
40 | 58 |
|
41 |
| -if (window.__app__) { |
42 |
| - window.__app__.unmount() |
43 |
| - document.getElementById('app').innerHTML = '' |
44 |
| -} |
| 59 | +// reset sandbox when version changes |
| 60 | +watch(vueRuntimeUrl, createSandbox) |
45 | 61 |
|
46 |
| -document.getElementById('__sfc-styles').innerHTML = window.__css__ |
47 |
| -const app = window.__app__ = _createApp(__modules__["${MAIN_FILE}"].default) |
48 |
| -app.config.errorHandler = e => console.error(e) |
49 |
| -app.mount('#app')`.trim() |
50 |
| - ]) |
| 62 | +onUnmounted(() => { |
| 63 | + proxy.destroy() |
| 64 | + stopUpdateWatcher && stopUpdateWatcher() |
| 65 | +}) |
| 66 | +
|
| 67 | +function createSandbox() { |
| 68 | + if (sandbox) { |
| 69 | + // clear prev sandbox |
| 70 | + proxy.destroy() |
| 71 | + stopUpdateWatcher() |
| 72 | + container.value.removeChild(sandbox) |
| 73 | + } |
| 74 | +
|
| 75 | + sandbox = document.createElement('iframe') |
| 76 | + sandbox.setAttribute('sandbox', [ |
| 77 | + 'allow-forms', |
| 78 | + 'allow-modals', |
| 79 | + 'allow-pointer-lock', |
| 80 | + 'allow-popups', |
| 81 | + 'allow-same-origin', |
| 82 | + 'allow-scripts', |
| 83 | + 'allow-top-navigation-by-user-activation' |
| 84 | + ].join(' ')) |
| 85 | +
|
| 86 | + let importMap: Record<string, any> |
| 87 | + try { |
| 88 | + importMap = JSON.parse(store.importMap || `{}`) |
51 | 89 | } catch (e) {
|
52 |
| - runtimeError.value = e.stack |
| 90 | + store.errors = [`Syntax error in import-map.json: ${e.message}`] |
| 91 | + return |
53 | 92 | }
|
54 |
| -} |
55 | 93 |
|
56 |
| -onMounted(() => { |
57 |
| - proxy = new PreviewProxy(iframe.value, { |
| 94 | + if (!importMap.imports) { |
| 95 | + importMap.imports = {} |
| 96 | + } |
| 97 | + importMap.imports.vue = vueRuntimeUrl.value |
| 98 | + const sandboxSrc = srcdoc.replace(/<!--IMPORT_MAP-->/, JSON.stringify(importMap)) |
| 99 | + sandbox.setAttribute('srcdoc', sandboxSrc) |
| 100 | + container.value.appendChild(sandbox) |
| 101 | +
|
| 102 | + proxy = new PreviewProxy(sandbox, { |
58 | 103 | on_fetch_progress: (progress: any) => {
|
59 | 104 | // pending_imports = progress;
|
60 | 105 | },
|
@@ -93,19 +138,43 @@ onMounted(() => {
|
93 | 138 | }
|
94 | 139 | })
|
95 | 140 |
|
96 |
| - iframe.value.addEventListener('load', () => { |
| 141 | + sandbox.addEventListener('load', () => { |
97 | 142 | proxy.handle_links()
|
98 |
| - updateHandle = watchEffect(updatePreview) |
| 143 | + stopUpdateWatcher = watchEffect(updatePreview) |
99 | 144 | })
|
100 |
| -}) |
| 145 | +} |
101 | 146 |
|
102 |
| -onUnmounted(() => { |
103 |
| - proxy.destroy() |
104 |
| - updateHandle && updateHandle() |
105 |
| -}) |
| 147 | +async function updatePreview() { |
| 148 | + runtimeError.value = null |
| 149 | + runtimeWarning.value = null |
| 150 | + try { |
| 151 | + const modules = compileModulesForPreview() |
| 152 | + console.log(`successfully compiled ${modules.length} modules.`) |
| 153 | + // reset modules |
| 154 | + await proxy.eval([ |
| 155 | + `window.__modules__ = {};window.__css__ = ''`, |
| 156 | + ...modules, |
| 157 | + ` |
| 158 | +import { createApp as _createApp } from "vue" |
| 159 | +
|
| 160 | +if (window.__app__) { |
| 161 | + window.__app__.unmount() |
| 162 | + document.getElementById('app').innerHTML = '' |
| 163 | +} |
| 164 | +
|
| 165 | +document.getElementById('__sfc-styles').innerHTML = window.__css__ |
| 166 | +const app = window.__app__ = _createApp(__modules__["${MAIN_FILE}"].default) |
| 167 | +app.config.errorHandler = e => console.error(e) |
| 168 | +app.mount('#app')`.trim() |
| 169 | + ]) |
| 170 | + } catch (e) { |
| 171 | + runtimeError.value = e.stack |
| 172 | + } |
| 173 | +} |
106 | 174 | </script>
|
107 | 175 |
|
108 | 176 | <style>
|
| 177 | +.preview-container, |
109 | 178 | iframe {
|
110 | 179 | width: 100%;
|
111 | 180 | height: 100%;
|
|
0 commit comments