ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
132 stars 47 forks source link

Using typesFile with ESM/moduleResolution nodenext causes build error due to missing ".js" extension #163

Open joshleitzel opened 1 month ago

joshleitzel commented 1 month ago

When using the config.typesFile option in a TypeScript project with moduleResolution set to node16 or nodenext, this package will always cause an error when building the project. This is because TypeScript expects local imports to have a .js extension. For example this config:

config: {
  typesFile: "types/generated"
}

will generate an import like this:

import { Type1, Type2 } from "types/generated";

which will in turn result in the error:

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean 'types/generated.js'?

And you can't work around it by appending the .js yourself:

config: {
  typesFile: "types/generated.js"
}

because the extension is explicitly stripped.

I'm not sure what the permanent solution here is, but I suspect the higher-level config's emitLegacyCommonJSImports value should be respected.

I worked around this for now by patch-packaging the source code to remove the extension stripping.

ardeois commented 3 weeks ago

Yes you're right we should probably add support for emitLegacyCommonJSImports option

Feel free to open a PR