Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify TypeScript output file name format

I need to change the TypeScript generated js file name to something else. How can I do that

For example I have MyCompany.ClassA.ts

It will generate MyCompany.ClassA.js by default

But I want MyCompany.ClassA.generated.js

I took a look at .tsConfig file but I couldn't find anything useful there.


ps. I am using VisualStudio2013 for Typescript and generating the js files

like image 627
Reza Avatar asked Feb 08 '16 19:02

Reza


People also ask

What is Tsconfig json in TypeScript?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What does TSC do in TypeScript?

Tsc stands for `TypeScript compiler` and is a simple tool included in Typescript itself, allowing you to compile any ts files into js.

How are TypeScript files compiled?

TypeScript provides a command-line utility tsc that compiles (transpiles) TypeScript files ( . ts ) into JavaScript. However, the tsc compiler (short for TypeScript compiler) needs a JSON configuration file to look for TypeScript files in the project and generate valid output files at a correct location.


2 Answers

To compile a single TypeScript file to .js with a custom name, use --outFile:

tsc MyCompany.ClassA.ts --outFile MyCompany.ClassA.generated.js

Compiling multiple files to the .generated.js pattern would require a bit more work. You could compile one at a time as in the tsc example above.

tsc A.ts --outFile A.generated.js
tsc B.ts --outFile B.generated.js

Or you could use --outDir to collect the compiled files under standard names, and then rename them with your own script.

tsc --outDir ./generated
# run script to rename ./generated/*.js to *.generated.js 

Note that --outFile and --outDir replace --out, which is deprecated:

https://github.com/Microsoft/TypeScript/wiki/Compiler-Options

like image 109
Robert Penner Avatar answered Sep 20 '22 12:09

Robert Penner


The other answer here is good and comprehensive, but just to address this:

I took a look at the .tsconfig file but I couldn't find anything useful there

You can also set the outFile config in your .tsconfig.json under compilerOptions

{
  "compilerOptions": {
    "outFile": "./some/path/MyCompany.ClassA.generated.js",
    ...
  }
}   

See this example in the documentation

like image 25
davnicwil Avatar answered Sep 20 '22 12:09

davnicwil