From 70d5deb9b5a83f7c6a073d4707660302a79cffd3 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Fri, 11 Oct 2024 20:34:45 +1030 Subject: [PATCH 01/19] fix importAttributes is not avaliable in old node versions fix #225 --- .../config/cache-busting-resolver.js | 25 +++++++++++++++---- .../config/cache-busting-resolver.types.ts | 6 +++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 2258569f7..983c73a00 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -3,6 +3,19 @@ const { pathToFileURL } = require("node:url"); /** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext } from "./cache-busting-resolver.types" */ +/** + * importAssertions was renamed to importAttributes in newer versions of Node.js. + * + * @param {ResolveContext|ImportContext} context + * @returns {"importAttributes"|"importAssertions"} + */ +function importAttributesKeyName(context) { + if (!("importAttributes" in context) && "importAssertions" in context) { + return "importAssertions"; + } + return "importAttributes"; +} + /** * @param {string} specifier * @returns {string} @@ -21,14 +34,15 @@ function bustFileName(specifier) { * @returns {Promise} */ async function resolve(specifier, context, nextResolve) { - if (context.importAttributes.as !== "cachebust") { + const importAttributesKey = importAttributesKeyName(context); + if (context[importAttributesKey].as !== "cachebust") { return nextResolve(specifier, context); } // no need to resolve at all, we have all necessary information return { url: bustFileName(specifier), - format: context.importAttributes.format, - importAttributes: context.importAttributes, + format: context[importAttributesKey].format, + [importAttributesKey]: context[importAttributesKey], shortCircuit: true, }; } @@ -41,13 +55,14 @@ async function resolve(specifier, context, nextResolve) { * @returns {Promise} */ async function load(url, context, nextLoad) { - if (context.importAttributes.as !== "cachebust") { + const importAttributesKey = importAttributesKeyName(context); + if (context[importAttributesKey].as !== "cachebust") { return nextLoad(url, context); } return { format: context.format || "module", shortCircuit: true, - source: context.importAttributes.contents, + source: context[importAttributesKey].contents, }; } diff --git a/src/language-server/config/cache-busting-resolver.types.ts b/src/language-server/config/cache-busting-resolver.types.ts index 4feba8766..01611767c 100644 --- a/src/language-server/config/cache-busting-resolver.types.ts +++ b/src/language-server/config/cache-busting-resolver.types.ts @@ -19,13 +19,15 @@ type Format = export interface ResolveContext { conditions: string[]; - importAttributes: ImportAttributes; + importAttributes?: ImportAttributes; + importAssertions: ImportAttributes; parentURL?: string; } export interface ImportContext { conditions: string[]; - importAttributes: ImportAttributes; + importAttributes?: ImportAttributes; + importAssertions: ImportAttributes; format: Format; } From ceecb1b70383c5b00895890bb9a62c6606ab06af Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:05:53 +1030 Subject: [PATCH 02/19] fix importAssertions --- .../config/cache-busting-resolver.js | 52 +++++++++++++++---- .../config/cache-busting-resolver.types.ts | 3 +- src/language-server/config/loadConfig.ts | 14 ++--- src/language-server/config/loadTsConfig.ts | 7 ++- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 983c73a00..2a6a6287e 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -3,6 +3,17 @@ const { pathToFileURL } = require("node:url"); /** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext } from "./cache-busting-resolver.types" */ +/** + * importAssertions was renamed to importAttributes in newer versions of Node.js. + * + * @param {ResolveContext|ImportContext} context + * @returns {boolean} + */ + +function isImportAttributesAvailable(context) { + return "importAttributes" in context; +} + /** * importAssertions was renamed to importAttributes in newer versions of Node.js. * @@ -10,10 +21,23 @@ const { pathToFileURL } = require("node:url"); * @returns {"importAttributes"|"importAssertions"} */ function importAttributesKeyName(context) { - if (!("importAttributes" in context) && "importAssertions" in context) { - return "importAssertions"; + if (isImportAttributesAvailable(context)) { + return "importAttributes"; + } + return "importAssertions"; +} + +/** + * importAssertions was renamed to importAttributes in newer versions of Node.js. + * + * @param {ResolveContext|ImportContext} context + * @returns {ImportAttributes} + */ +function resolveImportAttributes(context) { + if (!isImportAttributesAvailable(context)) { + return context.importAssertions; } - return "importAttributes"; + return context.importAttributes; } /** @@ -34,15 +58,16 @@ function bustFileName(specifier) { * @returns {Promise} */ async function resolve(specifier, context, nextResolve) { - const importAttributesKey = importAttributesKeyName(context); - if (context[importAttributesKey].as !== "cachebust") { + const importAttributes = resolveImportAttributes(context); + const [as, format] = importAttributes.as.split(":"); + if (as !== "cachebust") { return nextResolve(specifier, context); } // no need to resolve at all, we have all necessary information return { url: bustFileName(specifier), - format: context[importAttributesKey].format, - [importAttributesKey]: context[importAttributesKey], + format, + [importAttributesKeyName(context)]: importAttributes, shortCircuit: true, }; } @@ -55,14 +80,19 @@ async function resolve(specifier, context, nextResolve) { * @returns {Promise} */ async function load(url, context, nextLoad) { - const importAttributesKey = importAttributesKeyName(context); - if (context[importAttributesKey].as !== "cachebust") { + const importAttributes = resolveImportAttributes(context); + const [as, format] = importAttributes.as.split(":"); + if (as !== "cachebust") { return nextLoad(url, context); } + const contents = + "contents" in importAttributes + ? importAttributes.contents + : Object.keys(importAttributes)[1]; return { - format: context.format || "module", + format: format || "module", shortCircuit: true, - source: context[importAttributesKey].contents, + source: contents, }; } diff --git a/src/language-server/config/cache-busting-resolver.types.ts b/src/language-server/config/cache-busting-resolver.types.ts index 01611767c..453c90c8e 100644 --- a/src/language-server/config/cache-busting-resolver.types.ts +++ b/src/language-server/config/cache-busting-resolver.types.ts @@ -2,9 +2,8 @@ import { pathToFileURL } from "node:url"; export type ImportAttributes = | { - as: "cachebust"; + as: `cachebust:${"module" | "commonjs"}`; contents: string; - format: Format; } | { as?: undefined }; diff --git a/src/language-server/config/loadConfig.ts b/src/language-server/config/loadConfig.ts index e65f02f29..ee4db120c 100644 --- a/src/language-server/config/loadConfig.ts +++ b/src/language-server/config/loadConfig.ts @@ -73,16 +73,10 @@ export async function loadConfig({ // search can fail if a file can't be parsed (ex: a nonsense js file) so we wrap in a try/catch let loadedConfig: ConfigResult; - try { - loadedConfig = (await explorer.search( - configPath, - )) as ConfigResult; - } catch (error) { - throw new Error(`A config file failed to load with options: ${JSON.stringify( - arguments[0], - )}. - The error was: ${error}`); - } + loadedConfig = (await explorer.search( + configPath, + )) as ConfigResult; + if (!loadedConfig || loadedConfig.isEmpty) { Debug.error( diff --git a/src/language-server/config/loadTsConfig.ts b/src/language-server/config/loadTsConfig.ts index 6cfa000c7..162826572 100644 --- a/src/language-server/config/loadTsConfig.ts +++ b/src/language-server/config/loadTsConfig.ts @@ -113,9 +113,12 @@ async function loadCachebustedJs( // @ts-ignore { with: { - as: "cachebust", + as: `cachebust:${type}`, + contents, + } satisfies ImportAttributes, + assert: { + as: `cachebust:${type}`, contents, - format: type, } satisfies ImportAttributes, } ) From f9f5013a558277be64205ff6e7f2b8ed6ec6741b Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:10:52 +1030 Subject: [PATCH 03/19] remove duplicated comments --- src/language-server/config/cache-busting-resolver.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 2a6a6287e..22e6addd7 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -15,8 +15,6 @@ function isImportAttributesAvailable(context) { } /** - * importAssertions was renamed to importAttributes in newer versions of Node.js. - * * @param {ResolveContext|ImportContext} context * @returns {"importAttributes"|"importAssertions"} */ @@ -28,8 +26,6 @@ function importAttributesKeyName(context) { } /** - * importAssertions was renamed to importAttributes in newer versions of Node.js. - * * @param {ResolveContext|ImportContext} context * @returns {ImportAttributes} */ From e15e27a706db5c73f7c4c1b277779882586ef5c7 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:11:35 +1030 Subject: [PATCH 04/19] rename import-attributesKeyName to resolveImportAttributesKeyName --- src/language-server/config/cache-busting-resolver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 22e6addd7..010a7d413 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -18,7 +18,7 @@ function isImportAttributesAvailable(context) { * @param {ResolveContext|ImportContext} context * @returns {"importAttributes"|"importAssertions"} */ -function importAttributesKeyName(context) { +function resolveImportAttributesKeyName(context) { if (isImportAttributesAvailable(context)) { return "importAttributes"; } @@ -63,7 +63,7 @@ async function resolve(specifier, context, nextResolve) { return { url: bustFileName(specifier), format, - [importAttributesKeyName(context)]: importAttributes, + [resolveImportAttributesKeyName(context)]: importAttributes, shortCircuit: true, }; } From dfb3f67d36deb2268ecaeef1023e526351432b24 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:12:50 +1030 Subject: [PATCH 05/19] remove empty line --- src/language-server/config/cache-busting-resolver.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 010a7d413..eb0a9c64d 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -9,7 +9,6 @@ const { pathToFileURL } = require("node:url"); * @param {ResolveContext|ImportContext} context * @returns {boolean} */ - function isImportAttributesAvailable(context) { return "importAttributes" in context; } From d4a25ad52267af934a24815baacdf9afb7003946 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:13:46 +1030 Subject: [PATCH 06/19] revert changes to loadConfig.ts --- src/language-server/config/loadConfig.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/language-server/config/loadConfig.ts b/src/language-server/config/loadConfig.ts index ee4db120c..e65f02f29 100644 --- a/src/language-server/config/loadConfig.ts +++ b/src/language-server/config/loadConfig.ts @@ -73,10 +73,16 @@ export async function loadConfig({ // search can fail if a file can't be parsed (ex: a nonsense js file) so we wrap in a try/catch let loadedConfig: ConfigResult; - loadedConfig = (await explorer.search( - configPath, - )) as ConfigResult; - + try { + loadedConfig = (await explorer.search( + configPath, + )) as ConfigResult; + } catch (error) { + throw new Error(`A config file failed to load with options: ${JSON.stringify( + arguments[0], + )}. + The error was: ${error}`); + } if (!loadedConfig || loadedConfig.isEmpty) { Debug.error( From 6b9ed5f6cde9a2c0ad5e144af979367419ed8194 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:17:16 +1030 Subject: [PATCH 07/19] small tweak --- src/language-server/config/cache-busting-resolver.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index eb0a9c64d..6f80d4de9 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -29,10 +29,10 @@ function resolveImportAttributesKeyName(context) { * @returns {ImportAttributes} */ function resolveImportAttributes(context) { - if (!isImportAttributesAvailable(context)) { - return context.importAssertions; + if (isImportAttributesAvailable(context)) { + return context.importAttributes; } - return context.importAttributes; + return context.importAssertions; } /** From cf95ea51f3d8da4c379138673b9def8af48633f2 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:27:40 +1030 Subject: [PATCH 08/19] Update src/language-server/config/cache-busting-resolver.js Co-authored-by: Lenz Weber-Tronic --- src/language-server/config/cache-busting-resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 6f80d4de9..0fe1d21e0 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -2,7 +2,7 @@ const { pathToFileURL } = require("node:url"); /** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext } from "./cache-busting-resolver.types" */ - +/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext,ImportAttributes } from "./cache-busting-resolver.types" */ /** * importAssertions was renamed to importAttributes in newer versions of Node.js. * From e4077694fb2595a3cd1f91c2cbfe8712d1c67b99 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:28:32 +1030 Subject: [PATCH 09/19] remove redundant comment --- src/language-server/config/cache-busting-resolver.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 0fe1d21e0..3aae5cfb5 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -1,7 +1,6 @@ // @ts-check const { pathToFileURL } = require("node:url"); -/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext } from "./cache-busting-resolver.types" */ /** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext,ImportAttributes } from "./cache-busting-resolver.types" */ /** * importAssertions was renamed to importAttributes in newer versions of Node.js. From 99522b8a5fc436990c72f05090eeca62f6ea6e6b Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:29:21 +1030 Subject: [PATCH 10/19] add a space --- src/language-server/config/cache-busting-resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 3aae5cfb5..f5d59215d 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -1,7 +1,7 @@ // @ts-check const { pathToFileURL } = require("node:url"); -/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext,ImportAttributes } from "./cache-busting-resolver.types" */ +/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext, ImportAttributes } from "./cache-busting-resolver.types" */ /** * importAssertions was renamed to importAttributes in newer versions of Node.js. * From 61f79adb18e34d27fdcb00edf0a9b157297346ca Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 21:33:06 +1030 Subject: [PATCH 11/19] Update src/language-server/config/cache-busting-resolver.js Co-authored-by: Lenz Weber-Tronic --- src/language-server/config/cache-busting-resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index f5d59215d..a099f1625 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -6,7 +6,7 @@ const { pathToFileURL } = require("node:url"); * importAssertions was renamed to importAttributes in newer versions of Node.js. * * @param {ResolveContext|ImportContext} context - * @returns {boolean} + * @returns {context is {importAttributes: ImportAttributes}} */ function isImportAttributesAvailable(context) { return "importAttributes" in context; From ee6781482d3fd03d6677fc45b5c92d7b35475ad1 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 22:08:54 +1030 Subject: [PATCH 12/19] add more types and comments --- .../config/cache-busting-resolver.js | 46 ++++++++++++++----- .../config/cache-busting-resolver.types.ts | 28 ++++++++--- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index a099f1625..64d324de5 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -1,12 +1,22 @@ // @ts-check const { pathToFileURL } = require("node:url"); -/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext, ImportAttributes } from "./cache-busting-resolver.types" */ +/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext, ImportAttributes, ImportAssertions, LegacyResolveContext, LegacyImportContext, Format } from "./cache-busting-resolver.types" */ + /** - * importAssertions was renamed to importAttributes in newer versions of Node.js. + * importAssertions was renamed to importAttributes after following versions of Node.js. + * Once we hit a minimum of 1.92, we can remove the legacy check and + * use `importAttributes` directly. * - * @param {ResolveContext|ImportContext} context - * @returns {context is {importAttributes: ImportAttributes}} + * - v21.0.0 + * - v20.10.0 + * - v18.19.0 + * + * @see /~https://github.com/apollographql/vscode-graphql/issues/225 + * @see https://nodejs.org/docs/latest/api/module.html#resolvespecifier-context-nextresolve + * + * @param {ResolveContext|ImportContext|LegacyResolveContext|LegacyImportContext} context + * @returns {context is ResolveContext|ImportContext} */ function isImportAttributesAvailable(context) { return "importAttributes" in context; @@ -24,8 +34,8 @@ function resolveImportAttributesKeyName(context) { } /** - * @param {ResolveContext|ImportContext} context - * @returns {ImportAttributes} + * @param {ResolveContext|ImportContext|LegacyResolveContext|LegacyImportContext} context + * @returns {ImportAttributes|ImportAssertions} */ function resolveImportAttributes(context) { if (isImportAttributesAvailable(context)) { @@ -34,6 +44,20 @@ function resolveImportAttributes(context) { return context.importAssertions; } +/** + * @param {ImportAttributes|ImportAssertions} importAttributes + * @returns {Format|null} + */ +function resolveConfigFormat(importAttributes) { + const [as, format] = importAttributes.as + ? importAttributes.as.split(":") + : []; + if (as === "cachebust" && format) { + return /** @type {Format} */ (format); + } + return null; +} + /** * @param {string} specifier * @returns {string} @@ -53,8 +77,8 @@ function bustFileName(specifier) { */ async function resolve(specifier, context, nextResolve) { const importAttributes = resolveImportAttributes(context); - const [as, format] = importAttributes.as.split(":"); - if (as !== "cachebust") { + const format = resolveConfigFormat(importAttributes); + if (!format) { return nextResolve(specifier, context); } // no need to resolve at all, we have all necessary information @@ -75,8 +99,8 @@ async function resolve(specifier, context, nextResolve) { */ async function load(url, context, nextLoad) { const importAttributes = resolveImportAttributes(context); - const [as, format] = importAttributes.as.split(":"); - if (as !== "cachebust") { + const format = resolveConfigFormat(importAttributes); + if (!format) { return nextLoad(url, context); } const contents = @@ -84,7 +108,7 @@ async function load(url, context, nextLoad) { ? importAttributes.contents : Object.keys(importAttributes)[1]; return { - format: format || "module", + format, shortCircuit: true, source: contents, }; diff --git a/src/language-server/config/cache-busting-resolver.types.ts b/src/language-server/config/cache-busting-resolver.types.ts index 453c90c8e..d649a9f5e 100644 --- a/src/language-server/config/cache-busting-resolver.types.ts +++ b/src/language-server/config/cache-busting-resolver.types.ts @@ -2,12 +2,19 @@ import { pathToFileURL } from "node:url"; export type ImportAttributes = | { - as: `cachebust:${"module" | "commonjs"}`; + as: `cachebust:${Format}`; contents: string; } | { as?: undefined }; -type Format = +export type ImportAssertions = + | { + as: `cachebust:${Format}`; + [key: string]: string; + } + | { as?: undefined }; + +export type Format = | "builtin" | "commonjs" | "json" @@ -16,17 +23,26 @@ type Format = | null | undefined; +export interface LegacyResolveContext { + conditions: string[]; + importAssertions: ImportAssertions; + parentURL?: string; +} + export interface ResolveContext { conditions: string[]; - importAttributes?: ImportAttributes; - importAssertions: ImportAttributes; + importAttributes: ImportAttributes; parentURL?: string; } +export interface LegacyImportContext { + conditions: string[]; + importAssertions: ImportAssertions; + format: Format; +} export interface ImportContext { conditions: string[]; - importAttributes?: ImportAttributes; - importAssertions: ImportAttributes; + importAttributes: ImportAttributes; format: Format; } From 05af15fe9e943146d742c492f529c4d2f296782e Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 22:09:53 +1030 Subject: [PATCH 13/19] mention vscode --- src/language-server/config/cache-busting-resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 64d324de5..86eaa3574 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -5,7 +5,7 @@ const { pathToFileURL } = require("node:url"); /** * importAssertions was renamed to importAttributes after following versions of Node.js. - * Once we hit a minimum of 1.92, we can remove the legacy check and + * Once we hit a minimum of v1.92 of VSCode, we can remove the legacy check and * use `importAttributes` directly. * * - v21.0.0 From 1b5ab0c4673ebde97b6149409800b05fc56ab3c7 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 22:18:21 +1030 Subject: [PATCH 14/19] add changelog --- .changeset/smooth-dodos-build.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smooth-dodos-build.md diff --git a/.changeset/smooth-dodos-build.md b/.changeset/smooth-dodos-build.md new file mode 100644 index 000000000..2788a7034 --- /dev/null +++ b/.changeset/smooth-dodos-build.md @@ -0,0 +1,5 @@ +--- +"vscode-apollo": patch +--- + +Fix config files are unable to load in old VSCode versions From fc084dce6eec6478d9de28e73ddbc48ead08e6f2 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 22:21:51 +1030 Subject: [PATCH 15/19] update changelog --- .changeset/smooth-dodos-build.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/smooth-dodos-build.md b/.changeset/smooth-dodos-build.md index 2788a7034..ee1e58782 100644 --- a/.changeset/smooth-dodos-build.md +++ b/.changeset/smooth-dodos-build.md @@ -2,4 +2,4 @@ "vscode-apollo": patch --- -Fix config files are unable to load in old VSCode versions +Fixes config files being unable to load in old VSCode versions From 071775139d040665db79579037691f4e4d2cba14 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Tue, 15 Oct 2024 14:01:08 +0200 Subject: [PATCH 16/19] just try both locations for `cache-busting-resolver.js` --- src/language-server/config/loadTsConfig.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/language-server/config/loadTsConfig.ts b/src/language-server/config/loadTsConfig.ts index 727197430..f1720f291 100644 --- a/src/language-server/config/loadTsConfig.ts +++ b/src/language-server/config/loadTsConfig.ts @@ -8,11 +8,11 @@ import { ImportAttributes } from "./cache-busting-resolver.types"; // Copyright (c) 2015 David Clark licensed MIT. Full license can be found here: // /~https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/LICENSE -if (process.env.JEST_WORKER_ID === undefined) { +try { register( pathToFileURL(require.resolve("./config/cache-busting-resolver.js")), ); -} else { +} catch { register(pathToFileURL(require.resolve("./cache-busting-resolver.js"))); } From a95ebe3944c636da31aba7be923ab2eb2327ec99 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 22:39:25 +1030 Subject: [PATCH 17/19] remove assert --- src/language-server/config/loadTsConfig.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/language-server/config/loadTsConfig.ts b/src/language-server/config/loadTsConfig.ts index 727197430..08bf443f6 100644 --- a/src/language-server/config/loadTsConfig.ts +++ b/src/language-server/config/loadTsConfig.ts @@ -117,10 +117,6 @@ async function loadCachebustedJs( as: `cachebust:${type}`, contents, } satisfies ImportAttributes, - assert: { - as: `cachebust:${type}`, - contents, - } satisfies ImportAttributes, } ) ).default; From f765e66fc3a6bd0512597f4b93ff727e4648f41e Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Tue, 15 Oct 2024 23:26:54 +1030 Subject: [PATCH 18/19] fix import assert --- src/language-server/config/cache-busting-resolver.js | 2 +- .../config/cache-busting-resolver.types.ts | 1 + src/language-server/config/loadTsConfig.ts | 11 ++++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index 86eaa3574..a61737737 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -106,7 +106,7 @@ async function load(url, context, nextLoad) { const contents = "contents" in importAttributes ? importAttributes.contents - : Object.keys(importAttributes)[1]; + : Object.keys(importAttributes).find((key) => key != "as"); return { format, shortCircuit: true, diff --git a/src/language-server/config/cache-busting-resolver.types.ts b/src/language-server/config/cache-busting-resolver.types.ts index d649a9f5e..32559b688 100644 --- a/src/language-server/config/cache-busting-resolver.types.ts +++ b/src/language-server/config/cache-busting-resolver.types.ts @@ -4,6 +4,7 @@ export type ImportAttributes = | { as: `cachebust:${Format}`; contents: string; + format: Format; } | { as?: undefined }; diff --git a/src/language-server/config/loadTsConfig.ts b/src/language-server/config/loadTsConfig.ts index 4f674dc44..5fcabba20 100644 --- a/src/language-server/config/loadTsConfig.ts +++ b/src/language-server/config/loadTsConfig.ts @@ -3,7 +3,10 @@ import { dirname, extname } from "node:path"; import typescript from "typescript"; import { pathToFileURL } from "node:url"; import { register } from "node:module"; -import { ImportAttributes } from "./cache-busting-resolver.types"; +import { + ImportAssertions, + ImportAttributes, +} from "./cache-busting-resolver.types"; // 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 @@ -116,7 +119,13 @@ async function loadCachebustedJs( with: { as: `cachebust:${type}`, contents, + format: type, } satisfies ImportAttributes, + assert: { + as: `cachebust:${type}`, + contents, + format: type, + } satisfies ImportAssertions, } ) ).default; From 5bffe98a56de5ad497660a57a8be6cda69622d85 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Tue, 15 Oct 2024 15:20:11 +0200 Subject: [PATCH 19/19] fix TS --- src/language-server/config/cache-busting-resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-server/config/cache-busting-resolver.js b/src/language-server/config/cache-busting-resolver.js index a61737737..d80444de5 100644 --- a/src/language-server/config/cache-busting-resolver.js +++ b/src/language-server/config/cache-busting-resolver.js @@ -110,7 +110,7 @@ async function load(url, context, nextLoad) { return { format, shortCircuit: true, - source: contents, + source: /** @type {string} */ (contents), }; }