|
| 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 | +``` |
0 commit comments