只编译一个.env文件到JavaScript?

时间:2017-09-29 13:46:31

标签: javascript gruntjs

我有3个不同的npm流程:

"startdev": "cross-env PROJECT_ENV=development grunt",
"startstage": "cross-env PROJECT_ENV=staging grunt",
"startprod": "cross-env PROJECT_ENV=production grunt"

Gruntfile:

    var environment = process.env.PROJECT_ENV
    ts: {
        options:{
            rootDir: "src"
        },
        dev : {
            src: ["**/*.ts", "!node_modules/**/*.ts"],
            outDir: "out",
            watch: "src",
            options:{
                module: "commonjs"
            }

我的环境变量:

src/config/.envDevelopment
src/config/.envStaging 
src/config/.envProduction

如何根据传递给交叉环境的环境,编译项目中的.env,使grunt只编译一个环境变量?

1 个答案:

答案 0 :(得分:0)

我相信你需要以某种方式解析你的环境字符串。这可能是一个解决方案:

var capitalizedEnvironment = environment.charAt(0).toUpperCase() + environment.slice(1);

写下完整的路径:

'path/to/common/folder/' + '.env' + capitalizedEnvironment