Skip to content

Commit

Permalink
Avoid unnecessarily importing isType from graphql.
Browse files Browse the repository at this point in the history
To import `graphql` (and its internal `instanceOf` function) for the
first time with `process.env.NODE_ENV` safely polyfilled, PR #8347
selected a somewhat arbitrary export of the `graphql` package that uses
`instanceOf`, specifically the `isType` function exported by
`graphql/type/definition`.

As revealed by issue #8705, importing `isType` was a bad choice, since
it depends on a bunch of other code within the `graphql` package,
unnecessarily increasing minified+gzip bundle size by 3.4kB.

A better choice is the `Source` constructor, which is already imported
thanks to other necessary imports, and also imports/uses `instanceOf`.
  • Loading branch information
benjamn committed Oct 4, 2021
1 parent 17df471 commit 6f19b8e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/utilities/globals/fix-graphql.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// The ordering of these imports is important, because it ensures the temporary
// process.env.NODE_ENV polyfill is defined globally (if necessary) before we
// import { isType } from 'graphql'. The instanceOf function that we really care
// import { Source } from 'graphql'. The instanceOf function that we really care
// about (the one that uses process.env.NODE_ENV) is not exported from the
// top-level graphql package, but isType uses instanceOf, and is exported.
// top-level graphql package, but graphql/language/source uses instanceOf, and
// has relatively few dependencies, so importing it here should not increase
// bundle sizes as much as other options.
import { remove } from 'ts-invariant/process';
import { isType } from 'graphql';
import { Source } from 'graphql';

export function removeTemporaryGlobals() {
// Calling isType here just to make sure it won't be tree-shaken away,
// provided applyFixes is called elsewhere.
isType(null);
return remove();
// Using Source here here just to make sure it won't be tree-shaken away.
return typeof Source === "function" ? remove() : remove();
}

0 comments on commit 6f19b8e

Please sign in to comment.