Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Visual Studio to combine all TypeScript files into one JavaScript file?

Using tsc command it's as easy as running:

tsc --out all.js js/*.ts

How can I configure Visual Studio to do that when I build my project?

like image 798
Saulo Vallory Avatar asked Jan 13 '13 10:01

Saulo Vallory


People also ask

How do I compile multiple TypeScript files into one file?

Explanation: tsc: It stands for TypeScript compiler which is used to invoke the compiler in order to compile the TypeScript files. –out: It is a CLI (Command Line Interface) command which concatenates the TypeScript files and emits the output to a single JS file. outputFile.

Is it possible to combine multiple .ts files into a single .js file?

Can we combine multiple . ts files into a single . js file? Yes, we can combine multiple files.

Can I combine TypeScript and JavaScript?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).


2 Answers

I've found a potentially easier solution by just modifying the build properties of the project (.csproj / .vbproj) you are building:

Project Build Typescript Settings

I'm unsure which version of Typescript that this feature was introduced in. But it seems a much simpler method than the svallory's solution

Source: http://rostacik.net/2013/12/18/how-to-setup-post-build-event-in-visual-studio-to-combine-multiple-typescript-files-to-one-javascript-file/

EDIT: Since Visual Studio 2015, it's now very easy to integrate Grunt/Gulp tasks and have them run in your builds. I've personally be really enjoying using Gulp for more granular control over what files I build and/or minify, and I highly recommend it. Using this guide as a starting point should help.

like image 112
Zachary Dow Avatar answered Oct 18 '22 00:10

Zachary Dow


Just got it. Add this line:

<Exec Command="tsc --out all.js @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

to the BeforeBuild target of your .csproj or .vbproj file, like this:

<Target Name="BeforeBuild">
    <Message Text="Compiling TypeScript files" />
    <Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
    <Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
    <Exec Command="tsc --out all.js @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</Target>
like image 34
Saulo Vallory Avatar answered Oct 17 '22 23:10

Saulo Vallory