Commit f5d81fd0 authored by Robert Knight's avatar Robert Knight

Enable conditional inclusion of code based on NODE_ENV checks

Packages in the React ecosystem often include debugging checks guarded
by:

```
if (process.env.NODE_ENV === 'development') { ... }
```

OR

```
if (process.env.NODE_ENV !== 'production') { ... }
```

This commit uses loose-envify to replace the `process.env.$VAR`
expression with a literal string value when building JS bundles. This enables
debugging checks in dev builds and causes the minifier to remove the
code entirely in production builds.

Some packages don't need this because their package.json file includes a
"browserify" key which already configures this transform, however not
all of them do. Therefore the transform is configured to run globally.
parent d0ed54a5
......@@ -77,6 +77,7 @@
"katex": "^0.10.0",
"lodash.debounce": "^4.0.3",
"lodash.get": "^4.3.0",
"loose-envify": "^1.4.0",
"mkdirp": "^0.5.1",
"mocha": "^6.0.0",
"ng-tags-input": "^3.1.1",
......
......@@ -10,6 +10,7 @@ const babelify = require('babelify');
const browserify = require('browserify');
const coffeeify = require('coffeeify');
const exorcist = require('exorcist');
const envify = require('loose-envify/custom');
const gulpUtil = require('gulp-util');
const mkdirp = require('mkdirp');
const through = require('through2');
......@@ -225,6 +226,16 @@ module.exports = function createBundle(config, buildOpts) {
bundle.transform({global: true}, uglifyify);
}
// Include or disable debugging checks in our code and dependencies by
// replacing references to `process.env.NODE_ENV`.
bundle.transform(envify({
NODE_ENV: process.env.NODE_ENV || 'development',
}), {
// Ideally packages should configure this transform in their package.json
// file if they need it, but not all of them do.
global: true,
});
function build() {
const output = fs.createWriteStream(bundlePath);
const b = bundle.bundle();
......
......@@ -3,6 +3,7 @@
/* global __dirname */
const path = require('path');
const envify = require('loose-envify/custom');
module.exports = function(config) {
config.set({
......@@ -94,6 +95,8 @@ module.exports = function(config) {
],
},
],
// Enable debugging checks in libraries that use `NODE_ENV` guards.
[envify({ NODE_ENV: 'development' }), { global: true }],
],
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment