【发布时间】:2015-07-24 20:32:00
【问题描述】:
我们有一个 AMD Typescript 项目,它的目录结构类似于:
project_root/
/scripts
/ts
module.ts
/js (generated from the TS files)
/tests
/ts
moduleTest.ts (imports module.ts)
/js (generated from the TS files)
问题是,当我们编译一个文件,该文件从一个不是导入器目录后代的目录中导入另一个模块(文件)时,TS 编译器会从两个目录的直接共享父目录重建整个树,到js 目录。
例如,将tests/ts/moduleTest.ts(即导入scripts/ts/module.ts)编译到tests/js 目录中,将产生这个tests/js 目录:
project_root/tests/js
/scripts/ts
module1.js
/tests/ts
module1test.js
而不仅仅是project_root/tests/js 中的module1test.js。
在现实生活中情况会更糟,因为module1.ts 本身会从子目录中导入更多模块,所有这些模块都会被创建到project_root/tests/js 中。
除了导入生成的.js 文件并引用对应的.d.ts 文件而不是导入.ts 文件。是否有创建整个树的解决方案?最好,有没有办法告诉编译器不要编译导入的 TS 文件而只将它们用作参考?
我制作了一个基本的example/testing repository 用作游乐场。要查看我在说什么,从项目根目录运行:
tsc --module amd -t ES5 --outDir tests/js/ tests/ts/module1spec.ts
该项目使用 requireAdapters.d.ts 来避免在 import 语句中使用相对模块名称(换句话说,它在 TS 和 require.js 模块名称之间进行转换)。
【问题讨论】:
标签: typescript amd