我正在使用Typescript
,因此Release Mode
和Debug Mode
有很多区别。在Debug Mode
中,我使用的是http://localhost:port/
基本{{1} }。 但是,在URL
中,我必须使用Release Mode
这使我反复更改https://www.example.com/
和URL
中的Release Mode
个。 确实,我还必须在Debug Mode
和Release Mode
中手动更改其他参数,这些参数可能会导致错误配置错误。
我在寻找什么
避免在Debug Mode
和Release Mode
中手动配置
线索和假设
也许有一种在Debug Mode
中使用Macros
的方式,就像我们在Typescript
或MsBuild
中使用的方式,例如WebConfig
和$(ConfigurationName)
我正在寻找的最佳解决方案
最简单的解决方案,无需学习Extra,也无需更改项目体系结构。如果,我必须使用$(ProjectDir)
,请在其中加上完整的详细信息。
项目框架
webpack
或Asp.net .Net Framework
最少的可复制代码
仅考虑您要更改此URL
Asp.Net Core
收件人
const URL = http://localhost:port/
在const URL = https://www.example.com/
可能的硬性解决方案
@ Kaca992 建议webpack可以通过使用
解决问题 Release Mode
和mode: 'development'
,例如:
mode: 'production'
production and development mode extra information
但是上述解决方案具有以下缺点:
module.exports = {
mode: 'development'
};
答案 0 :(得分:2)
您将必须使用环境变量并根据该变量进行检查。您可以创建如下的帮助方法:
export function isProduction() {
return process && process.env && process.env.NODE_ENV === 'production';
}
请小心,大多数捆绑程序都不能删除此代码(需要进行内联检查以确保在编译时将其删除)。如果您使用的是Webpack之类的东西,则可以使用https://webpack.js.org/plugins/define-plugin/轻松包含NODE_ENV变量。
这也是该主题的出色读物:https://overreacted.io/how-does-the-development-mode-work/
如果您有许多不同的设置,那么解决此问题的一种好方法是将所有不同的值分组到一个帮助程序模块中:
debug.config.ts release.config.ts
,然后在代码中使用config.ts,只是根据您的配置重新导出:
import * as debug from debug.config.ts;
import * as release from release.config.ts;
const config = isProduction() ? release : debug;
export default config;
答案 1 :(得分:0)
我发现本机打字稿功能可以轻松配置Release Mode
和Debug Mode
参数,而无需使用webpack
和其他Extensions
。 >
我制作了config.ts
文件,并将其放在所有其他.js
文件的顶部,通过使用window.location.host
,我得到了.js
文件已经存在的域名使用。
/* put this configuration file at top of the other js files in order to make
sure the parameters will be initiated before use */
let basic_URL: string;
let velocity: number;
let delay: number;
let token: string;
(function () {
'use strict';
// Release Mode
if (window.location.host == "www.example.com") {
basic_URL = "https://www.example.com/";
velocity = 50;
delay = 100;
token = "Release Mode Token"
/*Assign Other Release Mode Parameters*/
}
// Debug Mode
else {
basic_URL = "http://localhost:1234/";
velocity = 500;
delay = 1000;
token = "Debug Mode Token"
/*Assign Other Debug Mode Parameters*/
}
})();
注意::此解决方案并不意味着我不会寻求其他解决方案。