Skip to content

Commit fa8f7d5

Browse files
fix: playgrounds work in build output
1 parent 5a20126 commit fa8f7d5

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

docs/nuxt.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ export default defineNuxtConfig({
2424
},
2525
css: ["~/assets/css/main.css"],
2626
devtools: { enabled: false },
27+
vite: {
28+
worker: {
29+
format: "es",
30+
},
31+
},
2732
})

docs/src/__tests__/processPlaygroundCode.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ for (let i; i<5; i++) {
102102

103103
it("wrap a console.log", () => {
104104
expect(processPlaygroundCode("const x = 123\nconsole.log(x)")).toBe(
105-
`(async () => { try {const x = 123\nlogOut(1, console.log(x)) } catch (e) { logError(e) } })()`
105+
`(async () => { try {const x = 123\nlogOut(1, consoleOut('log',x)) } catch (e) { logError(e) } })()`
106106
)
107107
})
108108
})

docs/src/playground.worker.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const noop = Symbol()
12
async function loadTempo() {
23
return await import("@formkit/tempo")
34
}
@@ -11,6 +12,27 @@ function logError(error: Error) {
1112
self.postMessage({ error: error.message })
1213
}
1314

14-
self.onmessage = function (event) {
15-
eval(event.data)
15+
function consoleOut(
16+
type: "log" | "error" | "warn" | "info" | symbol,
17+
...args: unknown[]
18+
) {
19+
if (typeof type !== "string") return
20+
console[type](...args)
21+
return args.join(", ")
1622
}
23+
24+
class Playground {
25+
init() {
26+
self.onmessage = async (event) => {
27+
eval(event.data)
28+
}
29+
}
30+
dummy() {
31+
loadTempo()
32+
logOut(0, "")
33+
logError(new Error(""))
34+
consoleOut(noop)
35+
}
36+
}
37+
38+
new Playground().init()

docs/src/processPlaygroundCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export function processPlaygroundCode(rawSource: string): string {
3737
return `const ${p1.trim()} = await loadTempo()`
3838
}
3939
)
40-
41-
code = wrapFunctions(code, [...fns, "console.log"], "logOut")
40+
code = code.replace(/console\.(log|error|warn|info)\(/, "consoleOut('$1',")
41+
code = wrapFunctions(code, [...fns, "consoleOut"], "logOut")
4242

4343
// Replace any api statements with a wrapped log statement with the line
4444
// number explicitly added in.

0 commit comments

Comments
 (0)