From 32c1560f1669f6726008226c0e467a7fcc90aa84 Mon Sep 17 00:00:00 2001 From: Scott Tolinski Date: Fri, 15 Jul 2022 16:47:43 -0600 Subject: [PATCH] =?UTF-8?q?getPluginByName=20fails=20unexpectedly=20when?= =?UTF-8?q?=20plugin=20is=20not=20prefixed=20with=20@=E2=80=A6=20(#8097)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * getPluginByName fails unexpectedly when plugin is not prefixed with @graphql-codgen/ We've been finding that our error code thrown is not `MODULE_NOT_FOUND` but rather `ERR_MODULE_NOT_FOUND` leading to codegen cli throwing and failing plugin load when trying the first plugin option. Since the error returned isn't `MODULE_NOT_FOUND`. As small double check here by adding the || `ERR_MODULE_NOT_FOUND` should account for that case. IMO this is a small but pretty urgent fix as it could be incorrectly throwing in some environments. I don't personally know the cause for the different error code, but our repo is via Vite / Node 17 and is ESM based. * Oopsie, need && * Create green-dogs-wait.md --- .changeset/green-dogs-wait.md | 8 ++++++++ packages/graphql-codegen-cli/src/plugins.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 .changeset/green-dogs-wait.md diff --git a/.changeset/green-dogs-wait.md b/.changeset/green-dogs-wait.md new file mode 100644 index 00000000000..1703fd1a292 --- /dev/null +++ b/.changeset/green-dogs-wait.md @@ -0,0 +1,8 @@ +--- +"@graphql-codegen/cli": patch +--- + +getPluginByName fails unexpectedly when plugin is not prefixed with @graphq-codegen in ESM context + +MODULE_NOT_FOUND is the error code you receive in a CommonJS context when you require() a module and it does not exist. +ERR_MODULE_NOT_FOUND is the error code you receive in an ESM context when you import or import() ad module that does not exist. diff --git a/packages/graphql-codegen-cli/src/plugins.ts b/packages/graphql-codegen-cli/src/plugins.ts index 1c846911b2a..6bf8aaf8912 100644 --- a/packages/graphql-codegen-cli/src/plugins.ts +++ b/packages/graphql-codegen-cli/src/plugins.ts @@ -22,7 +22,7 @@ export async function getPluginByName( try { return await pluginLoader(moduleName); } catch (err) { - if (err.code !== 'MODULE_NOT_FOUND') { + if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') { throw new DetailedError( `Unable to load template plugin matching ${name}`, `