21

In an attempt to separate my .ts source files from the .js output, I'm trying to implement a formal file-watcher in TypeScript and it seems as though the ability to specify an output path for a single file does not exist.

The flow, just to be clear: I begin watching my entire src directory, make a change to, say, src/views/HomeView.ts, and i want node to pick up that the file has been changed, and move the compiled version to public/js/views/HomeView.js.

Using tsc myfile.ts --out myfile.js it travels through all of the modules and compiles each in the same path that the .ts file exists, without placing the final file(s) in the properly specified path. It does however create an empty file where I would like it to end up.

I'm wondering:

1) Is it possible to use the --out parameter and only compile that one file? I do not want it to traverse imports and compile every single file, but merely do syntax / error checking at compile-time (and that's only a secondary requirement, not necessary); and

2) Is there a bug in the compiler that prevents it from properly combining / creating files? Again, the final output in the --out path directive is empty, but a file is indeed created.

Any help would be appreciated.

* Update *

While I don't want to close this question as I do believe it's still an issue, I went ahead and implemented the core TypeScript compiler to achieve what I needed to do, bypassing tsc altogether. Please see https://github.com/damassi/TypeScript-Watcher for more information and usage.

cnp
  • 1,010
  • 2
  • 9
  • 20
  • This is an issue for us, specifically part 1 where the compiler is recompiling all the dependencies and therefore changing them all very slightly but not functionally requiring them all to be re-checked in to source control. – Ed Bishop Jul 23 '15 at 09:58

1 Answers1

20

When you use the --out parameter, you get a single file with the compiler walking the dependencies and working out the correct order for the final file.

For example...

tsc app.ts --outfile final.js 

Will find any dependencies in app.ts and compile them all too. After it works out the correct order it will save all of the JavaScript in final.js. If this JavaScript file is empty, it is normally indicative of a compiler error.

It is not possible to use the --out parameter to generate a JavaScript file for just the TypeScript file you specify, unless that file has no dependencies.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 5
    I'd just like to add you can use `outFile` in the tsconfig too. – John Pavek Feb 02 '19 at 17:17
  • 1
    My personal opinion is that if you have modules (I approve) you can also load modules on demand (I approve) rather than bundle them all into one big file (I disapprove). This is just my personal view of course, but in the wild I see a great deal of benefit to fully embracing modules right through to production rather than just using them at "development time". – Fenton Apr 01 '19 at 10:10
  • `--out` is [deprecated](https://www.typescriptlang.org/docs/handbook/compiler-options.html) since TypeScript 5.5 – AirOne Jun 06 '23 at 12:21
  • @AirOne updated to use `outfile`. – Fenton Jun 06 '23 at 12:43