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

前端 未结 3 1975
猫巷女王i
猫巷女王i 2021-02-15 17:19

I am using Grunt to build a React project and I want to have \'dev\' and \'prod\' flavours. As react docs says:

To use React in productio

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-15 18:05

    Both John Reilly's and FakeRainBrigand 's answers did not work for me. What worked for me was the following:

    Step 1 - Run this command where your package.json is

    npm i grunt-env --save-dev
    

    Step 2 - Add the code in "evn:" to your Gruntfile.js within grunt.initConfig like so:

    grunt.initConfig({
    
    ...
    
    env: {
        prod: {
            NODE_ENV: 'production'
        }
    },
    
    ...
    
    });
    

    Step 3 - Add the grunt task to your Gruntfile.js

    grunt.loadNpmTasks('grunt-env');
    

    Step 4 - Call it before browserify like so:

    grunt.registerTask("default", ["env", "browserify"]);
    

提交回复
热议问题