Using references in one file

Topics: General, Language Specification
Jan 28, 2013 at 8:03 AM
Edited Jan 28, 2013 at 8:05 AM

Hi, today we saw http://www.stevefenton.co.uk/Content/Blog/Date/201301/Blog/Getting-The-Right-Set-Up-For-TypeScript/   with interesting idea. Still we have to reference all used files in each *.ts file. Steve (or better Mark) proposes that we should have one huge file in a project that contains paths to all *.ts modules.

/// <reference path="modulea.ts" />
/// <reference path="moduleb.ts" />
/// <reference path="modulec.ts" />
/// <reference path="moduled.ts" />
/// <reference path="modulee.ts" /

Then we just rerefence this file in all the other *.ts files and we don't have to manage the references individually in the *.ts files.

My question is - does it work with --out compiler option? The ///<reference is used as well for resolving dependencies. Then if everything depends on everything (with the approach above), how compile detects the dependencies? Does it take output from some lexical analysis? Will that work?

Coordinator
Jan 28, 2013 at 4:41 PM

To my understanding, when you use references like that a dependency graph is built.  Nodes that refer to each other are allowed in this graph, the compiler is just going to make the definitions of both files available to each other during compilation.

Whether or not you use the --out flag shouldn't effect the dependencies that are used.  The compiler builds out the definitions and will output these, if you specify --out <filename> and are not using external modules, into a single .js output file.

Jan 28, 2013 at 5:19 PM

Hi, I'm the Mark mentioned in that blog post.

Yes, this works with the --out compiler option. You create this one central file (references.ts) which references all your .ts files, reference that in, say, app.ts, and then build using

tsc --out app.js ap.ts

and it will automagically resolve all the dependencies.

I'm using this approach in a reasonably complex project (with AngularJS and Bootstrap) and it makes life much easier.

Jan 28, 2013 at 6:36 PM

Guys, are you really sure that the dependency graph is taken into account during the code concatenation triggered by the --out option?

As far as I understand, the references are used only to resolve types and not during subsequent building stages.

Jan 28, 2013 at 8:28 PM

I too have a single ts that contains all my references and I use --out w/ a single file and it works well, order is not an issue. The compiler parses them all first (I believe) and then performs type checking and then emits the output of non-ambient code.

Jan 29, 2013 at 10:45 AM
thorn0 wrote:

Guys, are you really sure that the dependency graph is taken into account during the code concatenation triggered by the --out option?

As far as I understand, the references are used only to resolve types and not during subsequent building stages.

Yes, we're really sure. We're doing it. It works. Jeez...

Jan 29, 2013 at 11:32 AM

Guys, thanks for replies. I'll definitelly try that as this solution is much easier than managing multiple reference directives. I also suspected that not only the ///<reference is used for dependency tree, so you confirmed that.

It is also great that the file can be in some manner generated with some simple script, so managing the file won't be PITA at all. 

 

Jan 29, 2013 at 2:28 PM
Edited Jan 29, 2013 at 2:52 PM

@stejcz and @markrendle,

A couple of questions:

  • What kind of project set up are we discussing here? Visual Studio or command line?
  • Are the referenced modules in one project or in separate projects?

 

For the case where the modules are separated into multiple projects in Visual Studio there might be a slight problem with this approach, because you may end up re-compiling all your modules every single time.

We do things similarly, but we only reference the ".d.ts" declaration files - never the ".ts" files directly.

But if you follow the latter approach then there might be implications for the "--out" option (we use a bundling mechanism at the moment, so I can't tell).

 

Noel

Jan 29, 2013 at 5:03 PM

Noel,

It's a .NET web project, making heavy use of AngularJS. I haven't tried this approach with a Node.js project yet.

All the .ts files are in the same project. What I'd really like to do is create a project template where the output is a single .js file and all the .ts files in the project automatically reference each other.