Skip to content

Commit 8095222

Browse files
author
Dsaquel
committed
chore: handles logs to be part of plugin
1 parent 3d2d04e commit 8095222

File tree

3 files changed

+53
-139
lines changed

3 files changed

+53
-139
lines changed

src/output/Sandbox.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function createSandbox() {
164164
if (log.level === 'error') {
165165
if (maybeMsg instanceof Error) {
166166
runtimeError.value = maybeMsg.message
167-
} else if (!maybeMsg.includes('%c Cannot clone the message')) {
167+
} else if (!maybeMsg?.startsWith('[vue-repl]')) {
168168
runtimeError.value = maybeMsg
169169
}
170170
} else if (

src/output/srcdoc.html

Lines changed: 51 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -153,36 +153,51 @@
153153
)
154154
}
155155
})
156-
;['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach(
157-
(level) => {
158-
const original = console[level]
159-
console[level] = (...args) => {
160-
const msg = args[0]
161-
if (typeof msg === 'string') {
162-
if (
163-
msg.includes('You are running a development build of Vue') ||
164-
msg.includes('You are running the esm-bundler build of Vue')
165-
) {
166-
return
167-
}
156+
const errorColorStr = 'color: #fb2c36'
157+
const errorMsg = 'please open the devtool to see correctly this log'
158+
;[
159+
'clear',
160+
'log',
161+
'info',
162+
'dir',
163+
'warn',
164+
'error',
165+
'table',
166+
'debug',
167+
'time',
168+
'timeLog',
169+
'timeEnd',
170+
'assert',
171+
'count',
172+
'countReset',
173+
].forEach((level) => {
174+
const original = console[level]
175+
console[level] = async (...args) => {
176+
const msg = args[0]
177+
if (typeof msg === 'string') {
178+
if (
179+
msg.includes('You are running a development build of Vue') ||
180+
msg.includes('You are running the esm-bundler build of Vue')
181+
) {
182+
return
168183
}
169-
original(...args)
170-
171-
const postMessage = () =>
172-
parent.postMessage({ action: 'console', level, args }, '*')
173-
174-
try {
175-
postMessage()
176-
} catch {
184+
}
185+
original(...args)
186+
const postMessage = () =>
187+
parent.postMessage({ action: 'console', level, args }, '*')
188+
try {
189+
postMessage()
190+
} catch {
191+
args = args.map(toString)
192+
if (!args?.[0]?.startsWith('[Vue warn]'))
177193
args = [
178-
'%c Cannot clone the message, please open the devtool to see corretly this log. Supported types are listed here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#javascript_types',
179-
'color: #fb2c36',
194+
`[vue-repl]: %c Cannot clone the message, ${errorMsg}. Supported types are listed here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#javascript_types`,
195+
errorColorStr,
180196
]
181-
postMessage()
182-
}
197+
postMessage()
183198
}
184-
},
185-
)
199+
}
200+
})
186201
;[
187202
{ method: 'group', action: 'console_group' },
188203
{ method: 'groupEnd', action: 'console_group_end' },
@@ -195,119 +210,17 @@
195210
original(label)
196211
}
197212
})
198-
199-
const timers = new Map()
200-
const original_time = console.time
201-
const original_timelog = console.timeLog
202-
const original_timeend = console.timeEnd
203-
204-
console.time = (label = 'default') => {
205-
original_time(label)
206-
timers.set(label, performance.now())
207-
}
208-
console.timeLog = (label = 'default') => {
209-
original_timelog(label)
210-
const now = performance.now()
211-
if (timers.has(label)) {
212-
parent.postMessage(
213-
{
214-
action: 'console',
215-
level: 'log',
216-
args: [`${label}: ${now - timers.get(label)}ms`],
217-
},
218-
'*',
219-
)
220-
} else {
221-
parent.postMessage(
222-
{
223-
action: 'console',
224-
level: 'warn',
225-
args: [`Timer '${label}' does not exist`],
226-
},
227-
'*',
228-
)
229-
}
230-
}
231-
console.timeEnd = (label = 'default') => {
232-
original_timeend(label)
233-
const now = performance.now()
234-
if (timers.has(label)) {
235-
parent.postMessage(
236-
{
237-
action: 'console',
238-
level: 'log',
239-
args: [`${label}: ${now - timers.get(label)}ms`],
240-
},
241-
'*',
242-
)
243-
} else {
244-
parent.postMessage(
245-
{
246-
action: 'console',
247-
level: 'warn',
248-
args: [`Timer '${label}' does not exist`],
249-
},
250-
'*',
251-
)
213+
;['profile', 'profileEnd', 'trace', 'dirXml'].forEach((level) => {
214+
const original = console[level]
215+
console[level] = () => {
216+
original()
217+
const args = [
218+
`[vue-repl]: %c Cannot handle "${level}" log, ${errorMsg}.`,
219+
errorColorStr,
220+
]
221+
parent.postMessage({ action: 'console', level: 'log', args }, '*')
252222
}
253-
timers.delete(label)
254-
}
255-
256-
const original_assert = console.assert
257-
console.assert = (condition, ...args) => {
258-
if (condition) {
259-
const stack = new Error().stack
260-
parent.postMessage(
261-
{ action: 'console', level: 'assert', args, stack },
262-
'*',
263-
)
264-
}
265-
original_assert(condition, ...args)
266-
}
267-
268-
const counter = new Map()
269-
const original_count = console.count
270-
const original_countreset = console.countReset
271-
272-
console.count = (label = 'default') => {
273-
counter.set(label, (counter.get(label) || 0) + 1)
274-
parent.postMessage(
275-
{
276-
action: 'console',
277-
level: 'log',
278-
args: [`${label}: ${counter.get(label)}`],
279-
},
280-
'*',
281-
)
282-
original_count(label)
283-
}
284-
285-
console.countReset = (label = 'default') => {
286-
if (counter.has(label)) {
287-
counter.set(label, 0)
288-
} else {
289-
parent.postMessage(
290-
{
291-
action: 'console',
292-
level: 'warn',
293-
args: [`Count for '${label}' does not exist`],
294-
},
295-
'*',
296-
)
297-
}
298-
original_countreset(label)
299-
}
300-
301-
const original_trace = console.trace
302-
303-
console.trace = (...args) => {
304-
const stack = new Error().stack
305-
parent.postMessage(
306-
{ action: 'console', level: 'trace', args, stack },
307-
'*',
308-
)
309-
original_trace(...args)
310-
}
223+
})
311224

312225
function toString(value) {
313226
if (value instanceof Error) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface EditorProps {
99
mode?: EditorMode
1010
}
1111
export type LogLevel =
12+
| 'clear'
1213
| 'log'
1314
| 'error'
1415
| 'warn'

0 commit comments

Comments
 (0)