使用 RequireJS 2.1 的 AMD 样式模块中的 TypeScript 1.8 相对模块名称解析
TypeScript 1.8 relative module name resolution in AMD style modules with RequireJS 2.1
我正在尝试在遗留 JS 系统的一部分中使用 TypeScript。
在这个系统中,存在许多在 RequireJS 环境中执行的应用程序,其中 main.js 将在该应用程序启动时执行。我创建了这个结构
App1/
main.js
app.ts
Modules/
myModule.ts
Models/
model1.js
model2.js
模型是生成的代码,必须保留为 .JS 文件。
来自不同 .ts 模块的 'Require' 语句应该需要模型。
我知道如何在模块文件夹中的声明文件中使用类似的东西来编译 .ts 文件。
declare module "Models/model1" { var x:any; export = x;}
但是代码不会执行,因为这将为 myModule.ts
创建这样一个 AMD 模块定义
define(["require","exports","Models/model1", function(require,exports,model){ ...
但是路径被 Require.JS 解析为
http://mysite/Models/model1.js
缺少路径部分,因为路径是相对的,必须是来自 RequireJS 上下文根的绝对路径。我不能将模型作为 TS,因为它们是生成的文件。他们不只是通过重命名为 .ts 来编译
由于应用程序在线束、生产和单元测试设置中运行的方式不同,因此非相对路径也不可用。
问题:有没有办法让 TypeScript 为 JavaScript (.js) 模块生成相对路径?
您调查过 Triple-Slash Directives 了吗?从 TypeScript 2.0 开始,这已被弃用 import "moduleName"
但应该适用于旧版本:
/// <amd-dependency path="x" />
informs the compiler about a non-TS
module dependency that needs to be injected in the resulting module’s
require call.
The amd-dependency directive can also have an optional name property;
this allows passing an optional name for an amd-dependency:
/// <amd-dependency path="legacy/moduleA" name="moduleA"/>
declare var moduleA:MyType
moduleA.callStuff()
Generated JS code:
define(["require", "exports", "legacy/moduleA"], function (require, exports, moduleA) {
moduleA.callStuff()
});
我正在尝试在遗留 JS 系统的一部分中使用 TypeScript。 在这个系统中,存在许多在 RequireJS 环境中执行的应用程序,其中 main.js 将在该应用程序启动时执行。我创建了这个结构
App1/
main.js
app.ts
Modules/
myModule.ts
Models/
model1.js
model2.js
模型是生成的代码,必须保留为 .JS 文件。 来自不同 .ts 模块的 'Require' 语句应该需要模型。
我知道如何在模块文件夹中的声明文件中使用类似的东西来编译 .ts 文件。
declare module "Models/model1" { var x:any; export = x;}
但是代码不会执行,因为这将为 myModule.ts
创建这样一个 AMD 模块定义define(["require","exports","Models/model1", function(require,exports,model){ ...
但是路径被 Require.JS 解析为
http://mysite/Models/model1.js
缺少路径部分,因为路径是相对的,必须是来自 RequireJS 上下文根的绝对路径。我不能将模型作为 TS,因为它们是生成的文件。他们不只是通过重命名为 .ts 来编译 由于应用程序在线束、生产和单元测试设置中运行的方式不同,因此非相对路径也不可用。
问题:有没有办法让 TypeScript 为 JavaScript (.js) 模块生成相对路径?
您调查过 Triple-Slash Directives 了吗?从 TypeScript 2.0 开始,这已被弃用 import "moduleName"
但应该适用于旧版本:
/// <amd-dependency path="x" />
informs the compiler about a non-TS module dependency that needs to be injected in the resulting module’s require call.The amd-dependency directive can also have an optional name property; this allows passing an optional name for an amd-dependency:
/// <amd-dependency path="legacy/moduleA" name="moduleA"/> declare var moduleA:MyType moduleA.callStuff()
Generated JS code:
define(["require", "exports", "legacy/moduleA"], function (require, exports, moduleA) { moduleA.callStuff() });