Skip to content

Commit 4db691e

Browse files
JoviDeCroockyaacovCR
authored andcommitted
fix: remove globalThis check and align with what bundlers can accept (graphql#4022)
As surfaced in [Discord](https://discord.com/channels/625400653321076807/862957336082645006/1206980831915282532) this currently is a breaking change in the 16.x.x release line which is preventing folks from upgrading towards a security fix. This PR should result in a patch release on the 16 release line. This change was originally introduced to support CFW and browser environments which should still be supported with the `typeof` check CC @n1ru4l This also adds a check whether `.env` is present as in the DOM using `id="process"` defines that as a global which we don't want to access on accident. as shown in graphql#4017 Bundles also target `process.env.NODE_ENV` specifically which fails when it replaces `globalThis.process.env.NODE_ENV` as this becomes `globalThis."production"` which is invalid syntax. Fixes graphql#3978 Fixes graphql#3918 Fixes graphql#3928 Fixes graphql#3758 Fixes graphql#3934 This purposefully does not account for graphql#3925 as we can't address this without breaking CF/plain browsers so the small byte-size increase will be expected for bundled browser environments. As a middle ground we did optimise the performance here. We can revisit this for v17. Most bundlers will be able to tree-shake this with a little help, in graphql#4075 (comment) you can find a conclusion with a repo where we discuss a few. - Next.JS by default replaces [`process.env.NODE_ENV`](https://github.com/vercel/next.js/blob/b0ab0fe85fe8c93792051b058e060724ff373cc2/packages/next/webpack.config.js#L182) you can add `typeof process` linearly - Vite allows you to specify [`config.define`](https://vitejs.dev/config/shared-options.html#define) - ESBuild by default will replace `process.env.NODE_ENV` but does not support replacing `typeof process` - Rollup has a plugin for this https://www.npmjs.com/package/@rollup/plugin-replace Supersedes graphql#4021 Supersedes graphql#4019 Supersedes graphql#3927 > This now also adds a documentation page on how to remove all of these
1 parent 9bba83f commit 4db691e

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

cspell.yml

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ overrides:
2020
words:
2121
- clsx
2222
- infima
23+
- noopener
24+
- Vite
25+
- craco
26+
- esbuild
27+
- swcrc
28+
- noreferrer
29+
- xlink
2330

2431
validateDirectives: true
2532
ignoreRegExpList:

src/jsutils/instanceOf.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { inspect } from './inspect.js';
22

3+
/* c8 ignore next 3 */
4+
const isProduction =
5+
globalThis.process != null &&
6+
// eslint-disable-next-line no-undef
7+
process.env.NODE_ENV === 'production';
8+
39
/**
410
* A replacement for instanceof which includes an error warning when multi-realm
511
* constructors are detected.
@@ -9,7 +15,7 @@ import { inspect } from './inspect.js';
915
export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
1016
/* c8 ignore next 6 */
1117
// FIXME: https://github.com/graphql/graphql-js/issues/2317
12-
globalThis.process != null && globalThis.process.env.NODE_ENV === 'production'
18+
isProduction
1319
? function instanceOf(value: unknown, constructor: Constructor): boolean {
1420
return value instanceof constructor;
1521
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: Going to production
3+
category: FAQ
4+
---
5+
6+
GraphQL.JS contains a few development checks which in production will cause slower performance and
7+
an increase in bundle-size. Every bundler goes about these changes different, in here we'll list
8+
out the most popular ones.
9+
10+
## Bundler-specific configuration
11+
12+
Here are some bundler-specific suggestions for configuring your bundler to remove `globalThis.process` and `process.env.NODE_ENV` on build time.
13+
14+
### Vite
15+
16+
```js
17+
export default defineConfig({
18+
// ...
19+
define: {
20+
'globalThis.process': JSON.stringify(true),
21+
'process.env.NODE_ENV': JSON.stringify('production'),
22+
},
23+
});
24+
```
25+
26+
### Next.js
27+
28+
```js
29+
// ...
30+
/** @type {import('next').NextConfig} */
31+
const nextConfig = {
32+
webpack(config, { webpack }) {
33+
config.plugins.push(
34+
new webpack.DefinePlugin({
35+
'globalThis.process': JSON.stringify(true),
36+
'process.env.NODE_ENV': JSON.stringify('production'),
37+
}),
38+
);
39+
return config;
40+
},
41+
};
42+
43+
module.exports = nextConfig;
44+
```
45+
46+
### create-react-app
47+
48+
With `create-react-app`, you need to use a third-party package like [`craco`](https://craco.js.org/) to modify the bundler configuration.
49+
50+
```js
51+
const webpack = require('webpack');
52+
module.exports = {
53+
webpack: {
54+
plugins: [
55+
new webpack.DefinePlugin({
56+
'globalThis.process': JSON.stringify(true),
57+
'process.env.NODE_ENV': JSON.stringify('production'),
58+
}),
59+
],
60+
},
61+
};
62+
```
63+
64+
### esbuild
65+
66+
```json
67+
{
68+
"define": {
69+
"globalThis.process": true,
70+
"process.env.NODE_ENV": "production"
71+
}
72+
}
73+
```
74+
75+
### Webpack
76+
77+
```js
78+
config.plugins.push(
79+
new webpack.DefinePlugin({
80+
'globalThis.process': JSON.stringify(true),
81+
'process.env.NODE_ENV': JSON.stringify('production'),
82+
}),
83+
);
84+
```
85+
86+
### Rollup
87+
88+
```js
89+
export default [
90+
{
91+
// ... input, output, etc.
92+
plugins: [
93+
minify({
94+
mangle: {
95+
toplevel: true,
96+
},
97+
compress: {
98+
toplevel: true,
99+
global_defs: {
100+
'@globalThis.process': JSON.stringify(true),
101+
'@process.env.NODE_ENV': JSON.stringify('production'),
102+
},
103+
},
104+
}),
105+
],
106+
},
107+
];
108+
```
109+
110+
### SWC
111+
112+
```json title=".swcrc"
113+
{
114+
"jsc": {
115+
"transform": {
116+
"optimizer": {
117+
"globals": {
118+
"vars": {
119+
"globalThis.process": true,
120+
"process.env.NODE_ENV": "production"
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}
127+
```

website/sidebars.cjs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = {
1515
label: 'Advanced',
1616
items: ['tutorials/constructing-types'],
1717
},
18+
{
19+
type: 'category',
20+
label: 'FAQ',
21+
items: ['tutorials/going-to-production'],
22+
},
1823
'tutorials/express-graphql',
1924
'tutorials/defer-stream',
2025
],

0 commit comments

Comments
 (0)