From daa9b35b4784c8f1b3cc629fe76b778f86bb2fe0 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Tue, 17 Sep 2024 18:15:44 +0200 Subject: [PATCH] Prevent configuration file "loading loop" when transpiling TS. (#202) --- .changeset/dry-humans-roll.md | 5 +++ src/language-server/config/loadTsConfig.ts | 40 ++++++++++++++-------- src/language-server/server.ts | 3 ++ src/language-server/workspace.ts | 3 -- 4 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 .changeset/dry-humans-roll.md diff --git a/.changeset/dry-humans-roll.md b/.changeset/dry-humans-roll.md new file mode 100644 index 00000000..f2690f67 --- /dev/null +++ b/.changeset/dry-humans-roll.md @@ -0,0 +1,5 @@ +--- +"vscode-apollo": patch +--- + +Prevent configuration file parsing loop when transpiling TypeScript configs. diff --git a/src/language-server/config/loadTsConfig.ts b/src/language-server/config/loadTsConfig.ts index ce873e34..bf555799 100644 --- a/src/language-server/config/loadTsConfig.ts +++ b/src/language-server/config/loadTsConfig.ts @@ -1,38 +1,50 @@ import { Loader, defaultLoaders } from "cosmiconfig"; import { dirname } from "node:path"; import { rm, writeFile } from "node:fs/promises"; -import { existsSync } from "node:fs"; +import typescript from "typescript"; // implementation based on /~https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/src/loaders.ts // Copyright (c) 2015 David Clark licensed MIT. Full license can be found here: // /~https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/LICENSE -let typescript: typeof import("typescript"); export const loadTs: Loader = async function loadTs(filepath, content) { try { - return await defaultLoaders[".ts"](filepath, content); + return await load(filepath, content, ".vscode-extension-ignore.mjs", { + module: typescript.ModuleKind.ES2022, + }); } catch (error) { if ( - !(error instanceof Error) || - !error.message.includes("module is not defined") - ) + error instanceof Error && + // [ERROR] ReferenceError: module is not defined in ES module scope + error.message.includes("module is not defined") + ) { + return await load(filepath, content, ".vscode-extension-ignore.cjs", { + module: typescript.ModuleKind.CommonJS, + }); + } else { throw error; + } } +}; - if (typescript === undefined) { - typescript = await import("typescript"); - } - const compiledFilepath = `${filepath.slice(0, -2)}cjs`; +async function load( + filepath: string, + content: string, + extension: string, + compilerOptions: Partial, +) { + const compiledFilepath = `${filepath}${extension}`; let transpiledContent; try { try { const config = resolveTsConfig(dirname(filepath)) ?? {}; config.compilerOptions = { ...config.compilerOptions, - module: typescript.ModuleKind.CommonJS, + moduleResolution: typescript.ModuleResolutionKind.Bundler, target: typescript.ScriptTarget.ES2022, noEmit: false, + ...compilerOptions, }; transpiledContent = typescript.transpileModule( content, @@ -46,11 +58,9 @@ export const loadTs: Loader = async function loadTs(filepath, content) { // eslint-disable-next-line @typescript-eslint/return-await return await defaultLoaders[".js"](compiledFilepath, transpiledContent); } finally { - if (existsSync(compiledFilepath)) { - await rm(compiledFilepath); - } + await rm(compiledFilepath, { force: true }); } -}; +} // eslint-disable-next-line @typescript-eslint/no-explicit-any function resolveTsConfig(directory: string): any { diff --git a/src/language-server/server.ts b/src/language-server/server.ts index fc0a0e51..d4869cc0 100644 --- a/src/language-server/server.ts +++ b/src/language-server/server.ts @@ -192,6 +192,9 @@ documents.onDidClose( connection.onDidChangeWatchedFiles((params) => { const handledByProject = new Map(); for (const { uri, type } of params.changes) { + if (uri.match(/vscode-extension-ignore.[mc]?js$/)) { + continue; + } const fsPath = URI.parse(uri).fsPath; const fileName = basename(fsPath); if ( diff --git a/src/language-server/workspace.ts b/src/language-server/workspace.ts index c3b056bb..08f7803f 100644 --- a/src/language-server/workspace.ts +++ b/src/language-server/workspace.ts @@ -215,9 +215,6 @@ export class GraphQLWorkspace { } // If project exists, update the config if (project && config) { - if (equal(project.config.rawConfig, config.rawConfig)) { - return; - } await Promise.all(project.updateConfig(config)); this.reloadService(); }