@@ -15,7 +15,7 @@ import srcdoc from './srcdoc.html?raw'
1515import { PreviewProxy } from ' ./PreviewProxy'
1616import { compileModulesForPreview } from ' ./moduleCompiler'
1717import type { Store } from ' ../store'
18- import { injectKeyProps } from ' ../types'
18+ import { type LogLevel , type SandboxEmits , injectKeyProps } from ' ../types'
1919import { getVersions , isVaporSupported } from ' ../import-map'
2020
2121export interface SandboxProps {
@@ -47,6 +47,10 @@ const props = withDefaults(defineProps<SandboxProps>(), {
4747 previewOptions : () => ({}),
4848 autoStoreInit: true ,
4949})
50+
51+ const emit = defineEmits <SandboxEmits >()
52+
53+
5054const { store, theme, clearConsole, previewOptions } = toRefs (props )
5155
5256const keyProps = inject (injectKeyProps )
@@ -132,6 +136,9 @@ function createSandbox() {
132136 sandbox .srcdoc = sandboxSrc
133137 containerRef .value ?.appendChild (sandbox )
134138
139+ const doLog = (logLevel : LogLevel , data ? : any ) =>
140+ emit (' log' , { logLevel , data })
141+
135142 proxy = new PreviewProxy (sandbox , {
136143 on_fetch_progress : (progress : any ) => {
137144 // pending_imports = progress;
@@ -157,33 +164,33 @@ function createSandbox() {
157164 }
158165 runtimeError .value = ' Uncaught (in promise): ' + error .message
159166 },
160- on_console : (log : any ) => {
161- if (log .duplicate ) {
162- return
163- }
167+ on_console : (log : any ) => {
168+ const maybeMsg = log .args [0 ]
164169 if (log .level === ' error' ) {
165- if (log .args [0 ] instanceof Error ) {
166- runtimeError .value = log .args [0 ].message
167- } else {
168- runtimeError .value = log .args [0 ]
169- }
170- } else if (log .level === ' warn' ) {
171- if (log .args [0 ].toString ().includes (' [Vue warn]' )) {
172- runtimeWarning .value = log .args
173- .join (' ' )
174- .replace (/ \[ Vue warn\] :/ , ' ' )
175- .trim ()
170+ if (maybeMsg instanceof Error ) {
171+ runtimeError .value = maybeMsg .message
172+ } else if (! maybeMsg ?.startsWith (' [vue-repl]' )) {
173+ runtimeError .value = maybeMsg
176174 }
175+ } else if (
176+ log .level === ' warn' &&
177+ maybeMsg .toString ().includes (' [Vue warn]' )
178+ ) {
179+ runtimeWarning .value = log .args
180+ .join (' ' )
181+ .replace (/ \[ Vue warn\] :/ , ' ' )
182+ .trim ()
177183 }
184+ doLog (log .level || ' log' , log .args )
178185 },
179186 on_console_group : (action : any ) => {
180- // group_logs( action.label, false);
187+ doLog ( ' group ' , action .label )
181188 },
182189 on_console_group_end : () => {
183- // ungroup_logs();
190+ doLog ( ' groupEnd ' )
184191 },
185192 on_console_group_collapsed : (action : any ) => {
186- // group_logs( action.label, true);
193+ doLog ( ' groupCollapsed ' , action .label )
187194 },
188195 })
189196
@@ -348,12 +355,7 @@ defineExpose({ reload, container: containerRef })
348355 </script >
349356
350357<template >
351- <div
352- v-show =" props.show"
353- ref =" container"
354- class =" iframe-container"
355- :class =" theme"
356- />
358+ <div v-show =" show" ref =" container" class =" iframe-container" :class =" theme" />
357359 <Message :err =" (previewOptions?.showRuntimeError ?? true) && runtimeError" />
358360 <Message
359361 v-if =" !runtimeError && (previewOptions?.showRuntimeWarning ?? true)"
0 commit comments