Skip to content

Commit

Permalink
Don't depend on DOM types in createDefaultMapFromCDN (#3140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Jun 4, 2024
1 parent 7691811 commit 26f3e56
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-kangaroos-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typescript/vfs": patch
---

Don't depend on DOM types in createDefaultMapFromCDN
1 change: 0 additions & 1 deletion packages/typescript-vfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-watch-typeahead": "^2.2.2",
"lz-string": "^1.5.0",
"ts-jest": "^29.0.5",
"tslib": "^2.6.2",
"typescript": "*"
Expand Down
28 changes: 22 additions & 6 deletions packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ type CompilerHost = import("typescript").CompilerHost
type SourceFile = import("typescript").SourceFile
type TS = typeof import("typescript")

type FetchLike = (url: string) => Promise<{ json(): Promise<any>; text(): Promise<string> }>

interface LocalStorageLike {
getItem(key: string): string | null
setItem(key: string, value: string): void
removeItem(key: string): void
}

declare var localStorage: LocalStorageLike | undefined;
declare var fetch: FetchLike | undefined;

let hasLocalStorage = false
try {
hasLocalStorage = typeof localStorage !== `undefined`
} catch (error) { }

const hasProcess = typeof process !== `undefined`
const shouldDebug = (hasLocalStorage && localStorage.getItem("DEBUG")) || (hasProcess && process.env.DEBUG)
const shouldDebug = (hasLocalStorage && localStorage!.getItem("DEBUG")) || (hasProcess && process.env.DEBUG)
const debugLog = shouldDebug ? console.log : (_message?: any, ..._optionalParams: any[]) => ""

export interface VirtualTypeScriptEnvironment {
Expand Down Expand Up @@ -292,6 +303,11 @@ export const addAllFilesFromFolder = (map: Map<string, string>, workingDir: stri
export const addFilesForTypesIntoFolder = (map: Map<string, string>) =>
addAllFilesFromFolder(map, "node_modules/@types")

export interface LZString {
compressToUTF16(input: string): string
decompressFromUTF16(compressed: string): string
}

/**
* Create a virtual FS Map with the lib files from a particular TypeScript
* version based on the target, Always includes dom ATM.
Expand All @@ -309,11 +325,11 @@ export const createDefaultMapFromCDN = (
version: string,
cache: boolean,
ts: TS,
lzstring?: typeof import("lz-string"),
fetcher?: typeof fetch,
storer?: typeof localStorage
lzstring?: LZString,
fetcher?: FetchLike,
storer?: LocalStorageLike
) => {
const fetchlike = fetcher || fetch
const fetchlike = fetcher || fetch!
const fsMap = new Map<string, string>()
const files = knownLibFilesForCompilerOptions(options, ts)
const prefix = `https://playgroundcdn.typescriptlang.org/cdn/${version}/typescript/lib/`
Expand All @@ -340,7 +356,7 @@ export const createDefaultMapFromCDN = (

// A localstorage and lzzip aware version of the lib files
function cached() {
const storelike = storer || localStorage
const storelike = storer || localStorage!

const keys = Object.keys(storelike)
keys.forEach(key => {
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-vfs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"target": "ES2015",
"module": "esnext",
"lib": ["dom", "esnext"],
"lib": ["esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26f3e56

Please sign in to comment.