自定义Typescript编译器输出

时间:2017-06-15 22:49:08

标签: typescript transpiler

我需要一些帮助来开始定制打字稿的编译器输出,或者如果可能的话更加精确。

我有openui5框架,这种语法(例如扩展类)与ES5 +标准有很大不同。 AMD的工作方式也不同。

以下是一个例子:

我想导入一个模块。打字稿应该是:

import { Button } from "sap/m/Button";

在javascript中输出类似的内容:

var Button = require("sap/m/Button");

但我需要的是:

var Button = sap.ui.require("sap/m/Button");

另一个例子:

使用getter和setter函数解析属性。所以当你扩展一个类时,你可以给它一个带有属性的json对象:

properties: {
   "title" : "string",                         // a simple string property, default value is undefined
   "buttonText" : {defaultValue: "Search"},    // when no type is given, the type is string
   "showLogoutButton" : {type : "boolean", defaultValue : true},   // a boolean property where a default value is given
   "width" : {type : "sap.ui.core.CSSSize", defaultValue : "50px"} // a CSS size property where a default value is given
}

现在openui5将获取给定属性并生成setter。例如,对于“title”属性,将生成两个函数:

getTitle();
setTitle(value);

但没有ES 5属性。

所以在打字稿中你不能像这样访问你的财产:

让myclass = new MyClass(); myclass.title =“Hello World”;

但你必须使用getter和setter方法:

让myclass = new MyClass(); myclass.setTitle(“Hello World”);

我想在这里做的是编写这个打字稿代码:

let myclass = new MyClass();
myclass.title = "Hello World";

转换为这个javascript代码:

let myclass = new MyClass();
myclass.setTitle("Hello World");

我想我可以使用装饰器在我的类上执行此操作,但是您不能在声明文件中使用装饰器(d.ts)。所以我不能这样使用基类。

所以这是我的问题:

是否可以扩展typescript转换器并告诉它以不同的方式编译这些东西?

非常感谢向正确的方向发展。

1 个答案:

答案 0 :(得分:1)

你可以制作自己的发布者。

  1. 通过扩展TypeScript源代码
  2. 脚本使用TypeScript Language Service(文档TSLS)和Compiler API(文档Compiler API),您可以通过简单的方式处理脚本并构建自己的转换器。
  3. 按框架要求编写代码。
相关问题