Skip to content

Commit b52dc1b

Browse files
authored
move cosmiconfig-typescript-loader in peerDependencyMeta (#1171)
* should be good add cosmiconfig-typescript-loader to devdeps fix set node 14 in ci, clean eslint errors * add friendly error * clean peerDeps * simplify * Update .changeset/four-frogs-flash.md * just spread args
1 parent d01b878 commit b52dc1b

11 files changed

+118
-92
lines changed

.changeset/four-frogs-flash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-config': minor
3+
---
4+
5+
move `cosmiconfig-typescript-loader` in `peerDependencyMeta`

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"prepublishOnly": "yarn build",
3737
"clean": "rimraf dist",
3838
"prebuild": "yarn clean && yarn json-schema",
39+
"postbuild": "tsx scripts/postbuild.ts",
3940
"build": "bob build",
4041
"prettier": "prettier --cache --write --list-different .",
4142
"prettier:check": "prettier --cache --check .",
@@ -47,8 +48,14 @@
4748
"json-schema": "typescript-json-schema src/types.ts IGraphQLConfig --out config-schema.json --ignoreErrors --required --titles && prettier --write config-schema.json"
4849
},
4950
"peerDependencies": {
51+
"cosmiconfig-typescript-loader": "^4.0.0",
5052
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
5153
},
54+
"peerDependenciesMeta": {
55+
"cosmiconfig-typescript-loader": {
56+
"optional": true
57+
}
58+
},
5259
"dependencies": {
5360
"@graphql-tools/graphql-file-loader": "^7.3.7",
5461
"@graphql-tools/json-file-loader": "^7.3.7",
@@ -58,10 +65,8 @@
5865
"@graphql-tools/utils": "^8.6.5",
5966
"cosmiconfig": "7.0.1",
6067
"cosmiconfig-toml-loader": "1.0.0",
61-
"cosmiconfig-typescript-loader": "^4.0.0",
6268
"minimatch": "4.2.1",
6369
"string-env-interpolation": "1.0.1",
64-
"ts-node": "^10.8.1",
6570
"tslib": "^2.4.0"
6671
},
6772
"devDependencies": {
@@ -71,6 +76,7 @@
7176
"@typescript-eslint/eslint-plugin": "5.47.0",
7277
"@typescript-eslint/parser": "5.47.0",
7378
"bob-the-bundler": "4.2.0-alpha-20221222140753-fcf5286",
79+
"cosmiconfig-typescript-loader": "4.3.0",
7480
"del": "6.1.1",
7581
"eslint": "8.25.0",
7682
"eslint-config-prettier": "8.5.0",
@@ -81,6 +87,7 @@
8187
"parent-module": "2.0.0",
8288
"prettier": "2.7.1",
8389
"rimraf": "3.0.2",
90+
"ts-node": "10.9.1",
8491
"tsx": "3.12.1",
8592
"typescript": "4.8.4",
8693
"typescript-json-schema": "0.54.0",

scripts/postbuild.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable no-console */
2+
import { writeFile, readFile } from 'node:fs/promises';
3+
import path from 'node:path';
4+
5+
const filePath = path.resolve(process.cwd(), 'dist/esm/helpers/cosmiconfig.js');
6+
7+
console.time('done');
8+
const content = await readFile(filePath, 'utf8');
9+
10+
await writeFile(
11+
filePath,
12+
`
13+
import { createRequire } from 'module';
14+
const require = createRequire(import.meta.url);
15+
${content}`.trimStart(),
16+
);
17+
console.timeEnd('done');

src/config.ts

+6-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { GraphQLExtensionDeclaration, GraphQLExtensionsRegistry } from './extens
1515
import { EndpointsExtension } from './extensions/endpoints.js';
1616
import { isLegacyConfig } from './helpers/cosmiconfig.js';
1717

18-
const cwd = typeof process !== 'undefined' ? process.cwd() : undefined;
18+
const CWD = process.cwd();
1919
const defaultConfigName = 'graphql';
2020

2121
interface LoadConfigOptions {
@@ -29,7 +29,7 @@ interface LoadConfigOptions {
2929
}
3030

3131
const defaultLoadConfigOptions: LoadConfigOptions = {
32-
rootDir: cwd,
32+
rootDir: CWD,
3333
extensions: [],
3434
throwOnMissing: true,
3535
throwOnEmpty: true,
@@ -45,16 +45,8 @@ export async function loadConfig(options: LoadConfigOptions): Promise<GraphQLCon
4545

4646
try {
4747
const found = filepath
48-
? await getConfig({
49-
filepath,
50-
configName,
51-
legacy,
52-
})
53-
: await findConfig({
54-
rootDir,
55-
configName,
56-
legacy,
57-
});
48+
? await getConfig({ filepath, configName, legacy })
49+
: await findConfig({ rootDir, configName, legacy });
5850

5951
return new GraphQLConfig(found, extensions);
6052
} catch (error) {
@@ -70,16 +62,8 @@ export function loadConfigSync(options: LoadConfigOptions) {
7062

7163
try {
7264
const found = filepath
73-
? getConfigSync({
74-
filepath,
75-
configName,
76-
legacy,
77-
})
78-
: findConfigSync({
79-
rootDir,
80-
configName,
81-
legacy,
82-
});
65+
? getConfigSync({ filepath, configName, legacy })
66+
: findConfigSync({ rootDir, configName, legacy });
8367

8468
return new GraphQLConfig(found, extensions);
8569
} catch (error) {

src/errors.ts

+6
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,47 @@ export class ConfigNotFoundError extends ExtendableBuiltin(Error) {
2020
this.message = message;
2121
}
2222
}
23+
2324
export class ConfigEmptyError extends ExtendableBuiltin(Error) {
2425
constructor(message: string) {
2526
super(message);
2627
this.name = this.constructor.name;
2728
this.message = message;
2829
}
2930
}
31+
// TODO: remove in v5
3032
export class ConfigInvalidError extends ExtendableBuiltin(Error) {
3133
constructor(message: string) {
3234
super(message);
3335
this.name = this.constructor.name;
3436
this.message = message;
3537
}
3638
}
39+
3740
export class ProjectNotFoundError extends ExtendableBuiltin(Error) {
3841
constructor(message: string) {
3942
super(message);
4043
this.name = this.constructor.name;
4144
this.message = message;
4245
}
4346
}
47+
// TODO: remove in v5
4448
export class LoadersMissingError extends ExtendableBuiltin(Error) {
4549
constructor(message: string) {
4650
super(message);
4751
this.name = this.constructor.name;
4852
this.message = message;
4953
}
5054
}
55+
// TODO: remove in v5
5156
export class LoaderNoResultError extends ExtendableBuiltin(Error) {
5257
constructor(message: string) {
5358
super(message);
5459
this.name = this.constructor.name;
5560
this.message = message;
5661
}
5762
}
63+
5864
export class ExtensionMissingError extends ExtendableBuiltin(Error) {
5965
constructor(message: string) {
6066
super(message);

src/helpers/cosmiconfig.ts

+27-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig';
2-
import { TypeScriptLoader } from 'cosmiconfig-typescript-loader';
32
import { loadToml } from 'cosmiconfig-toml-loader';
43
import { env } from 'string-env-interpolation';
54

@@ -9,49 +8,47 @@ export interface ConfigSearchResult {
98
isEmpty?: boolean;
109
}
1110

12-
const legacySearchPlaces = ['.graphqlconfig', '.graphqlconfig.json', '.graphqlconfig.yaml', '.graphqlconfig.yml'];
11+
const legacySearchPlaces = [
12+
'.graphqlconfig',
13+
'.graphqlconfig.json',
14+
'.graphqlconfig.yaml',
15+
'.graphqlconfig.yml',
16+
] as const;
1317

14-
export function isLegacyConfig(filepath: string): boolean {
15-
filepath = filepath.toLowerCase();
16-
17-
return legacySearchPlaces.some((name) => filepath.endsWith(name));
18+
export function isLegacyConfig(filePath: string): boolean {
19+
filePath = filePath.toLowerCase();
20+
return legacySearchPlaces.some((name) => filePath.endsWith(name));
1821
}
1922

2023
function transformContent(content: string): string {
2124
return env(content);
2225
}
2326

24-
const createCustomLoader = (loader: Loader): Loader => {
25-
return (filepath, content) => {
26-
return loader(filepath, transformContent(content));
27-
};
28-
};
27+
function createCustomLoader(loader: Loader): Loader {
28+
return (filePath, content) => loader(filePath, transformContent(content));
29+
}
2930

30-
export function createCosmiConfig(
31-
moduleName: string,
32-
{
33-
legacy,
34-
}: {
35-
legacy: boolean;
36-
},
37-
) {
38-
const options = prepareCosmiconfig(moduleName, {
39-
legacy,
40-
});
31+
export function createCosmiConfig(moduleName: string, legacy: boolean) {
32+
const options = prepareCosmiconfig(moduleName, legacy);
4133

4234
return cosmiconfig(moduleName, options);
4335
}
4436

45-
export function createCosmiConfigSync(moduleName: string, { legacy }: { legacy: boolean }) {
46-
const options = prepareCosmiconfig(moduleName, { legacy });
37+
export function createCosmiConfigSync(moduleName: string, legacy: boolean) {
38+
const options = prepareCosmiconfig(moduleName, legacy);
4739

4840
return cosmiconfigSync(moduleName, options);
4941
}
5042

51-
function prepareCosmiconfig(moduleName: string, { legacy }: { legacy: boolean }) {
43+
const loadTypeScript: Loader = (...args) => {
44+
// eslint-disable-next-line @typescript-eslint/no-var-requires
45+
const { TypeScriptLoader } = require('cosmiconfig-typescript-loader');
46+
47+
return TypeScriptLoader({ transpileOnly: true })(...args);
48+
};
49+
50+
function prepareCosmiconfig(moduleName: string, legacy: boolean) {
5251
const loadYaml = createCustomLoader(defaultLoaders['.yaml']);
53-
const loadTomlCustom = createCustomLoader(loadToml);
54-
const loadJson = createCustomLoader(defaultLoaders['.json']);
5552

5653
const searchPlaces = [
5754
'#.config.ts',
@@ -81,12 +78,12 @@ function prepareCosmiconfig(moduleName: string, { legacy }: { legacy: boolean })
8178
return {
8279
searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)),
8380
loaders: {
84-
'.ts': TypeScriptLoader({ transpileOnly: true }),
81+
'.ts': loadTypeScript,
8582
'.js': defaultLoaders['.js'],
86-
'.json': loadJson,
83+
'.json': createCustomLoader(defaultLoaders['.json']),
8784
'.yaml': loadYaml,
8885
'.yml': loadYaml,
89-
'.toml': loadTomlCustom,
86+
'.toml': createCustomLoader(loadToml),
9087
noExt: loadYaml,
9188
},
9289
};

src/helpers/find-config.ts

+15-23
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,37 @@ import { ConfigNotFoundError, ConfigEmptyError, composeMessage } from '../errors
22
import { GraphQLConfigResult } from '../types.js';
33
import { createCosmiConfig, createCosmiConfigSync, ConfigSearchResult } from './cosmiconfig.js';
44

5-
const cwd = typeof process !== 'undefined' ? process.cwd() : undefined;
5+
const CWD = process.cwd();
66

7-
export async function findConfig({
8-
rootDir = cwd,
9-
legacy = true,
10-
configName,
11-
}: {
7+
type FindConfigOptions = {
128
rootDir: string;
139
configName: string;
1410
legacy?: boolean;
15-
}): Promise<GraphQLConfigResult> {
16-
validate({ rootDir });
11+
};
12+
13+
export async function findConfig({
14+
rootDir = CWD,
15+
legacy = true,
16+
configName,
17+
}: FindConfigOptions): Promise<GraphQLConfigResult> {
18+
validate(rootDir);
1719

1820
return resolve({
1921
rootDir,
20-
result: await createCosmiConfig(configName, { legacy }).search(rootDir),
22+
result: await createCosmiConfig(configName, legacy).search(rootDir),
2123
});
2224
}
2325

24-
export function findConfigSync({
25-
rootDir = cwd,
26-
legacy = true,
27-
configName,
28-
}: {
29-
rootDir: string;
30-
configName: string;
31-
legacy?: boolean;
32-
}): GraphQLConfigResult {
33-
validate({ rootDir });
26+
export function findConfigSync({ rootDir = CWD, legacy = true, configName }: FindConfigOptions): GraphQLConfigResult {
27+
validate(rootDir);
3428

3529
return resolve({
3630
rootDir,
37-
result: createCosmiConfigSync(configName, { legacy }).search(rootDir),
31+
result: createCosmiConfigSync(configName, legacy).search(rootDir),
3832
});
3933
}
4034

41-
//
42-
43-
function validate({ rootDir }: { rootDir: string }) {
35+
function validate(rootDir: string): void {
4436
if (!rootDir) {
4537
throw new Error(`Defining a root directory is required`);
4638
}

src/helpers/get-config.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export async function getConfig({
1111
configName: string;
1212
legacy?: boolean;
1313
}): Promise<GraphQLConfigResult> {
14-
validate({ filepath });
14+
validate(filepath);
1515

1616
return resolve({
17-
result: await createCosmiConfig(configName, { legacy }).load(filepath),
17+
result: await createCosmiConfig(configName, legacy).load(filepath),
1818
filepath,
1919
});
2020
}
@@ -28,10 +28,10 @@ export function getConfigSync({
2828
configName: string;
2929
legacy?: boolean;
3030
}): GraphQLConfigResult {
31-
validate({ filepath });
31+
validate(filepath);
3232

3333
return resolve({
34-
result: createCosmiConfigSync(configName, { legacy }).load(filepath),
34+
result: createCosmiConfigSync(configName, legacy).load(filepath),
3535
filepath,
3636
});
3737
}
@@ -55,7 +55,7 @@ function resolve({ result, filepath }: { result?: ConfigSearchResult; filepath:
5555
};
5656
}
5757

58-
function validate({ filepath }: { filepath?: string }) {
58+
function validate(filepath: string): void {
5959
if (!filepath) {
6060
throw new Error(`Defining a file path is required`);
6161
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
export { Source, Loader } from '@graphql-tools/utils';
12
export { GraphQLConfig, loadConfig, loadConfigSync } from './config.js';
23
export { GraphQLProjectConfig } from './project-config.js';
34
export { GraphQLExtensionDeclaration } from './extension.js';
4-
export { Source, Loader } from '@graphql-tools/utils';
55
export * from './types.js';
66
export * from './errors.js';
77
export { LoadersRegistry } from './loaders.js';

tsconfig.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"compilerOptions": {
3-
"jsx": "react",
43
"module": "esnext",
54
"target": "es2018",
6-
"lib": ["es2017", "dom", "esnext.asynciterable"],
5+
"lib": ["es2018"],
76
"outDir": "dist",
87
"declaration": true,
98
"declarationMap": true,
@@ -17,6 +16,6 @@
1716
"graphql-config": ["./src/index.ts"]
1817
}
1918
},
20-
"exclude": ["node_modules", "dist", "website"],
21-
"include": ["src", "website/src/components"]
19+
"exclude": [],
20+
"include": ["src"]
2221
}

0 commit comments

Comments
 (0)