I am trying to reduce the size of my 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.
Here is my command: 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
.
All the instances that are in my "app.js" or my other components are correctly replaced with "production".
...But in all of my required modules from the node_modules
folder (like react etc), the instances are not replaced.
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).
I created a simple example that shows the situation.
Here is my app.js file:
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.
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
This solves my problem, but I am still unsure why react-router
doesn't allow browserify and envify to work together.
I hope this helps others with a similar problem!!