diff --git a/packages/graphql-codegen-cli/CHANGELOG.md b/packages/graphql-codegen-cli/CHANGELOG.md index 0012f3dc1a4..2822294da40 100644 --- a/packages/graphql-codegen-cli/CHANGELOG.md +++ b/packages/graphql-codegen-cli/CHANGELOG.md @@ -1,5 +1,14 @@ # @graphql-codegen/cli +## 7.1.2 + +### Patch Changes + +- [#10861](https://github.com/dotansimha/graphql-code-generator/pull/10861) + [`a2e1093`](https://github.com/dotansimha/graphql-code-generator/commit/a2e109397eda1bdee51864eb3e713afce5ef7771) + Thanks [@eddeee888](https://github.com/eddeee888)! - Fix CLI's `require` flag when resolving + module issue in ESM in native Windows + ## 7.1.1 ### Patch Changes diff --git a/packages/graphql-codegen-cli/package.json b/packages/graphql-codegen-cli/package.json index d724779e11e..b14ed8fc034 100644 --- a/packages/graphql-codegen-cli/package.json +++ b/packages/graphql-codegen-cli/package.json @@ -1,6 +1,6 @@ { "name": "@graphql-codegen/cli", - "version": "7.1.1", + "version": "7.1.2", "type": "module", "repository": { "type": "git", diff --git a/packages/graphql-codegen-cli/src/codegen.ts b/packages/graphql-codegen-cli/src/codegen.ts index 217cef5256c..d6d54f05c57 100644 --- a/packages/graphql-codegen-cli/src/codegen.ts +++ b/packages/graphql-codegen-cli/src/codegen.ts @@ -18,17 +18,11 @@ import { NoTypeDefinitionsFound, type UnnormalizedTypeDefPointer } from '@graphq import { mergeTypeDefs } from '@graphql-tools/merge'; import { CodegenContext, ensureContext } from './config.js'; import { getDocumentTransform } from './documentTransforms.js'; +import { isESMModule } from './isESMModule.js'; import { getPluginByName } from './plugins.js'; import { getPresetByName } from './presets.js'; import { debugLog, printLogs } from './utils/debugging.js'; -/** - * Poor mans ESM detection. - * Looking at this and you have a better method? - * Send a PR. - */ -const isESMModule = (typeof __dirname === 'string') === false; - const makeDefaultLoader = (from: string) => { if (fs.statSync(from).isDirectory()) { from = path.join(from, '__fake.js'); diff --git a/packages/graphql-codegen-cli/src/config.ts b/packages/graphql-codegen-cli/src/config.ts index 49d1f7e7ade..4103c5cf002 100644 --- a/packages/graphql-codegen-cli/src/config.ts +++ b/packages/graphql-codegen-cli/src/config.ts @@ -1,6 +1,7 @@ import { BinaryToTextEncoding, createHash } from 'crypto'; import { promises } from 'fs'; import { createRequire } from 'module'; +import * as url from 'node:url'; import { resolve } from 'path'; import { cosmiconfig, defaultLoaders } from 'cosmiconfig'; import { GraphQLSchema, GraphQLSchemaExtensions, print } from 'graphql'; @@ -18,6 +19,7 @@ import { } from '@graphql-codegen/plugin-helpers'; import type { UnnormalizedTypeDefPointer } from '@graphql-tools/load'; import { findAndLoadGraphQLConfig } from './graphql-config.js'; +import { isESMModule } from './isESMModule.js'; import { defaultDocumentsLoadOptions, defaultSchemaLoadOptions, @@ -293,13 +295,12 @@ export async function createContext( if (cliFlags.require && cliFlags.require.length > 0) { const relativeRequire = createRequire(process.cwd()); await Promise.all( - cliFlags.require.map( - mod => - import( - relativeRequire.resolve(mod, { - paths: [process.cwd()], - }) - ), + cliFlags.require.map(mod => + safeDynamicImport( + relativeRequire.resolve(mod, { + paths: [process.cwd()], + }), + ), ), ); } @@ -544,3 +545,24 @@ async function addMetadataToSources( }), ); } + +/** + * `safeDynamicImport` is a wrapper of dynamic `import()` + * to work across Linux and Windows + * + * CJS: + * `import()` seems to work well in CJS when given resolved filename + * + * ESM: + * On native Windows (i.e. no WSL or CI), filename may look like this: `C:\\Users\\path\\to\\file.ts` + * If used directly with `import()`, we'll see `ERR_UNSUPPORTED_ESM_URL_SCHEME` error because `c:` is not a valid protocol + * `url.pathToFileURL` turns the filename to `file:///C:/Users/path/to/file.ts`, which is import-able + */ +const safeDynamicImport = (absoluteFilename: string): Promise => { + if (isESMModule) { + const { href: fileUrl } = url.pathToFileURL(absoluteFilename); + return import(fileUrl); + } + + return import(absoluteFilename); +}; diff --git a/packages/graphql-codegen-cli/src/isESMModule.ts b/packages/graphql-codegen-cli/src/isESMModule.ts new file mode 100644 index 00000000000..c49ff12ea7e --- /dev/null +++ b/packages/graphql-codegen-cli/src/isESMModule.ts @@ -0,0 +1,6 @@ +/** + * Poor mans ESM detection. + * Looking at this and you have a better method? + * Send a PR. + */ +export const isESMModule = (typeof __dirname === 'string') === false;