Skip to content

Commit 790cfc1

Browse files
authored
feat: support top-level await (#1631)
* Add tests * Support top-level await * Add changeset
1 parent 12915b5 commit 790cfc1

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

.changeset/fresh-bikes-kneel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-config': patch
3+
---
4+
5+
support top-level await

src/helpers/cosmiconfig.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig';
22
import { env } from 'string-env-interpolation';
3-
import jiti from 'jiti';
3+
import { createJiti } from 'jiti';
44

55
export interface ConfigSearchResult {
66
config: any;
@@ -29,21 +29,23 @@ function createCustomLoader(loader: Loader): Loader {
2929
}
3030

3131
export function createCosmiConfig(moduleName: string, legacy: boolean) {
32-
const options = prepareCosmiconfig(moduleName, legacy);
33-
32+
const options = prepareCosmiconfig(moduleName, legacy, 'async');
3433
return cosmiconfig(moduleName, options);
3534
}
3635

3736
export function createCosmiConfigSync(moduleName: string, legacy: boolean) {
38-
const options = prepareCosmiconfig(moduleName, legacy);
37+
const options = prepareCosmiconfig(moduleName, legacy, 'sync');
3938
return cosmiconfigSync(moduleName, options);
4039
}
4140

4241
const loadTypeScript: Loader = (filepath) => {
43-
const jitiLoader = jiti(__filename, {
44-
interopDefault: true,
45-
});
46-
return jitiLoader(filepath);
42+
const jiti = createJiti(__filename, { interopDefault: true });
43+
return jiti.import(filepath);
44+
};
45+
46+
const loadTypeScriptSync: Loader = (filepath) => {
47+
const jiti = createJiti(__filename, { interopDefault: true });
48+
return jiti(filepath);
4749
};
4850

4951
const loadToml: Loader = (...args) => {
@@ -57,6 +59,7 @@ const loadYaml = createCustomLoader(defaultLoaders['.yaml']);
5759
function prepareCosmiconfig(
5860
moduleName: string,
5961
legacy: boolean,
62+
mode: 'sync' | 'async',
6063
): {
6164
searchPlaces: string[];
6265
loaders: Record<string, Loader>;
@@ -95,11 +98,11 @@ function prepareCosmiconfig(
9598
return {
9699
searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)),
97100
loaders: {
98-
'.ts': loadTypeScript,
99-
'.cts': loadTypeScript,
100-
'.mts': loadTypeScript,
101-
'.js': loadTypeScript,
102-
'.mjs': loadTypeScript,
101+
'.ts': mode === 'async' ? loadTypeScript : loadTypeScriptSync,
102+
'.cts': mode === 'async' ? loadTypeScript : loadTypeScriptSync,
103+
'.mts': mode === 'async' ? loadTypeScript : loadTypeScriptSync,
104+
'.js': mode === 'async' ? loadTypeScript : loadTypeScriptSync,
105+
'.mjs': mode === 'async' ? loadTypeScript : loadTypeScriptSync,
103106
'.json': defaultLoaders['.json'],
104107
'.yaml': loadYaml,
105108
'.yml': loadYaml,

test/config.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
128128
['package.json', packageJsonConfig],
129129
];
130130

131+
if (mode === 'async') {
132+
const topAwaitConfigTs = `await Promise.resolve(); export default { schema: '${schemaFilename}' } satisfies any`;
133+
const topAwaitConfig = `await Promise.resolve(); export default { schema: '${schemaFilename}' }`;
134+
135+
configFiles.push(
136+
// #.config files
137+
[`${moduleName}.config.ts`, topAwaitConfigTs, typeModule],
138+
[`${moduleName}.config.js`, topAwaitConfig, typeModule],
139+
[`${moduleName}.config.mts`, topAwaitConfigTs],
140+
[`${moduleName}.config.mjs`, topAwaitConfig],
141+
142+
// .#rc files
143+
[`.${moduleName}rc.ts`, topAwaitConfigTs, typeModule],
144+
[`.${moduleName}rc.js`, topAwaitConfig, typeModule],
145+
[`.${moduleName}rc.mts`, topAwaitConfigTs],
146+
[`.${moduleName}rc.mjs`, topAwaitConfig],
147+
);
148+
}
149+
131150
beforeEach(() => {
132151
temp.clean();
133152
temp.createFile(schemaFilename, testSDL);

0 commit comments

Comments
 (0)