简体   繁体   English

启用CLI而不删除process.env.NODE_ENV

[英]Envify CLI not removing process.env.NODE_ENV

I am trying to reduce the size of my React bundle.js. 我正在尝试减小React bundle.js的大小。

I am attempting to use Envify with Browserify to replace process.env.NODE_ENV with the string "production", in order to use uglifyjs and remove all the extra development code. 我试图将Envify与Browserify一起使用,以字符串“ production”替换process.env.NODE_ENV ,以便使用uglifyjs并删除所有额外的开发代码。

Here is my command: browserify -t [envify --NODE_ENV production] assets/js/app.js -o assets/js/bundle.js 这是我的命令: browserify -t [envify --NODE_ENV production] assets/js/app.js -o assets/js/bundle.js

So my bundle.js is successfully created, but it still has some of the instances of process.env.NODE_ENV . 因此,我的bundle.js已成功创建,但是它仍然具有process.env.NODE_ENV一些实例。

All the instances that are in my "app.js" or my other components are correctly replaced with "production". 我的“ app.js”或其他组件中的所有实例均已正确替换为“生产”。

...But in all of my required modules from the node_modules folder (like react etc), the instances are not replaced. ...但是在node_modules文件夹中所有必需的模块中(例如react等),实例不会被替换。

Any help greatly appreciated! 任何帮助,不胜感激! THX! 谢谢!

******************* Edit ********************** *******************编辑**********************

JMM's solution successfully answered the original question, but I still had an issue because I am using React-Router (I think). JMM的解决方案成功地回答了最初的问题,但是仍然存在一个问题,因为我正在使用React-Router (我认为)。

I created a simple example that shows the situation. 我创建了一个显示情况的简单示例。

Here is my app.js file: 这是我的app.js文件:

var React = require('react');           
var ReactDOM = require('react-dom');
var Router = require('react-router').Router; 
var Route = require('react-router').Route;

var Example = React.createClass({
    render: function(){
        console.log(process.env.NODE_ENV);
        if (process.env.NODE_ENV === "development") { 
          console.log('Development Version');
        } else {
            console.log('Production Version');
        }
        return <span>Hello World!</span>;
    }
});

var AppRoutes = ( <Route name="/" path="/" component={Example} /> );


ReactDOM.render(
    (<Router>
        {AppRoutes}
    </Router>), 
    document.getElementById('ExampleApp')
    );

If I run NODE_ENV=production browserify -t envify assets/js/app.js -o assets/js/bundle.js , I still have some instances of process.env.NODE_ENV in the bundle.js. 如果我运行NODE_ENV=production browserify -t envify assets/js/app.js -o assets/js/bundle.js ,则NODE_ENV=production browserify -t envify assets/js/app.js -o assets/js/bundle.js仍然有一些process.env.NODE_ENV实例。

I found a work-around by simply creating the bundle.js with: browserify assets/js/app.js -o assets/js/bundle.js , and then running envify on the bundle with: NODE_ENV=production envify assets/js/bundle.js > assets/js/bundle2.js 我找到了一种解决方法,只需使用以下方法创建bundle.jsbundle.js browserify assets/js/app.js -o assets/js/bundle.js ,然后在bundle上运行envify: NODE_ENV=production envify assets/js/bundle.js > assets/js/bundle2.js

This solves my problem, but I am still unsure why react-router doesn't allow browserify and envify to work together. 这解决了我的问题,但是我仍然不确定为什么react-router不允许浏览器和env一起工作。

I hope this helps others with a similar problem!! 我希望这可以帮助其他有类似问题的人!

Browserify doesn't run transforms on stuff in node_modules . Browserify不会对node_modules中的内容进行node_modules However, React has envify (actually loose-envify now) configured in its package.json . 但是,React已经在package.json配置了envify(实际上现在是envenvenify)。 I think the reason it's not working for you is passing the environment to envify as an option (again, your invocation of envify there is not running on React). 我认为它对您不起作用的原因是将环境作为选项传递给环境(同样,您对环境的调用并未在React上运行)。 The envify docs are not good about explaining how this works. envify文档不能很好地解释其工作原理。 To get the production build of React you should do: 要获得React的生产版本,您应该执行以下操作:

NODE_ENV=production browserify -t envify assets/js/app.js -o assets/js/bundle.js

I believe this should cause envify to function as expected on your app code and React. 我相信这会导致envify在您的应用程序代码和React上按预期运行。

Unfortunately JMM's answer doesn't work, because setting process.env.NODE_ENV has no effect in Browserify. 遗憾的是,JMM的答案无效,因为设置process.env.NODE_ENV在Browserify中无效。 The resulting bundle still has process.env.NODE_ENV references in it and hence 生成的包中仍然具有process.env.NODE_ENV引用,因此

  • Browserify will not require() the React production version modules, Browserify require() React生产版本模块,
  • the minifier will not be able to remove dead code, and 缩小器将无法删除无效代码,并且
  • the application will still be running in debug mode. 该应用程序仍将在调试模式下运行。

This is unfortunately not the only place where this approach is offered as the correct answer :-( 不幸的是,这不是唯一提供此方法作为正确答案的地方:-(


The correct approach can be found in eg 正确的方法可以在例如

You need to switch the envify transform to be a global one, eg 您需要将envify转换切换为全局转换,例如

# note the "-g" instead of the usual "-t"
$ browserify ... -g [ envify --NODE_ENV production ] ....

or in gulpfile.js 或在gulpfile.js

browserify(...)
    ...
    .transform('envify', {
        global:   true, // also apply to node_modules
        NODE_ENV: debug ? 'development' : 'production',
    })
    ...
    .bundle()
    ...
    .pipe(gulpif(!debug, babelMinify())) // my example uses gulp-babel-minify
    ...

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

相关问题 使用browserify / envify删除process.env.NODE_ENV? - Removing process.env.NODE_ENV with browserify/envify? 在heroku开发之后,process.env.NODE_ENV被开发 - process.env.NODE_ENV is development in after heroku development process.env.NODE_ENV === &quot;开发&quot; 即使在生产中 - process.env.NODE_ENV === "developement" even in production 反应 | webpack | 在 Azure 上获取 process.env.NODE_ENV== undefined - React | webpack | getting process.env.NODE_ENV== undefined on Azure process.env.NODE_ENV 在一个项目中未定义,但在另一个项目中未定义 - process.env.NODE_ENV is undefined in one project, but not another Webpack:-p vs NODE_ENV =生产vs process.env.NODE_ENV - Webpack: -p vs NODE_ENV=production vs process.env.NODE_ENV 在运行时可在create-react-app中将“ process.env.NODE_ENV”替换为字符串或过程对象 - 'process.env.NODE_ENV' replaced as a string or process object is available at runtime in create-react-app 通过envify设置NODE_ENV不起作用 - Setting NODE_ENV via envify not working process.env.NODE_ENV 在 webpack 4 中返回 undefined,服务器端 - process.env.NODE_ENV returns undefined in webpack 4, server-side 在 Google Cloud Run 使用 Dockerfile 时,ReactJS process.env.NODE_ENV 未设置为生产环境 - ReactJS process.env.NODE_ENV is not set to production when using Dockerfile at Google Cloud Run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM