Redux NODE_ENV errors with Gulp/Browserify

2019-06-20 06:15发布

I'm getting this error message on a React/Redux app that is minified and packaged with Browserify and Gulp and deployed to Heroku.

bundle.js:39 You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux.

But it seems the build step is being done in NODE_ENV = 'production'.

I've a task that set the env variables like so

gulp.task('apply-prod-environment', function() {
  return process.env.NODE_ENV = 'production';
});

And the logs on Heroku show the ENV is production:

enter image description here

To guarantee the apply-prod-environment runs before the other tasks, I'm using RunSequence Gulp plugin.

gulp.task('buildProd', cb => {
  runSequence(
    'apply-prod-environment',
    'task-1',
    'task-2',
    'etc',
    cb
  );
});

EDIT
Second Try..

import envify from 'envify/custom';

function buildJS(sourceFile, {setEnv}) {
  return browserify(sourceFile)
    .transform(babelify, {
      presets: ['es2015', 'react', 'stage-2']
    })
    .transform(envify({
      NODE_ENV: setEnv
    }))
    .bundle()
    .on('error', (e) =>  {
      gutil.log(e);
    });
}

Still Getting same Error

Third Try..

function buildJS(sourceFile, {setEnv}) {
  return browserify(sourceFile)
    .transform(babelify, {
      presets: ['es2015', 'react', 'stage-2']
    })
    .transform(
      {global: true},
      envify({
        NODE_ENV: setEnv
      })
    )
    .bundle()
    .on('error', (e) =>  {
      gutil.log(e);
    });
}

Still Getting same Error

1条回答
欢心
2楼-- · 2019-06-20 06:42

I struggled with this same problem and I ended up using loose-envify to mock the environment variables that I wanted to override.

Then, my gulp task looked like this:

gulp.task('javascript:prod', function() {
return browserify("app/main.js", { debug: !IS_PROD })
    .transform("babelify", { presets: [ "es2015", "react" ], plugins: [ "transform-object-rest-spread", "transform-function-bind", "transform-object-assign" ] })
    .transform('loose-envify', { NODE_ENV: 'production' })
    .bundle()
    .pipe(source('app.js'))
    .pipe(buffer())
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest("./public/javascripts/"))
    .pipe(rev.manifest({merge:true, base: 'build/assets'}))
    .pipe(gulp.dest('build/assets'));
});
查看更多
登录 后发表回答