我正在使用VisualStudio2015使用ASP.NET Core 6。在我的gulpfile.js脚本中,我想知道宿主环境是开发、暂存还是生产,这样我就可以添加或删除源代码映射(.map文件)并做其他事情。这个是可能的吗?
更新
关于GitHub的相关问题。
发布于 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源代码这里。
// 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; }
};有关如何设置环境变量,请参见这答案。
发布于 2015-07-05 10:54:36
您需要在每个环境中设置NODE_ENV环境变量,然后在gulpfile中使用process.env.NODE_ENV读取它。
有关更多细节,请查看https://stackoverflow.com/a/16979503/672859。
https://stackoverflow.com/questions/31228947
复制相似问题