typescript 预处理器在打字稿中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32906097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:06:22  来源:igfitidea点击:

Preprocessor Defines in Typescript

typescript

提问by Greg Gum

In c#, you can do #if DEBUG... to do something special when debugging. I would like to do a similar thing in Typescript so that I can set form values when testing.

在 c# 中,您可以执行 #if DEBUG... 来在调试时做一些特别的事情。我想在 Typescript 中做类似的事情,以便我可以在测试时设置表单值。

Anyone have any suggestions on how to implement this in Typescript?

有人对如何在 Typescript 中实现这一点有任何建议吗?

The best I have come up with is a settings class:

我想出的最好的是一个设置类:

export class Settings {
    static isDebugging = false;
};

Then when starting the app, I set it to true.

然后在启动应用程序时,我将其设置为 true。

Then in code, I import the class and check to see if it is true or not.

然后在代码中,我导入类并检查它是否正确。

if (Settings.isDebugging)...

Seems to work ok.

似乎工作正常。

采纳答案by David Sherret

If you want to achieve something similar to #ifdirectives from C#, then you will have to make something that strips certain code when your application is built.

如果你想实现类似于#ifC# 指令的东西,那么你将不得不在构建应用程序时制作一些剥离某些代码的东西。

To do that, there are a few build plugins you can use. There are some build plugins (see hereand here) that will strip code that is within comments like this at build time:

为此,您可以使用一些构建插件。有一些构建插件(参见此处此处)将在构建时删除这样的注释中的代码:

/* test-code */
removeMeInProduction();
/* end-test-code */

However, by using these plugins you could make a mistake in your spelling of test-codeor end-test-codecausing you to leave test code in production. You won't get a compile error about it.

但是,通过使用这些插件,您可能会在拼写错误test-codeend-test-code导致您将测试代码留在生产环境中。你不会得到关于它的编译错误。

Because of this, it's probably better to find a build plugin that strips a function based on its name (see here—unfortunately I can't find a gulp plugin for this). That way you could write something like this:

正因为如此,最好找到一个基于函数名称剥离函数的构建插件(见这里 -不幸的是我找不到一个 gulp 插件)。这样你就可以写出这样的东西:

function ifNotProduction(func: () => void) {
    func();
}

Then use it:

然后使用它:

ifNotProduction(() => {
    console.log("Only run when test.");
});

And then tell the build script to strip out the use of that function when it's not a production build.

然后告诉构建脚本在它不是生产构建时去掉该函数的使用。

Overall though, your simple solution of just checking a boolean is good enough.

总的来说,您只检查布尔值的简单解决方案就足够了。

回答by Remo H. Jansen

Flags like #if DEBUGare not available in TypeScript because the compilation output is always the same. In C# when you compile to debug mode the DLLfiles include some extra information for debugging. In release mode that information is omitted and the DLLfiles are lighter.

像这样#if DEBUG的标志在 TypeScript 中不可用,因为编译输出总是相同的。在 C# 中,当您编译为调试模式时,DLL文件会包含一些用于调试的额外信息。在发布模式下,信息被省略并且DLL文件更轻。

If you are using an automated build environment like Gulpor Gruntyou can store your settings under a folder like /config/debug/config.tsand /config/release/config.tsthen you can create two tasks:

如果你使用像GulpGrunt这样的自动化构建环境,你可以将你的设置存储在一个像这样的文件夹下/config/debug/config.ts/config/release/config.ts然后你可以创建两个任务:

One for debug:

一种用于调试:

$ gulp bundle-debug

And one for release

和一个释放

$ gulp bundle-release

To copy one file or another.

复制一个或另一个文件。

回答by basarat

There is code that adds support for preprocessor directive into TypeScript : https://github.com/Microsoft/TypeScript/issues/4691

有代码将预处理器指令的支持添加到 TypeScript 中:https: //github.com/Microsoft/TypeScript/issues/4691

However it is under discussion if it will become a part of TypeScript proper. Till then the workaround are mostly to use external build tools, or different configuration options (which strip code at compile time) and magic variables (which just disable code paths at runtime)

然而,它是否会成为 TypeScript 的一部分正在讨论中。到那时,解决方法主要是使用外部构建工具,或不同的配置选项(在编译时剥离代码)和魔术变量(在运行时仅禁用代码路径)

回答by eskimwier

I always thought it was neat to use the C Preprocessor for the #ifdefconditional in JavaScript programs. Haven't tried it with TypeScript yet though. Here's a nice page about it: http://www.nongnu.org/espresso/js-cpp.html.

我一直认为#ifdef在 JavaScript 程序中使用 C 预处理器来处理条件是很不错的。不过还没有用 TypeScript 尝试过。这是一个很好的页面:http: //www.nongnu.org/espresso/js-cpp.html

The command shared in the link is:

链接中共享的命令是:

# configure your web server to pipe Javascript through GNU cpp:
/usr/bin/cpp -P -undef -Wundef -std=c99 -nostdinc -Wtrigraphs -fdollars-in-identifiers -C

回答by BluE

I found this project, which adds this feature: https://github.com/domchen/typescript-plus

我找到了这个项目,它增加了这个功能:https: //github.com/domchen/typescript-plus

This adds these features:

这增加了以下功能:

The typescript-plus compiler provides extra features to the original typescript compiler, such as accessors optimization, class reflection, conditional compilationand the most useful one: automatically reordering the source files by analyzing their dependencies in code. This compiler is integrated into the Egret Engine and has been heavily used by it.

This project will try to stay up to date with the new release of the original TypeScript project.

Current TypeScript Version: 3.1.3

typescript-plus 编译器为原始的 typescript 编译器提供了额外的功能,例如访问器优化、类反射、 条件编译和最有用的功能:通过分析源文件在代码中的依赖关系自动重新排序源文件。该编译器已集成到 Egret Engine 中,并已被大量使用。

该项目将尝试与原始 TypeScript 项目的新版本保持同步。

当前打字稿版本:3.1.3