@@ -2,6 +2,7 @@ import { createSingletonPromise } from '@antfu/utils'
2
2
import type { CodeRunner , CodeRunnerOutput , CodeRunnerOutputText , CodeRunnerOutputs } from '@slidev/types'
3
3
import type { CodeToHastOptions } from 'shiki'
4
4
import type ts from 'typescript'
5
+ import { ref } from 'vue'
5
6
import { isDark } from '../logic/dark'
6
7
import deps from '#slidev/monaco-run-deps'
7
8
import setups from '#slidev/setups/code-runners'
@@ -62,32 +63,32 @@ export default createSingletonPromise(async () => {
62
63
} )
63
64
64
65
// Ported from https://github.com/microsoft/TypeScript-Website/blob/v2/packages/playground/src/sidebar/runtime.ts
65
- async function runJavaScript ( code : string ) : Promise < CodeRunnerOutputs > {
66
- const allLogs : CodeRunnerOutput [ ] = [ ]
67
-
68
- const replace = { } as any
69
- const logger = function ( ...objs : any [ ] ) {
70
- allLogs . push ( objs . map ( printObject ) )
71
- }
72
- replace . info = replace . log = replace . debug = replace . warn = replace . error = logger
73
- replace . clear = ( ) => allLogs . length = 0
74
- const vmConsole = Object . assign ( { } , console , replace )
66
+ function runJavaScript ( code : string ) : CodeRunnerOutputs {
67
+ const result = ref < CodeRunnerOutput [ ] > ( [ ] )
68
+
69
+ const onError = ( error : any ) => result . value . push ( { error : String ( error ) } )
70
+ const logger = ( ...objs : any [ ] ) => result . value . push ( objs . map ( printObject ) )
71
+ const vmConsole = Object . assign ( { } , console )
72
+ vmConsole . info = vmConsole . log = vmConsole . debug = vmConsole . warn = vmConsole . error = logger
73
+ vmConsole . clear = ( ) => result . value . length = 0
75
74
try {
76
- const safeJS = `return async (console, __slidev_import) => {
77
- ${ sanitizeJS ( code ) }
75
+ const safeJS = `return async (console, __slidev_import, __slidev_on_error) => {
76
+ try {
77
+ ${ sanitizeJS ( code ) }
78
+ } catch (e) {
79
+ __slidev_on_error(e)
80
+ }
78
81
}`
79
82
// eslint-disable-next-line no-new-func
80
- await ( new Function ( safeJS ) ( ) ) ( vmConsole , ( specifier : string ) => {
83
+ ; ( new Function ( safeJS ) ( ) ) ( vmConsole , ( specifier : string ) => {
81
84
const mod = deps [ specifier ]
82
85
if ( ! mod )
83
86
throw new Error ( `Module not found: ${ specifier } .\nAvailable modules: ${ Object . keys ( deps ) . join ( ', ' ) } . Please refer to https://sli.dev/custom/config-code-runners#additional-runner-dependencies` )
84
87
return mod
85
- } )
88
+ } , onError )
86
89
}
87
90
catch ( error ) {
88
- return {
89
- error : String ( error ) ,
90
- }
91
+ onError ( error )
91
92
}
92
93
93
94
function printObject ( arg : any ) : CodeRunnerOutputText {
@@ -162,7 +163,7 @@ async function runJavaScript(code: string): Promise<CodeRunnerOutputs> {
162
163
return code
163
164
}
164
165
165
- return allLogs
166
+ return result
166
167
}
167
168
168
169
let tsModule : typeof import ( 'typescript' ) | undefined
@@ -183,7 +184,7 @@ export async function runTypeScript(code: string) {
183
184
const importRegex = / i m p o r t \s * \( ( .+ ) \) / g
184
185
code = code . replace ( importRegex , ( _full , specifier ) => `__slidev_import(${ specifier } )` )
185
186
186
- return await runJavaScript ( code )
187
+ return runJavaScript ( code )
187
188
}
188
189
189
190
/**
0 commit comments