graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

rollup-plugin-inject: failed to parse graphql/jsutils/instanceOf.mjs

Open iBobik opened this issue 1 year ago • 7 comments

When this library is used in the Nuxt 3 project and compiled for Cloudflare it fails like this:

ℹ Building Nitro Server (preset: cloudflare-pages)                  nitro 19:16:00
(inject plugin) rollup-plugin-inject: failed to parse /home/projects/github-twqhau-upewai/node_modules/graphql/jsutils/instanceOf.mjs. Consider restricting the plugin to particular files via options.include

[nitro 19:16:14]  ERROR  RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)


10:   /* c8 ignore next 6 */
11:   // FIXME: https://github.com/graphql/graphql-js/issues/2317
12:   globalThis.process && globalThis.process.env.NODE_ENV === 'production'
                                       ^
13:     ? function instanceOf(value, constructor) {
14:         return value instanceof constructor;


[19:16:14]  ERROR  Unexpected token (Note that you need plugins to import files that are not JavaScript)

  at error (<anonymous>)
  at setSource (<anonymous>)
  at  (<anonymous>)

Happens in version 16.7.1 and 16.7.0 but works with 16.6.0 and 17.0.0-alpha.2.

Repro: https://stackblitz.com/edit/github-twqhau-upewai Start with: NITRO_PRESET=cloudflare-pages npm run build

iBobik avatar Jun 27 '23 17:06 iBobik

This is also tracked as an issue in Nuxt and nitro: nuxt/nuxt#21768. Other issues like #3918 also refer to the same problem, but it seem it will be solved soon by PR #3927.

The module tries to access globalThis.process, which makes the Cloudflare pages deployment fail. You can't access process in Cloudflare, as it's a node-only API. Technically it should be poyfilled by unenv, but there seems to be an issue preventing this. As a work-around, in our project we are currently patching the respective code in the graphql module.

pnpm patch graphql
code <generated_folder>

In node_modules/graphql/jsutils/instanceOf.mjs remove the code that tries to access process.

export const instanceOf = function instanceOf(value, constructor) {
  return value instanceof constructor
}

Apply the patch.

pnpm patch-commit <generated_folder>

The resulting patch should appear in package.json to be applied during deployment on Cloudflare.

  "pnpm": {
    "patchedDependencies": {
      "[email protected]": "patches/[email protected]"
    }
  }

Aietes avatar Jun 29 '23 09:06 Aietes

Any updates on the fix ? I was checking the PR but that also doesnt seem to have merged.

utsavkapoor avatar Sep 26 '23 19:09 utsavkapoor

Bumping this...

jgalbsss avatar Nov 30 '23 17:11 jgalbsss

UP

websmithcode avatar Jan 12 '24 21:01 websmithcode

UP

sofialapteva avatar Mar 21 '24 11:03 sofialapteva

It appears this is still not fixed. I just ran into it the last few days. Any word on when it'll get fixed?

luisfrocha avatar May 10 '24 14:05 luisfrocha

https://github.com/graphql/graphql-js/pull/4022 seems close to ready.

scottopherson avatar May 10 '24 15:05 scottopherson

This is fixed in https://github.com/graphql/graphql-js/pull/4022 and published in GraphQL 16.8.2, you can find the instructions for various bundlers here on how to remove it in production.

JoviDeCroock avatar Jul 11 '24 18:07 JoviDeCroock