打字稿发布模式和调试模式配置

时间:2020-01-25 10:39:42

标签: asp.net typescript webpack

我正在使用Typescript,因此Release ModeDebug Mode有很多区别。在Debug Mode中,我使用的是http://localhost:port/基本{{1} }。 但是,在URL中,我必须使用Release Mode

主要问题

这使我反复更改https://www.example.com/URL中的Release Mode个。 确实,我还必须在Debug ModeRelease Mode中手动更改其他参数,这些参数可能会导致错误配置错误。

我在寻找什么

避免在Debug ModeRelease Mode中手动配置

线索和假设

也许有一种在Debug Mode中使用Macros的方式,就像我们在TypescriptMsBuild中使用的方式,例如WebConfig$(ConfigurationName)

我正在寻找的最佳解决方案

最简单的解决方案,无需学习Extra,也无需更改项目体系结构。如果,我必须使用$(ProjectDir),请在其中加上完整的详细信息。

项目框架

webpackAsp.net .Net Framework

最少的可复制代码

仅考虑您要更改此URL

Asp.Net Core

收件人

const URL = http://localhost:port/

const URL = https://www.example.com/

可能的硬性解决方案

@ Kaca992 建议webpack可以通过使用

解决问题

Release Modemode: 'development',例如:

mode: 'production'

production and development mode extra information

但是上述解决方案具有以下缺点:

  • 花时间学习module.exports = { mode: 'development' };
  • 需要熟悉npm
  • 的基本概念
  • 需要熟悉捆绑和缩小的概念
  • 需要使用模块(导入/导出)

2 个答案:

答案 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 ModeDebug 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*/
    }
})();

注意::此解决方案并不意味着我不会寻求其他解决方案。