如何在我的Gulpfile.js中确定ASP.NET核心环境

Muh*_*eed 11 production-environment gulp asp.net-core-mvc visual-studio-2015 asp.net-core

我使用Visual Studio 2015使用ASP.NET Core MVC 6.在我的gulpfile.js脚本中,我想知道托管环境是开发,暂存还是生产,以便我可以添加或删除源映射(.map文件)并执行其他事情.这可能吗?

UPDATE

关于GitHub的相关问题.

Muh*_*eed 6

您可以使用ASPNETCORE_ENVIRONMENT(以前ASPNET_ENV在RC1中)环境变量来获取环境.这可以在你的gulpfile中使用process.env.ASPNETCORE_ENVIRONMENT.

如果环境变量不存在,则可以回退到读取launchSettings.jsonVisual Studio用于启动应用程序的文件.如果这也不存在,那么回退到使用开发环境.

我编写了以下JavaScript对象,以便更轻松地处理gulpfile.js中的环境.你可以在这里找到完整的gulpfile.js源代码.

// Read the launchSettings.json file into the launch variable.
var launch = require('./Properties/launchSettings.json');

// Holds information about the hosting environment.
var environment = {
    // The names of the different environments.
    development: "Development",
    staging: "Staging",
    production: "Production",
    // Gets the current hosting environment the application is running under.
    current: function () { 
        return process.env.ASPNETCORE_ENVIRONMENT ||
            (launch && launch.profiles['IIS Express'].environmentVariables.ASPNETCORE_ENVIRONMENT) ||
            this.development;
    },
    // Are we running under the development environment.
    isDevelopment: function () { return this.current() === this.development; },
    // Are we running under the staging environment.
    isStaging: function () { return this.current() === this.staging; },
    // Are we running under the production environment.
    isProduction: function () { return this.current() === this.production; }
};
Run Code Online (Sandbox Code Playgroud)

有关如何设置环境变量的信息,请参阅答案.