Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript compile and keep comments

Tags:

typescript

tsc

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. )

like image 310
joeriks Avatar asked Oct 06 '12 09:10

joeriks


1 Answers

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.

like image 181
9 revs, 2 users 99% Avatar answered Oct 08 '22 14:10

9 revs, 2 users 99%