From 790cfc1df60ad2738b0c0ed71436c4bbbfbc248c Mon Sep 17 00:00:00 2001 From: Aaron Adams Date: Mon, 17 Feb 2025 23:47:18 +0100 Subject: [PATCH] feat: support top-level await (#1631) * Add tests * Support top-level await * Add changeset --- .changeset/fresh-bikes-kneel.md | 5 +++++ src/helpers/cosmiconfig.ts | 29 ++++++++++++++++------------- test/config.spec.ts | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 .changeset/fresh-bikes-kneel.md diff --git a/.changeset/fresh-bikes-kneel.md b/.changeset/fresh-bikes-kneel.md new file mode 100644 index 000000000..82f95a455 --- /dev/null +++ b/.changeset/fresh-bikes-kneel.md @@ -0,0 +1,5 @@ +--- +'graphql-config': patch +--- + +support top-level await diff --git a/src/helpers/cosmiconfig.ts b/src/helpers/cosmiconfig.ts index 9b817946b..32a02efdb 100644 --- a/src/helpers/cosmiconfig.ts +++ b/src/helpers/cosmiconfig.ts @@ -1,6 +1,6 @@ import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig'; import { env } from 'string-env-interpolation'; -import jiti from 'jiti'; +import { createJiti } from 'jiti'; export interface ConfigSearchResult { config: any; @@ -29,21 +29,23 @@ function createCustomLoader(loader: Loader): Loader { } export function createCosmiConfig(moduleName: string, legacy: boolean) { - const options = prepareCosmiconfig(moduleName, legacy); - + const options = prepareCosmiconfig(moduleName, legacy, 'async'); return cosmiconfig(moduleName, options); } export function createCosmiConfigSync(moduleName: string, legacy: boolean) { - const options = prepareCosmiconfig(moduleName, legacy); + const options = prepareCosmiconfig(moduleName, legacy, 'sync'); return cosmiconfigSync(moduleName, options); } const loadTypeScript: Loader = (filepath) => { - const jitiLoader = jiti(__filename, { - interopDefault: true, - }); - return jitiLoader(filepath); + const jiti = createJiti(__filename, { interopDefault: true }); + return jiti.import(filepath); +}; + +const loadTypeScriptSync: Loader = (filepath) => { + const jiti = createJiti(__filename, { interopDefault: true }); + return jiti(filepath); }; const loadToml: Loader = (...args) => { @@ -57,6 +59,7 @@ const loadYaml = createCustomLoader(defaultLoaders['.yaml']); function prepareCosmiconfig( moduleName: string, legacy: boolean, + mode: 'sync' | 'async', ): { searchPlaces: string[]; loaders: Record; @@ -95,11 +98,11 @@ function prepareCosmiconfig( return { searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)), loaders: { - '.ts': loadTypeScript, - '.cts': loadTypeScript, - '.mts': loadTypeScript, - '.js': loadTypeScript, - '.mjs': loadTypeScript, + '.ts': mode === 'async' ? loadTypeScript : loadTypeScriptSync, + '.cts': mode === 'async' ? loadTypeScript : loadTypeScriptSync, + '.mts': mode === 'async' ? loadTypeScript : loadTypeScriptSync, + '.js': mode === 'async' ? loadTypeScript : loadTypeScriptSync, + '.mjs': mode === 'async' ? loadTypeScript : loadTypeScriptSync, '.json': defaultLoaders['.json'], '.yaml': loadYaml, '.yml': loadYaml, diff --git a/test/config.spec.ts b/test/config.spec.ts index 5057b9d7e..9921f9d8f 100644 --- a/test/config.spec.ts +++ b/test/config.spec.ts @@ -128,6 +128,25 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => { ['package.json', packageJsonConfig], ]; + if (mode === 'async') { + const topAwaitConfigTs = `await Promise.resolve(); export default { schema: '${schemaFilename}' } satisfies any`; + const topAwaitConfig = `await Promise.resolve(); export default { schema: '${schemaFilename}' }`; + + configFiles.push( + // #.config files + [`${moduleName}.config.ts`, topAwaitConfigTs, typeModule], + [`${moduleName}.config.js`, topAwaitConfig, typeModule], + [`${moduleName}.config.mts`, topAwaitConfigTs], + [`${moduleName}.config.mjs`, topAwaitConfig], + + // .#rc files + [`.${moduleName}rc.ts`, topAwaitConfigTs, typeModule], + [`.${moduleName}rc.js`, topAwaitConfig, typeModule], + [`.${moduleName}rc.mts`, topAwaitConfigTs], + [`.${moduleName}rc.mjs`, topAwaitConfig], + ); + } + beforeEach(() => { temp.clean(); temp.createFile(schemaFilename, testSDL);