Skip to content

Commit

Permalink
fix: playgrounds work in build output
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-schroeder committed Feb 9, 2024
1 parent 5a20126 commit fa8f7d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ export default defineNuxtConfig({
},
css: ["~/assets/css/main.css"],
devtools: { enabled: false },
vite: {
worker: {
format: "es",
},
},
})
2 changes: 1 addition & 1 deletion docs/src/__tests__/processPlaygroundCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ for (let i; i<5; i++) {

it("wrap a console.log", () => {
expect(processPlaygroundCode("const x = 123\nconsole.log(x)")).toBe(
`(async () => { try {const x = 123\nlogOut(1, console.log(x)) } catch (e) { logError(e) } })()`
`(async () => { try {const x = 123\nlogOut(1, consoleOut('log',x)) } catch (e) { logError(e) } })()`
)
})
})
26 changes: 24 additions & 2 deletions docs/src/playground.worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const noop = Symbol()
async function loadTempo() {
return await import("@formkit/tempo")
}
Expand All @@ -11,6 +12,27 @@ function logError(error: Error) {
self.postMessage({ error: error.message })
}

self.onmessage = function (event) {
eval(event.data)
function consoleOut(
type: "log" | "error" | "warn" | "info" | symbol,
...args: unknown[]
) {
if (typeof type !== "string") return
console[type](...args)
return args.join(", ")
}

class Playground {
init() {
self.onmessage = async (event) => {
eval(event.data)
}
}
dummy() {
loadTempo()
logOut(0, "")
logError(new Error(""))
consoleOut(noop)
}
}

new Playground().init()
4 changes: 2 additions & 2 deletions docs/src/processPlaygroundCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export function processPlaygroundCode(rawSource: string): string {
return `const ${p1.trim()} = await loadTempo()`
}
)

code = wrapFunctions(code, [...fns, "console.log"], "logOut")
code = code.replace(/console\.(log|error|warn|info)\(/, "consoleOut('$1',")
code = wrapFunctions(code, [...fns, "consoleOut"], "logOut")

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

0 comments on commit fa8f7d5

Please sign in to comment.