Skip to content

perf(twoslash): calculate playgroundURL on-demand #3013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-dolls-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typescript/twoslash": patch
---

Calculate `playgroundURL` on-demand
10 changes: 5 additions & 5 deletions packages/ts-twoslasher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ export interface TwoSlashOptions {
*/
export function twoslasher(code: string, extension: string, options: TwoSlashOptions = {}): TwoSlashReturn {
const ts: TS = options.tsModule ?? require("typescript")
const lzstring: LZ = options.lzstringModule ?? require("lz-string")

const originalCode = code
const safeExtension = typesToExtension(extension)
Expand Down Expand Up @@ -785,9 +784,6 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt
staticQuickInfos = []
}

const zippedCode = lzstring.compressToEncodedURIComponent(originalCode)
const playgroundURL = `https://www.typescriptlang.org/play/#code/${zippedCode}`

// Cutting happens last, and it means editing the lines and character index of all
// the type annotations which are attached to a location

Expand Down Expand Up @@ -854,7 +850,11 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt
queries,
staticQuickInfos,
errors,
playgroundURL,
get playgroundURL() {
const lzstring: LZ = options.lzstringModule ?? require("lz-string")
const zippedCode = lzstring.compressToEncodedURIComponent(originalCode)
return `https://www.typescriptlang.org/play/#code/${zippedCode}`
},
tags,
}
}
Expand Down