简体   繁体   English

使用browserify / envify删除process.env.NODE_ENV?

[英]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. 所以我正在通过NPM和Browserify使用ReactJS,但是我试图弄清楚如何在生产模式下构建它(如自述文件所述),但似乎没有用。 I have this code to setup the browserify: 我有以下代码来设置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. 然后可以通过UglifyJS2之类的工具自动将其删除。 Am I setting Envify up wrong? 我设置的Envify错误吗? or something. 或者其他的东西。

React already configures envify automatically. React已经自动配置envify 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, eg 它将选择构建脚本本身所运行的环境。通常,在运行实际的构建脚本之前,应先设置NODE_ENV ,例如

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 甚至更好的是,您已经将构建步骤添加到package.json"scripts"块中,因此您可以执行

npm run --production build

simply change to 只需更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM