javascript - React and Grunt - Envify NODE_ENV='production' and UglifyJS -


i using grunt build react project , want have 'dev' , 'prod' flavours. react docs says:

to use react in production mode, set environment variable node_env production. minifier performs dead-code elimination such uglifyjs recommended remove code present in development mode.

i new using grunt, browserify , stuff let's see. first problem have envify, use transform:

browserify: {   options: {     transform: ['reactify'],     extensions: ['.jsx']   },   dev:{     options: {       watch: true //uses watchify (faster)     },     src: ['js/app.js'],     dest: 'js/bundle.js'   },   /**    * use react in production mode, set environment variable node_env production.    * minifier performs dead-code elimination such uglifyjs    * recommended remove code present in development mode.    **/   prod: {     options: {      transform: ['envify'] //how set nod_env='production' ?     },     src: ['js/app.js'],     dest: 'js/bundle.js'   } }, 

ok, doing grunt:dev works fine. when running grunt:prod... how can set node_env: 'production'? mean, know passing 'envify' transform but... no idea how use that.

after this, have uglify task:

uglify: {  prod: {    files: {      'js/bundle.min.js': ['js/bundle.js']    }  } } 

so after calling grunt:prod, creates 2 files (bundle.js , bundle-min.js). in production have bundle.min.js. know can do:

js/bundle.js': ['js/bundle.js']

but mmm don't know if there way rename bundle.min.js, guess so... problem in html have:

<script src="js/bundle.js"></script> 

is there here trick make accepts either bundle.js or bundle.min.js?

thanks in advance.

transforms local, , made packages put transforms in package.json file. unless you're using envify in your own code, don't need it.

what need grunt-env, or way set environmental variables.

here's alternative using package.json:

{   "scripts": {     "build": "node_env=development grunt build-dev",     "dist": "node_env=production grunt dist"   } }, "devdependencies": {   "grunt": "...",   "grunt-cli": "..." } 

the benefit here person using package doesn't need install grunt globally. npm run build run ./node_modules/.bin/grunt build-dev correct environmental variable set.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -