首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定my Gulpfile.js中的Gulpfile.js核心环境

如何确定my Gulpfile.js中的Gulpfile.js核心环境
EN

Stack Overflow用户
提问于 2015-07-05 09:31:23
回答 2查看 5.4K关注 0票数 11

我正在使用VisualStudio2015使用ASP.NET Core 6。在我的gulpfile.js脚本中,我想知道宿主环境是开发、暂存还是生产,这样我就可以添加或删除源代码映射(.map文件)并做其他事情。这个是可能的吗?

更新

关于GitHub的相关问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-30 16:08:20

您可以使用ASPNETCORE_ENVIRONMENT (以前是ASPNET_ENV in RC1)环境变量来获取环境。这可以在您的吞咽文件中使用process.env.ASPNETCORE_ENVIRONMENT完成。

如果环境变量不存在,则可以退回去读取Visual用于启动应用程序的launchSettings.json文件。如果这也不存在,那么就回过头来使用Development环境。

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

代码语言:javascript
复制
// 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; }
};

有关如何设置环境变量,请参见答案。

票数 6
EN

Stack Overflow用户

发布于 2015-07-05 10:54:36

您需要在每个环境中设置NODE_ENV环境变量,然后在gulpfile中使用process.env.NODE_ENV读取它。

有关更多细节,请查看https://stackoverflow.com/a/16979503/672859

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31228947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档