Skip to content

Commit 950e883

Browse files
committed
Conditionally drop console messages from production build
Add a environment variable, `DROP_CONSOLE`, to specify if console function calls should be dropped in the production build.
1 parent de8b2b3 commit 950e883

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

docusaurus/docs/advanced-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You can adjust various development and production settings by setting environmen
1919
| WDS_SOCKET_PORT | ✅ Used | 🚫 Ignored | When set, Create React App will run the development server with a custom websocket port for hot module reloading. Normally, `webpack-dev-server` defaults to `window.location.port` for the SockJS port. You may use this variable to start local development on more than one Create React App project at a time. See [webpack-dev-server documentation](https://webpack.js.org/configuration/dev-server/#devserversockport) for more details. |
2020
| PUBLIC_URL | ✅ Used | ✅ Used | Create React App assumes your application is hosted at the serving web server's root or a subpath as specified in [`package.json` (`homepage`)](deployment#building-for-relative-paths). Normally, Create React App ignores the hostname. You may use this variable to force assets to be referenced verbatim to the url you provide (hostname included). This may be particularly useful when using a CDN to host your application. |
2121
| CI | ✅ Used | ✅ Used | When set to `true`, Create React App treats warnings as failures in the build. It also makes the test runner non-watching. Most CIs set this flag by default. |
22+
| DROP_CONSOLE | 🚫 Ignored | ✅ Used | When set to `true`, `console.*` functions during the production build will be dropped. When set to `false`, the default, these function calls will be included as usual. |
2223
| REACT_EDITOR | ✅ Used | 🚫 Ignored | When an app crashes in development, you will see an error overlay with clickable stack trace. When you click on it, Create React App will try to determine the editor you are using based on currently running processes, and open the relevant source file. You can [send a pull request to detect your editor of choice](https://github.com/facebook/create-react-app/issues/2636). Setting this environment variable overrides the automatic detection. If you do it, make sure your systems [PATH](<https://en.wikipedia.org/wiki/PATH_(variable)>) environment variable points to your editor’s bin folder. You can also set it to `none` to disable it completely. |
2324
| CHOKIDAR_USEPOLLING | ✅ Used | 🚫 Ignored | When set to `true`, the watcher runs in polling mode, as necessary inside a VM. Use this option if `npm start` isn't detecting changes. |
2425
| GENERATE_SOURCEMAP | 🚫 Ignored | ✅ Used | When set to `false`, source maps are not generated for a production build. This solves out of memory (OOM) issues on some smaller machines. |

packages/react-scripts/config/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const imageInlineSizeLimit = parseInt(
5959
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
6060
);
6161

62+
const shouldDropConsole = process.env.DROP_CONSOLE === 'true';
63+
6264
// Check if TypeScript is setup
6365
const useTypeScript = fs.existsSync(paths.appTsConfig);
6466

@@ -251,6 +253,7 @@ module.exports = function (webpackEnv) {
251253
ecma: 8,
252254
},
253255
compress: {
256+
drop_console: shouldDropConsole,
254257
ecma: 5,
255258
warnings: false,
256259
// Disabled because of an issue with Uglify breaking seemingly valid code:

0 commit comments

Comments
 (0)