每个文件夹的Typescript模块

时间:2013-11-04 23:15:10

标签: typescript

我的应用程序有以下结构

app/
 |-common/
 |-modules/
 | |-projects/
 | | |-models.ts
 | | |-controller.ts
 | | |-commands.ts
 | | '-module.ts
 | |-login/
 | '-dashboard/
 |-typings/
 '-app.ts

为简洁起见,省略了common/login/dashboard/内的文件,但假设文件夹包含文件,模块之间没有交叉引用,但模块引用{{1}内的文件1}}自由。

我正试图找到一种方法让打字稿将这个项目编译成这个......

common/

我正在使用内部模块,因此像app/ |-common.js |-modules/ | |-projects.js | |-login.js | '-dashboard.js '-app.js 这样的文件如下所示:

models.ts

我尝试在每个文件夹的基础上组合打字稿文件,然后使用/// <reference path='../../common/ModelCollection.ts' /> module Sparrow.Projects { 'use strict'; // . . . export class Company { id: number; name: string; get text() { return this.name; } } } 进行编译,但管理参考文件比我最初的想法更难。事实上,我无法让它发挥作用。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您需要使用单独的编译来执行此操作。

示例:

<强>公共/ utils.ts

function utilFn1() { ... }

<强>公共/ fooManager.ts

/// <reference path="utils.ts" />  
class FooManager () { ... utilFn1(); ... }

<强>模块/仪表板/ panel.ts

/// <reference path="../../common.d.ts" />
var panel = new FooManager();

<强>模块/仪表板/ main.ts

/// <reference path="../../common.d.ts" />
/// <reference path="panel.ts" />
panel.load();

然后你的编译步骤如下:

tsc --out common.js --declaration common\utils.ts common\fooManager.ts
tsc --out modules\dashboard.js modules\dashboard\panel.ts modules\dashboard.main.ts

您可以考虑的另一件事是每个输出都有一个共同的“references.ts”文件,它只是指向所有文件的引用标记。例如,common/references.ts会引用utils.tsfooManager.ts,而这些文件会引用references.ts。这使得更清楚地了解哪些文件是逻辑组的一部分,并使您更明确地控制输出的顺序。在这种情况下,您只需将references.ts作为唯一输入传递给tsc,而不是单独传递所有文件。另请参阅this blog post

的“参考”部分

提问者提出的一个有用的澄清附录,我认为值得补充:

  

我想强调的是,在引用其他模块时引用声明文件(.d.ts)而不是.ts文件至关重要。那和分割编译所以声明文件可以自动生成就可以了。