ryanzec
ryanzec

Reputation: 28030

Removing process.env.NODE_ENV with browserify/envify?

So I am using ReactJS through NPM and Browserify however I am trying to figure out how to build it in production mode like the readme says but it does not seems to be working. I have this code to setup the browserify:

var browserify = require('browserify');
var envify = require('envify/custom');
var debug = false;

...

var libraries = browserify({
  debug: debug
}).transform(envify({
  _: 'purge',
  NODE_ENV: debug ? 'development' : 'production'
}));

gulpConfig.tasks.browserify.transformers.forEach(function(transform) {
  libraries.transform(transform);
});

gulpConfig.tasks.browserify.libraries.forEach(function(metaData) {
  if(metaData.path) {
    libraries.require(metaData.path, {
      expose: metaData.name
    });
  } else {
    libraries.require(metaData.name);
  }
});

var libraryStream = libraries.bundle()
.on('error', function(err){
  var message;

  if(err.description)
    message = 'browserify error: ' + err.description + ' when parsing ' + err.fileName + ' | Line ' + err.lineNumber + ', Column ' + err.column;
  else {
    message = err.message;
  }

  gutil.log(gutil.colors.red(message));

  this.emit('end');
})
.pipe(source('libraries.js'));

libraryStream.pipe(gulp.dest(gulpConfig.buildPath));

However when I looked at the compiled code, I see a bunch of this:

if ("production" !== process.env.NODE_ENV) {

I though it should compile to:

if ("production" !== "production") {

Which could then be automatically removed by tools like UglifyJS2. Am I setting Envify up wrong? or something.

Upvotes: 3

Views: 6426

Answers (2)

WaiKit Kung
WaiKit Kung

Reputation: 1296

simply change to

var libraries = browserify({
  debug: debug
}).transform(envify({
  _: 'purge',
  NODE_ENV: debug ? 'development' : 'production'
}), {
  global: true
});

Upvotes: 2

loganfsmyth
loganfsmyth

Reputation: 161447

React already configures envify automatically. It will pick up the environment that the build script itself is running in. You'd generally set NODE_ENV before running your actual build scripts, e.g.

NODE_ENV=production gulp build

Or even better, you'd have added your build step to the "scripts" block in your package.json, so then you can just do

npm run --production build

Upvotes: 6

Related Questions