From 26f3e566aa8fff235a8f6927ef2c33b28be4fe89 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 4 Jun 2024 10:18:30 -0700 Subject: [PATCH] Don't depend on DOM types in createDefaultMapFromCDN (#3140) --- .changeset/silver-kangaroos-exist.md | 5 +++++ packages/typescript-vfs/package.json | 1 - packages/typescript-vfs/src/index.ts | 28 +++++++++++++++++++++------ packages/typescript-vfs/tsconfig.json | 2 +- pnpm-lock.yaml | 3 --- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 .changeset/silver-kangaroos-exist.md diff --git a/.changeset/silver-kangaroos-exist.md b/.changeset/silver-kangaroos-exist.md new file mode 100644 index 000000000000..30fc0f08ebcd --- /dev/null +++ b/.changeset/silver-kangaroos-exist.md @@ -0,0 +1,5 @@ +--- +"@typescript/vfs": patch +--- + +Don't depend on DOM types in createDefaultMapFromCDN diff --git a/packages/typescript-vfs/package.json b/packages/typescript-vfs/package.json index b089789e2c77..37af58a5122a 100755 --- a/packages/typescript-vfs/package.json +++ b/packages/typescript-vfs/package.json @@ -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": "*" diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index 10ec61f02026..d994f57a8c87 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -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; text(): Promise }> + +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 { @@ -292,6 +303,11 @@ export const addAllFilesFromFolder = (map: Map, workingDir: stri export const addFilesForTypesIntoFolder = (map: Map) => 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. @@ -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() const files = knownLibFilesForCompilerOptions(options, ts) const prefix = `https://playgroundcdn.typescriptlang.org/cdn/${version}/typescript/lib/` @@ -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 => { diff --git a/packages/typescript-vfs/tsconfig.json b/packages/typescript-vfs/tsconfig.json index 5bbc757d4b6b..3c13716e7565 100644 --- a/packages/typescript-vfs/tsconfig.json +++ b/packages/typescript-vfs/tsconfig.json @@ -5,7 +5,7 @@ "compilerOptions": { "target": "ES2015", "module": "esnext", - "lib": ["dom", "esnext"], + "lib": ["esnext"], "importHelpers": true, "declaration": true, "sourceMap": true, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ba41c92b553..b4248ba2d00a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -324,9 +324,6 @@ importers: jest-watch-typeahead: specifier: ^2.2.2 version: 2.2.2(jest@29.7.0(@types/node@18.19.33)(babel-plugin-macros@3.1.0)(ts-node@10.9.2(@types/node@18.19.33)(typescript@5.4.5))) - lz-string: - specifier: ^1.5.0 - version: 1.5.0 ts-jest: specifier: ^29.0.5 version: 29.1.3(@babel/core@7.24.5)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.24.5))(jest@29.7.0(@types/node@18.19.33)(babel-plugin-macros@3.1.0)(ts-node@10.9.2(@types/node@18.19.33)(typescript@5.4.5)))(typescript@5.4.5)