diff --git a/packages/sfc-playground/src/output/Preview.vue b/packages/sfc-playground/src/output/Preview.vue index 52b35c1f..4c5a41c8 100644 --- a/packages/sfc-playground/src/output/Preview.vue +++ b/packages/sfc-playground/src/output/Preview.vue @@ -96,7 +96,7 @@ function createSandbox() { } importMap.imports.vue = vueRuntimeUrl.value const sandboxSrc = srcdoc.replace(//, JSON.stringify(importMap)) - sandbox.setAttribute('srcdoc', sandboxSrc) + sandbox.srcdoc = sandboxSrc container.value.appendChild(sandbox) proxy = new PreviewProxy(sandbox, { @@ -104,19 +104,30 @@ function createSandbox() { // pending_imports = progress; }, on_error: (event: any) => { - runtimeError.value = event.value + const msg = event.value instanceof Error ? event.value.message : event.value + if ( + msg.includes('Failed to resolve module specifier') || + msg.includes('Error resolving module specifier') + ) { + runtimeError.value = msg.replace(/\. Relative references must.*$/, '') + + `.\nTip: add an "import-map.json" file to specify import paths for dependencies.` + } else { + runtimeError.value = event.value + } }, on_unhandled_rejection: (event: any) => { let error = event.value - if (typeof error === 'string') error = { message: error } + if (typeof error === 'string') { + error = { message: error } + } runtimeError.value = 'Uncaught (in promise): ' + error.message }, on_console: (log: any) => { if (log.level === 'error') { if (log.args[0] instanceof Error) { - runtimeError.value = log.args[0].stack + runtimeError.value = log.args[0].message } else { - runtimeError.value = log.args + runtimeError.value = log.args[0] } } else if (log.level === 'warn') { if (log.args[0].toString().includes('[Vue warn]')) { @@ -145,6 +156,7 @@ function createSandbox() { } async function updatePreview() { + console.clear() runtimeError.value = null runtimeWarning.value = null try { @@ -168,7 +180,7 @@ app.config.errorHandler = e => console.error(e) app.mount('#app')`.trim() ]) } catch (e) { - runtimeError.value = e.stack + runtimeError.value = e.message } } diff --git a/packages/sfc-playground/src/output/srcdoc.html b/packages/sfc-playground/src/output/srcdoc.html index 8890eae0..91dd9e1a 100644 --- a/packages/sfc-playground/src/output/srcdoc.html +++ b/packages/sfc-playground/src/output/srcdoc.html @@ -8,11 +8,6 @@ } - - - - - + + + +
diff --git a/packages/sfc-playground/src/sfcCompiler.ts b/packages/sfc-playground/src/sfcCompiler.ts index d77a6e31..43e7d9fa 100644 --- a/packages/sfc-playground/src/sfcCompiler.ts +++ b/packages/sfc-playground/src/sfcCompiler.ts @@ -14,8 +14,8 @@ let SFCCompiler: typeof defaultCompiler = defaultCompiler // @ts-ignore const defaultVueUrl = import.meta.env.PROD - ? '/vue.runtime.esm-browser.js' // to be copied on build - : '/src/vue-dev-proxy' + ? `${location.origin}/vue.runtime.esm-browser.js` // to be copied on build + : `${location.origin}/src/vue-dev-proxy` export const vueRuntimeUrl = ref(defaultVueUrl)