@@ -15,8 +15,21 @@ import { getPresetByName } from './presets';
15
15
import { debugLog } from './utils/debugging' ;
16
16
import { printSchemaWithDirectives } from '@graphql-tools/utils' ;
17
17
import { CodegenContext , ensureContext } from './config' ;
18
+ import path from 'path' ;
19
+ // eslint-disable-next-line
20
+ import { createRequire , createRequireFromPath } from 'module' ;
18
21
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
+ } ;
20
33
21
34
export async function executeCodegen ( input : CodegenContext | Types . Config ) : Promise < Types . FileOutput [ ] > {
22
35
function wrapTask ( task : ( ) => void | Promise < void > , source : string ) {
@@ -73,8 +86,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
73
86
async function normalize ( ) {
74
87
/* Load Require extensions */
75
88
const requireExtensions = normalizeInstanceOrArray < string > ( config . require ) ;
89
+ const loader = makeDefaultLoader ( context . cwd ) ;
76
90
for ( const mod of requireExtensions ) {
77
- await import ( mod ) ;
91
+ await loader ( mod ) ;
78
92
}
79
93
80
94
/* Root plugin config */
@@ -218,14 +232,14 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
218
232
debugLog ( `[CLI] Generating output` ) ;
219
233
220
234
const normalizedPluginsArray = normalizeConfig ( outputConfig . plugins ) ;
221
- const pluginLoader = config . pluginLoader || defaultLoader ;
235
+ const pluginLoader = config . pluginLoader || makeDefaultLoader ( context . cwd ) ;
222
236
const pluginPackages = await Promise . all (
223
237
normalizedPluginsArray . map ( plugin => getPluginByName ( Object . keys ( plugin ) [ 0 ] , pluginLoader ) )
224
238
) ;
225
239
const pluginMap : { [ name : string ] : CodegenPlugin } = { } ;
226
240
const preset : Types . OutputPreset = hasPreset
227
241
? typeof outputConfig . preset === 'string'
228
- ? await getPresetByName ( outputConfig . preset , defaultLoader )
242
+ ? await getPresetByName ( outputConfig . preset , makeDefaultLoader ( context . cwd ) )
229
243
: outputConfig . preset
230
244
: null ;
231
245
0 commit comments