TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Option for a light compilation using *.d.ts files?

Open klarezz opened this issue 12 years ago • 10 comments

When compiling a script (main.ts) that imports another module (dependency.ts):

  • typescript 0.9.5 used to take as input the definition file (dependency.d.ts) when available.
  • since 1.0.x, it seems like typescript always compiles dependency.ts even when a definition file is available.

This significantly slows down compilation for most projects. Is there a workaround? A compilation argument to opt for the old behavior?

klarezz avatar Aug 17 '14 14:08 klarezz

typescript 0.9.5 used to take as input the definition file (dependency.d.ts) when available.

I didn't know that. The ordering code here here (as per my understanding) .ts before .d.ts:

https://github.com/Microsoft/TypeScript/blob/eea8409e4f84441e3a206db50c22efddd60cbb3a/src/compiler/parser.ts#L3605

basarat avatar Aug 18 '14 07:08 basarat

thanks for pointing me to the current code ! seems like compiler/parser.ts didn't exist in 0.9.5 (commit d00b79a93c9e0d8bdbe748d2b854afa5848599d0 in old repository)

still, a compilation argument would be great!

klarezz avatar Aug 18 '14 07:08 klarezz

The problem with this flag, in any simple implementation, is that it would be a gigantic trap. People would turn this on, change one file, then not understand why a referencing file didn't see the changes in it. We'd need to see some kind of mitigation for that.

RyanCavanaugh avatar Aug 18 '14 17:08 RyanCavanaugh

@RyanCavanaugh perhaps an explicit .d.ts extension should be allowed : https://github.com/Microsoft/TypeScript/blob/eea8409e4f84441e3a206db50c22efddd60cbb3a/src/compiler/parser.ts#L3597 ?

basarat avatar Aug 19 '14 00:08 basarat

The rational here is .d.ts is generated from a .ts, so if the compiler can reach a .ts, it is better to use than a .d.ts as the source will be more up-to-date than a generated output. so for instance if you have module A, and a dependent module B, and you make a change to module A, and build modules in reverse order B, then A, you can end up with a successful compilation for both, if B looks at an older A.d.ts.

One alternative is to have output in a different location than the input, this will ensure that there is no ambiguity about which files you are picking up.

mhegazy avatar Aug 19 '14 17:08 mhegazy

Yes, there could be compilation errors due to outdated files, but it's a small price to pay compared to the time saved compiling. I'm still using 0.9.5 right now, and it doesn't bother me to recompile specific files when I have (explicit) errors. Besides I guess that most of the times files are compiled upon change, at least that's my setup.

I don't see much trouble into allowing this through a compilation flag. As a precaution, a warning could be displayed when compiling with this flag.

klarezz avatar Aug 19 '14 20:08 klarezz

It seems like the suggested option may also lead to symbols duplication when using ///<reference path='file.ts'/>. It is replaced with ///<reference path='file.d.ts'/> when creating the definition file. So we might end up having both file.ts and file.d.ts included in the compilation process.

klarezz avatar Sep 23 '14 07:09 klarezz

This is an issue for me, too, as I develop for node.js.
Because I develop for node.js, all of my .ts files result in commonjs modules, and my references are only to declaration files for commonjs modules. (I just searched my entire source tree, and there isn't a single instance of a reference statement for an implementation file.)

@mhegazy: I consider my declaration files to be my standard, so I want the compiler to use just those, similarly to C++ include files. If my implementation incorrectly implements my standard (the declaration file), I consider my implementation file to be incorrect, not my interface file. I think of my declaration file as my contract with the end user, and my implementation doesn't affect that contract.

@RyanCavanaugh: I have my build system set up so implementation files have dependencies on the declaration files they include. So to the extent I specify these dependencies correctly, my builds work correctly. However, it's tedious to keep these dependencies up to date, I haven't yet figured out how to build so these dependencies are found automatically, so I do have the problem you mentioned. This problem disappears when I do a clean build.

psnider avatar Nov 19 '14 16:11 psnider

There is one other problem that this compilation mode would solve for me:

I like to work on one module at a time. But when there is an error in another referenced module, I see all the errors from that module as well. At best this is distracting, at worst it slows down my development.

I imagine that this is more of a problem for developers who write external modules.

psnider avatar Dec 03 '14 00:12 psnider

Using TypescriptServices, it seems possible to cache files and only recompile changed code. It seems to be the right way to go to reduce compile time. Closing this issue.

klarezz avatar Apr 24 '15 15:04 klarezz