使用grunt-env设置process.env变量会导致未定义

时间:2016-01-11 20:52:30

标签: javascript gruntjs

我有grunt-env任务:

env: {
    dev: {
        NODE_ENV: 'dev',
        DEST: '<%= config.dev %>'
    },
    dist: {
        NODE_ENV: 'dist',
        DEST: '<%= config.dist %>'
    }
},

除了测试process.env.DEST设置正确的简单任务外。

grunt.registerTask('testEnv', function(){
  console.log(process.env.DEST);
});

当我运行它时,我得到dev的预期输出。

Running "env:dev" (env) task

Running "testEnv" task
dev

Done, without errors.

现在,如果我在另一个grunt任务中使用process.env.DEST,我的值为undefined

Running tasks: env:dev, testEnv, concat:preload

Running "env:dev" (env) task
Verifying property env.dev exists in config...OK
File: [no files]
Options: (none)

Running "testEnv" task
dev

Running "concat:preload" (concat) task
Verifying property concat.preload exists in config...OK
Files: app/scripts/preload/modernizr.js -> undefined/scripts/preload.js
Options: separator="\n", banner="", footer="", stripBanners=false, process=false, sourceMap=false, sourceMapName=undefined, sourceMapStyle="embed"
Reading app/scripts/preload/modernizr.js...OK
Writing undefined/scripts/preload.js...OK
File undefined/scripts/preload.js created.

Done, without errors.

您可以在此行Files: app/scripts/preload/modernizr.js -> undefined/scripts/preload.js中看到未定义的内容。这是一个简单的concat任务,我希望输出根据我设置的环境而有所不同。

concat: {
    baseDir: '<%= config.app %>/scripts',
    preload: {
        src: ['<%= concat.baseDir %>/preload/*.js'],
        dest: process.env.DEST + '/scripts/preload.js'
    },
    libs: {
        src: ['<%= concat.baseDir %>/libs/*.js'],
        dest: process.env.DEST + '/scripts/vendors.js'
    }
},

我现在已经在这一天了,并且不知道我做错了什么。任何帮助将不胜感激。

0 个答案:

没有答案