最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compiling multiple Typescript files - Stack Overflow

programmeradmin6浏览0评论

My nodejs Typescript server has grown in plexity and now relies on a few classes defined in another .ts file. This has created a strange pilation problem:

  • tsc server.ts piles everything fine. But node server.js crashes at the first line that instantiates a class from the other .ts file

  • tsc --out server.js server.ts results in the error message: "module emit collides with emitted script"

  • tsc --out serv.js server.ts seems to work but actually piles everything EXCEPT server.ts. The code from the other files is there and node serv.js just returns without any output

I'm not the only one with this error, unfortunately the solution on codeplex doesn't work for me.

How do I use tsc correctly ?

My nodejs Typescript server has grown in plexity and now relies on a few classes defined in another .ts file. This has created a strange pilation problem:

  • tsc server.ts piles everything fine. But node server.js crashes at the first line that instantiates a class from the other .ts file

  • tsc --out server.js server.ts results in the error message: "module emit collides with emitted script"

  • tsc --out serv.js server.ts seems to work but actually piles everything EXCEPT server.ts. The code from the other files is there and node serv.js just returns without any output

I'm not the only one with this error, https://typescript.codeplex./workitem/294 unfortunately the solution on codeplex doesn't work for me.

How do I use tsc correctly ?

Share Improve this question edited Sep 13, 2024 at 9:44 Jonas 129k101 gold badges327 silver badges405 bronze badges asked Jan 11, 2013 at 22:51 lhklhk 30.1k36 gold badges137 silver badges217 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

Your server.ts dependencies need to be modules that export their surface area with top-level export directives, and server.ts should load them using import directives. The root cause here is that TypeScript has two different sorts of universes for pilation.

The first is the default one that you'd use for regular webpages, where some simple loader takes 1 or more source files in some fixed order and executes them in that order, and you're on your own for dependency ordering. This is called "program" pilation. In program pilation, you might do side-by-side pilation (a.ts => a.js, b.ts => b.js), or you might do concatenated pilation using --out ((a.ts + b.ts) => out.js).

In program pilation, you refer to your references using ///<reference> tags. If those references are to source files (.ts), they'll get concatenated in to the output if using --out, or emitted as a side-by-side .js file otherwise. If those references are to a declaration file (.d.ts), you're basically saying you will be getting definitions for those files loaded via the external loader (i.e. a <script> tag in the browser).

The second is the kind of pilation you'd use for node.js or other environments that do asynchronous or idempotent module loading with runtime dependency resolution. This called "module" pilation. Here, the --module flag you pass to tsc matters, and the only valid thing to do is side-by-side pilation, because loading a single file as a module is (generally) how the module loaders in node.js, etc work.

In module pilation, you use the export keyword on a top-level object (function, class, module, interface, or var) to control what's available to code that refers to you using import. You should only ever have /// <reference> tags that point to .d.ts declaration files, because the module-based runtime loaders don't have a notion of loading a naked JS file. You won't pile with --out.

You never want to mix and match these pilation modes, because it simply is not going to work. In fact, in 0.8.2.0, tsc will simply issue an error if you try to do this.

I have a web app with a lot of TypeScript files. Here is how I solved this problem:

  1. Created a global _references.ts file in the ~/Scripts dir. This file has a reference path=... for each ts file in the web project.
  2. Next I generated this file using a T4 template because its was being a pain to manage by hand.
  3. Also, using the T4 template, I ordered the references in _references.ts according to my TypeScript dependencies. e.g. All ..Base.ts files at the top.
  4. Also, I created a tsc argument file which starts with -out app.js followed by a list of every ts file in the project. This was also generated using T4 and I called it app.tsproj.
  5. Finally, I call tsc @app.tsproj to generate the JavaScript with all dependencies correctly ordered.

_references.ts T4 template: https://gist.github./danabenson/5224712

app.tsproj T4 template: https://gist.github./danabenson/5224718

发布评论

评论列表(0)

  1. 暂无评论