Skip to content

Commit

Permalink
feat: support top-level await (#1631)
Browse files Browse the repository at this point in the history
* Add tests

* Support top-level await

* Add changeset
  • Loading branch information
aaronadamsCA authored Feb 17, 2025
1 parent 12915b5 commit 790cfc1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-bikes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-config': patch
---

support top-level await
29 changes: 16 additions & 13 deletions src/helpers/cosmiconfig.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -57,6 +59,7 @@ const loadYaml = createCustomLoader(defaultLoaders['.yaml']);
function prepareCosmiconfig(
moduleName: string,
legacy: boolean,
mode: 'sync' | 'async',
): {
searchPlaces: string[];
loaders: Record<string, Loader>;
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 790cfc1

Please sign in to comment.