Skip to content

Commit 34d07be

Browse files
committed
fix(src/preview.js): hijack console before importing modules
1 parent 138dcbc commit 34d07be

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/preview.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import application from 'socket:application'
2-
import { inspect } from 'socket:util'
3-
41
globalThis.RUNTIME_APPLICATION_ALLOW_MULTI_WINDOWS = true
52

6-
const currentWindow = await application.getCurrentWindow()
7-
setTimeout(() => {
8-
const consoleMethods = ['log', 'error', 'info', 'warn', 'debug']
9-
for (const method of consoleMethods) {
10-
const original = console[method]
11-
globalThis.console[method] = (...args) => {
12-
original.call(console, ...args)
3+
let currentWindow = null
4+
const consoleMethods = ['log', 'error', 'info', 'warn', 'debug']
5+
for (const method of consoleMethods) {
6+
const original = console[method]
7+
globalThis.console[method] = async (...args) => {
8+
if (!currentWindow) {
9+
currentWindow = await application.getCurrentWindow()
10+
}
11+
original.call(console, ...args)
12+
if (currentWindow) {
13+
// @ts-ignore
1314
currentWindow.channel.postMessage({
1415
[method]: [inspect(...args)]
1516
})
1617
}
1718
}
18-
}, 2048)
19+
}
20+
21+
import application from 'socket:application'
22+
import { inspect } from 'socket:util'
1923

2024
const previewWindowTitleBar = 38
2125
const previewWindowMargin = 12

0 commit comments

Comments
 (0)