55

I have a test project on TypeScript, code can found here.

When I'm creating new project with VS2012, an app.ts file is created. When I'm changing it's content as shown by the link and adding new module called GameModule, I'm getting compile error. When I'm deleting app.ts and creating Main.ts instead, everything compiling fine, but there is a problem - only Main.ts is compiled to Main.js, and GameModule.ts stays uncompiled.

How can I make compiler to merge all the code in one JS?

patridge
  • 26,385
  • 18
  • 89
  • 135
skayred
  • 10,603
  • 10
  • 52
  • 94
  • I'm not putting this as an answer, but just as an alternative: if you are having problems with auto-compiling, either single files or multiple files within a directory, then http://onthefly.codeplex.com/ can help. – Arrow Oct 26 '12 at 10:18
  • Shane Anderson also has a nice solution described in his [Blog](http://blog.anderson.geek.nz/2013/02/26/sorting-a-scriptbundle-based-on-reference-tags/). He uses [BundleTransformer](http://bundletransformer.codeplex.com/) to extend the ASP.NET ScriptBundles. – Kuepper Mar 10 '15 at 17:04

6 Answers6

46

You have to use command line arguments of compiler

--outFile FILE Concatenate and emit output to single file

example

 tsc --outFile modules.js main.ts app.ts
Richard Simões
  • 12,401
  • 6
  • 41
  • 50
Menotaurus
  • 615
  • 5
  • 8
  • 1
    This somehow no longer works for me. First, I was prompted to specify --module, then it compiles into multiple files instead of a single file. (i.e. main.js and app.js instead of modules.js) – louis.luo Mar 30 '15 at 21:44
  • 6
    In case someone is also wondering why it doesn't work, it may because all your files are modules. TSC doesn't concat modules. See here: http://typescript.codeplex.com/workitem/1745 – louis.luo Mar 30 '15 at 22:38
  • Same question here. Changing the --out parameter has no effect. All ts files get compiled into their own js files. – Kokodoko Nov 23 '15 at 15:28
  • 2
    `--out` is deprecated in favor of `--outFile`: https://www.typescriptlang.org/docs/handbook/compiler-options.html – Cameron Tacklind Feb 21 '19 at 19:49
46

Using the GUI

If you have Visual Studio 2013 and the TypeScript extension installed, right-click your project in the solution explorer and chose Properties. Click on the TypeScript Build tab. Select Combine JavaScript output into file: and type in a name to use for your combined file in the input field right next to the option. Remember you can use variables in this field. For example: "$(ProjectDir)dist\js\myCombinedFile.js".

Manually

If you cannot find this GUI option anywhere, then modify your project configuration file manually. Go to your project folder; right-click the project in the solution explorer and click on Open folder in File Explorer. In the folder that pop up, you'll see a couple of files. Edit file myProject.csproj with any text editor of your choice. Find two lines that reads like so:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">

and

<PropertyGroup Condition="'$(Configuration)' == 'Release'">

Within the two tree of child nodes, add a new child to each parent:

<TypeScriptOutFile>$(ProjectDir)dist\js\myCombinedFile.js</TypeScriptOutFile>

When you get back to Visual Studio, he should ask you whether or not to reload the project. Of course this is something that has to be done for the changes to take effect!

The manual procedure I just described is exactly what the GUI procedure would do for you. My thoughts around the manual procedure originates from this source.

Finally

Build your project as you would do normally. If it doesn't work, try reloading your project.

Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
  • This does not appear to be working with VS2013.1 and TS RC1.0 (0.9.7.0). "Redirect JavaScript output" option works, but no combined file is generated. – Zac Morris Mar 17 '14 at 14:44
  • 1
    I'm getting an empty file generated when I use the combine option. Anyone experienced this? Each file separately is generated fine. – Tamir Daniely Apr 23 '14 at 05:29
  • 4
    This (GUI version) works is VS21013 Express Release 3, but you have to use `/// ` to force the dependencies, otherwise the files may be appended in the wrong order – Jeremy Sorensen Sep 10 '14 at 22:18
  • @TamirDaniely Set the module system to 'none' [ See the answer at http://stackoverflow.com/questions/28624372/visual-studio-can-not-combine-javascript-output-into-one-file-is-there-any-adju: – Gerrit Begher Apr 20 '15 at 16:17
  • If using the manual solution. Is there any way to minify and/or version the file (dist\js\myCombinedFile.js?v=xxx)? – dmathisen Jun 29 '15 at 15:25
  • IIRC, Visual Studio has the option to combine different JavaScript source code files into one file. But not to minimize them. It is rather easy to trigger build hooks that do this job for you. In the end, this whole process will not be very portable. I personally use Gradle as my build tool both to compile `.ts` into `.js` and to minimize the JavaScript files. I am not sure how to do versioning in Visual Studio. You should post your comment as a real question on StackOverflow, perhaps elaborating it a bit first =) – Martin Andersson Jun 29 '15 at 20:00
  • using `$(ProjectDir)` did not work... VS 2015 actually created a folder called `$(ProjectDir)` and put the file there... – Serj Sagan Sep 04 '15 at 00:15
  • It should be noted, that as of posting this, the Typescript build settings screen is a mess. The setting in question is the last option listed, and the input box required to set the path is cut off. I had to tab into it and type blindly to set it. There's an open issue in the Visual Studio Feedback center for this, but it only has 7 votes so far :/. – Carcigenicate Apr 01 '16 at 22:57
  • The combined .js file will be loaded by requirejs but unfortunately the parameter is every time "undefined". Therefore this solution not works. – Simon Feb 08 '19 at 15:02
16

You do not need any third-party tool or library for this.
You can use Microsoft's System.Web.Optimization:

BundleTable.Bundles.Add(new ScriptBundle("~/scripts/bundle.js").IncludeDirectory("~/scripts/", "*.js", true));

All the *.js files (results of compiling your *.ts files) will be combined and accessible at runtime at:
http:// .../scripts/bundle.js

Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95
11

You can use tsc --project ./ to build an concatenated output file if you use tsconfig.json configuration file in the current directory.

The tsconfig.json file may look like this :

{
  "compileOnSave": true,
  "compilerOptions": {
    "module":"system",
    "target": "es5",
    "sourceMap": true,
    "outFile": "dist/app-build.js"
  },
  "files":[
    "./src/index.ts",
    "./typings/browser.d.ts"
  ]
}

Classic files layout would be :

project/
   - src/ 
        - index.ts
        ... all my ts files
   - dist /
       - vendor.js        # angular, jquery...
       - app-build.js     # our build project
       - system.js        # Module loader we used
       - index.html       # call all js
   - typings/
       - browser.d.ts
       - main.d.ts     # for node.js server side, not included
   - package.json
   - tsconfig.json

The bad news is that we need a call to SystemJS (works the same with RequireJS, but as of Tsc 1.8, CommonJS is not accepted for concatenated build)

So let's learn quickly about SystemJS : Add SystemJS, and call a module in index.html :

<html lang="en" ng-app="skeleton">
<head>
    <script src="system.js" type="text/javascript"></script>
    <script src="angular.min.js"></script>
    <script src="app-build.js" type="text/javascript"></script>
</head>
<body>

   <skeletonDirective></skeletonDirective>

<script type="text/javascript">

    System.import('index')

</script></body></html>

A big advantage is that you can also let your ide bundle it for you. The IDE need anyway to compile to understand types, so we may avoid to compile it twice. You don't need Gulp, browserify or anything for the moment. SystemJS might do most of the stuff like loading a html template.

Behnam
  • 6,244
  • 1
  • 39
  • 36
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
2

If I got you right, there is another way to produce a single JS file:

  1. Create a new HTML file and include all your .ts files using:

    <script type="text/typescript" src="GameModule.ts"></script>

    <script type="text/typescript" src="app.ts"></script>

  2. Add TypeScript Compile .js files:

    <script type="text/javascript" src="http://niutech.github.com/typescript-compile/js/typescript.min.js"></script>

    <script type="text/javascript" src="http://niutech.github.com/typescript-compile/js/typescript.compile.min.js"></script>

  3. Open this HTML file in a browser. The automatically compiled JS code will be injected into a single <script type="text/javascript"></script> tag at the end of the body. You can preview and copy it using Web Inspector or Firebug.

Here is a working demo.

niutech
  • 28,923
  • 15
  • 96
  • 106
-4

Add a postbuild that executes Browserify.js

Josh Reuben
  • 577
  • 3
  • 20