Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/graphql-codegen-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphql-codegen/cli",
"version": "7.1.1",
"version": "7.1.2",
"type": "module",
"repository": {
"type": "git",
Expand Down
8 changes: 1 addition & 7 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
36 changes: 29 additions & 7 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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()],
}),
),
),
);
}
Expand Down Expand Up @@ -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<any> => {
if (isESMModule) {
const { href: fileUrl } = url.pathToFileURL(absoluteFilename);
return import(fileUrl);
}

return import(absoluteFilename);
};
6 changes: 6 additions & 0 deletions packages/graphql-codegen-cli/src/isESMModule.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading