Skip to content

Commit 398b094

Browse files
authoredSep 28, 2020
fix(cli): import relative to user config (#4688)
* fix(cli): import relative to user config * chore: changeset
1 parent f603b8f commit 398b094

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed
 

‎.changeset/yellow-apes-cover.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/cli': patch
3+
---
4+
5+
Load user provided things relative to the config

‎packages/graphql-codegen-cli/src/codegen.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,21 @@ import { getPresetByName } from './presets';
1515
import { debugLog } from './utils/debugging';
1616
import { printSchemaWithDirectives } from '@graphql-tools/utils';
1717
import { CodegenContext, ensureContext } from './config';
18+
import path from 'path';
19+
// eslint-disable-next-line
20+
import { createRequire, createRequireFromPath } from 'module';
1821

19-
export const defaultLoader = (mod: string) => import(mod);
22+
const makeDefaultLoader = (from: string) => {
23+
if (!path.extname(from)) {
24+
from = path.join(from, '__fake.js');
25+
}
26+
27+
const relativeRequire = (createRequire || createRequireFromPath)(from);
28+
29+
return (mod: string) => {
30+
return import(relativeRequire.resolve(mod));
31+
};
32+
};
2033

2134
export async function executeCodegen(input: CodegenContext | Types.Config): Promise<Types.FileOutput[]> {
2235
function wrapTask(task: () => void | Promise<void>, source: string) {
@@ -73,8 +86,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
7386
async function normalize() {
7487
/* Load Require extensions */
7588
const requireExtensions = normalizeInstanceOrArray<string>(config.require);
89+
const loader = makeDefaultLoader(context.cwd);
7690
for (const mod of requireExtensions) {
77-
await import(mod);
91+
await loader(mod);
7892
}
7993

8094
/* Root plugin config */
@@ -218,14 +232,14 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
218232
debugLog(`[CLI] Generating output`);
219233

220234
const normalizedPluginsArray = normalizeConfig(outputConfig.plugins);
221-
const pluginLoader = config.pluginLoader || defaultLoader;
235+
const pluginLoader = config.pluginLoader || makeDefaultLoader(context.cwd);
222236
const pluginPackages = await Promise.all(
223237
normalizedPluginsArray.map(plugin => getPluginByName(Object.keys(plugin)[0], pluginLoader))
224238
);
225239
const pluginMap: { [name: string]: CodegenPlugin } = {};
226240
const preset: Types.OutputPreset = hasPreset
227241
? typeof outputConfig.preset === 'string'
228-
? await getPresetByName(outputConfig.preset, defaultLoader)
242+
? await getPresetByName(outputConfig.preset, makeDefaultLoader(context.cwd))
229243
: outputConfig.preset
230244
: null;
231245

‎packages/graphql-codegen-cli/src/presets.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { DetailedError, Types } from '@graphql-codegen/plugin-helpers';
2-
import { resolve } from 'path';
32

43
export async function getPresetByName(
54
name: string,
65
loader: Types.PackageLoaderFn<{ preset?: Types.OutputPreset; default?: Types.OutputPreset }>
76
): Promise<Types.OutputPreset> {
87
const possibleNames = [`@graphql-codegen/${name}`, `@graphql-codegen/${name}-preset`, name];
9-
const possibleModules = possibleNames.concat(resolve(process.cwd(), name));
108

11-
for (const moduleName of possibleModules) {
9+
for (const moduleName of possibleNames) {
1210
try {
1311
const loaded = await loader(moduleName);
1412

0 commit comments

Comments
 (0)
Please sign in to comment.