Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use custom plugin in ES module project #6049

Closed
AndreasHald opened this issue May 25, 2021 · 4 comments
Closed

How to use custom plugin in ES module project #6049

AndreasHald opened this issue May 25, 2021 · 4 comments
Labels
kind/enhancement New feature or request

Comments

@AndreasHald
Copy link

Describe the bug
Hi, we recently updated our project to use ES modules by adding "type": "module", in our package.json file.

We use a custom plugin to generate utility functions for our project, this custom plugin is simply a js file we include in our codegen.yml like so:

schema: ...
generates:
   ./src/generated/graphql.ts
      schema: ...
      plugins:
         - ./our-custom-plugin.js

In order to comply with ES6 I converted our plugin file from

module.exports = {
   plugin: (...) => {
   }
}

To

export default {
   plugin: (...) => {
   }
}

However when running the codegen we get the following error

Unable to load template plugin matching './our-custom-plugin.js'.
                  Reason: 
                    Must use import to load ES Module: /Users/.../our-custom-plugin.js

The error appears to occur here const pluginLoader = config.pluginLoader || makeDefaultLoader(context.cwd);

I'm guessing that the makeDefaultLoader is using require? I can see that it appears to be possible to supply your own loader? is there documentation of this somewhere? I haven't been able to find it.

Are there another suggested approach to this?

@dotansimha
Copy link
Owner

Hi @AndreasHald !
Thank you for creating this. We are currently working on adding ESM support: #6102

cc @PabloSzx

@dotansimha dotansimha added the kind/enhancement New feature or request label Jun 20, 2021
@AndreasHald
Copy link
Author

Hi again @dotansimha, I saw that #6102 was closed and released, was that supposed to fix this issue, because even after installing @latest I'm still seeing it.

@PabloSzx
Copy link
Contributor

PabloSzx commented Aug 25, 2021

This issue is due to prevent breaking changes and to be able to do require relative to a specific path, in the ESM support PR the require with "createRequire" was kept for the imports for the cli in config, but it is currently possible to do programmatic usage of graphql-codegen and importing using full ESM all the way

oh, and btw, if you have "type": "module" and you have CJS files, simply use the ".cjs" extension and everything will work just fine

- ./our-custom-plugin.cjs

@alexbjorlig
Copy link

This issue is due to prevent breaking changes and to be able to do require relative to a specific path, in the ESM support PR the require with "createRequire" was kept for the imports for the cli in config, but it is currently possible to do programmatic usage of graphql-codegen and importing using full ESM all the way

oh, and btw, if you have "type": "module" and you have CJS files, simply use the ".cjs" extension and everything will work just fine

- ./our-custom-plugin.cjs

This is fucking awesome 🚀
I have been SO tired of removing "type": "module" generate code, then remember to add "type": "module", before everything explodes in CI. Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants