问题
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. )
回答1:
Yes, the -c (or --comments) option;
Syntax: tsc [options] [file ..]
Examples: tsc hello.ts
tsc --out foo.js foo.ts
tsc @args.txtOptions:
-c, --comments Emit comments to output
...
回答2:
Comments that start with /*!
are preserved.
example:/*! this comment remains untouched */
/* but this one will be removed */
回答3:
Since 2015 you can create a tsconfig.json
in your project and add "removeComments": false
to its "compilerOptions"
property in order to keep your comments in the resulting javascript files.
1. Create the tsconfig.json
file for your project from your terminal (if necessary)
tsc -init
2. Add "removeComments": false
to your tsconfig.json
file inside the "compilerOptions"
property
At the end, you should expect your tsconfig.json
file content to be like this:
{
"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
- Use
tsc myFile.ts
in order to keep your comments - Use
tsc --removeComments myFile.ts
in order to remove your comments
You can learn more about tsconfig.json
compiler options on Typescriptlang.org tsconfig.json page.
Furthermore, according to the Typescript documentation, setting true
or false
to the "removeComments"
property will have no effect on copy-right header comments beginning with /*!
. Thus, they will always appear in your .js
files.
回答4:
You will have to edit the underlying .csproj file and include the -c option.
Have a look here:
http://blorkfish.wordpress.com/2012/10/06/including-typescript-comments-in-generated-javascript/
回答5:
Currently using 1.6.2 and it appears comments are preserved by default. The only comment-related flag in the compiler is to remove comments. As per the docs:
--removeComments
Remove all comments except copy-right header comments beginning with /!*
回答6:
Chutzpah 2.2 now 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.
来源:https://stackoverflow.com/questions/12758657/typescript-compile-and-keep-comments