TypeScript 编译并保留注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12758657/
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-09-13 16:12:33  来源:igfitidea点击:

TypeScript compile and keep comments

typescripttsc

提问by joeriks

I like to have my comments intact in the resulting javascript file, by default the compiler removes them. Is there a tsc parameter for that? (The use case is to keep /// reference path's = ... for chutzpah unit testing. )

我喜欢在生成的 javascript 文件中保留我的注释,默认情况下编译器会删除它们。是否有 tsc 参数?(用例是保持 /// 引用路径的 = ... 用于 chutzpah 单元测试。)

采纳答案by Joachim Isaksson

Yes, the -c (or --comments) option;

是的,-c(或 --comments)选项;

Syntax: tsc [options] [file ..]

Examples: tsc hello.ts
tsc --out foo.js foo.ts
tsc @args.txt

Options:
   -c, --comments        Emit comments to output
...

语法:tsc [选项] [文件 ..]

示例: tsc hello.ts
tsc --out foo.js foo.ts
tsc @args.txt

选项:
   -c、--comments 将注释输出到输出
...

回答by amin

Comments that start with /*!are preserved.

以 开头的评论将/*!被保留。

example:
/*! this comment remains untouched */
/* but this one will be removed */

例子:
/*! this comment remains untouched */
/* but this one will be removed */

回答by Imanou Petit

Since 2015 you can create a tsconfig.jsonin your project and add "removeComments": falseto its "compilerOptions"property in order to keep your comments in the resulting javascript files.

自 2015 年以来,您可以tsconfig.json在项目中创建一个并添加"removeComments": false到其"compilerOptions"属性中,以便将您的评论保留在生成的 javascript 文件中。



1. Create the tsconfig.jsonfile for your project from your terminal (if necessary)

1.tsconfig.json从您的终端为您的项目创建文件(如有必要)

tsc -init

2. Add "removeComments": falseto your tsconfig.jsonfile inside the "compilerOptions"property

2. 添加"removeComments": false到您的tsconfig.json文件中的"compilerOptions"属性

At the end, you should expect your tsconfig.jsonfile content to be like this:

最后,你应该期望你的tsconfig.json文件内容是这样的:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "removeComments": false
    },
    "exclude": [
        "node_modules"
    ]
}

3. Compile your .ts file into a .js file from your terminal

3. 从终端将 .ts 文件编译为 .js 文件

  • Use tsc myFile.tsin order to keep your comments
  • Use tsc --removeComments myFile.tsin order to remove your comments
  • 使用tsc myFile.ts以保留您的评论
  • 使用tsc --removeComments myFile.ts,以消除您的意见


You can learn more about tsconfig.jsoncompiler options on Typescriptlang.org tsconfig.json page.

您可以tsconfig.jsonTypescriptlang.org tsconfig.json 页面上了解有关编译器选项的更多信息。

Furthermore, according to the Typescript documentation, setting trueor falseto the "removeComments"property will have no effect on copy-right header comments beginning with /*!. Thus, they will always appear in your .jsfiles.

此外,根据打字稿文档,设置truefalse"removeComments"财产将有上开始复制权头注释没有影响/*!。因此,它们将始终出现在您的.js文件中。

回答by blorkfish

You will have to edit the underlying .csproj file and include the -c option.
Have a look here:

您必须编辑基础 .csproj 文件并包含 -c 选项。
看看这里:

http://blorkfish.wordpress.com/2012/10/06/including-typescript-comments-in-generated-javascript/

http://blorkfish.wordpress.com/2012/10/06/include-typescript-comments-in-generated-javascript/

回答by Jonathan Guerrera

Currently using 1.6.2 and it appears comments are preserved by default. The only comment-related flag in the compiler is to removecomments. As per the docs:

当前使用 1.6.2 并且似乎默认情况下保留注释。编译器中唯一与注释相关的标志是删除注释。根据文档:

--removeComments
Remove all comments except copy-right header comments beginning with /!*

--removeComments
删除除以 /!* 开头的版权标题注释之外的所有注释

回答by Matthew Manela

Chutzpah 2.2now supports TypeScript natively so you don't need to worry about this. You can run Chutzpah directly on the .ts file and it will run your tests.

Chutzpah 2.2现在原生支持 TypeScript,因此您无需担心这一点。您可以直接在 .ts 文件上运行 Chutzpah,它会运行您的测试。