自定义 Typescript 编译器输出
Customize Typescript compiler output
我需要一些帮助来开始自定义 typescript 的编译器输出,或者更准确地说,如果可能的话。
我有 openui5 框架,其语法(例如扩展 classes)与 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 函数解析。因此,当您扩展 class 时,您可以给它一个 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 属性。
所以在打字稿中你不能像这样访问你的属性:
让我的class = new MyClass();
myclass.title = "Hello World";
但您将不得不使用 getter 和 setter 方法:
让我的class = new MyClass();
myclass.setTitle("Hello World");
我想在这里做的是写这个打字稿代码:
let myclass = new MyClass();
myclass.title = "Hello World";
转换成这个 javascript 代码:
let myclass = new MyClass();
myclass.setTitle("Hello World");
我想我可以在我的 classes 上使用装饰器来做到这一点,但是你不能在声明文件中使用装饰器 (d.ts)。所以我不能以这种方式使用基础 classes。
所以这是我的问题:
是否可以扩展打字稿转译器并告诉它以不同的方式编译这些东西?
将不胜感激。
您可以制作自己的转译器。
- 通过扩展 TypeScript 源代码
- 通过脚本使用
TypeScript Language Service
(文档 TSLS) and Compiler API
(docs Compiler API)您可以处理脚本并通过简单的方式构建自己的转译器。
- 根据框架要求编写代码。
我需要一些帮助来开始自定义 typescript 的编译器输出,或者更准确地说,如果可能的话。
我有 openui5 框架,其语法(例如扩展 classes)与 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 函数解析。因此,当您扩展 class 时,您可以给它一个 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 属性。
所以在打字稿中你不能像这样访问你的属性:
让我的class = new MyClass(); myclass.title = "Hello World";
但您将不得不使用 getter 和 setter 方法:
让我的class = new MyClass(); myclass.setTitle("Hello World");
我想在这里做的是写这个打字稿代码:
let myclass = new MyClass();
myclass.title = "Hello World";
转换成这个 javascript 代码:
let myclass = new MyClass();
myclass.setTitle("Hello World");
我想我可以在我的 classes 上使用装饰器来做到这一点,但是你不能在声明文件中使用装饰器 (d.ts)。所以我不能以这种方式使用基础 classes。
所以这是我的问题:
是否可以扩展打字稿转译器并告诉它以不同的方式编译这些东西?
将不胜感激。
您可以制作自己的转译器。
- 通过扩展 TypeScript 源代码
- 通过脚本使用
TypeScript Language Service
(文档 TSLS) andCompiler API
(docs Compiler API)您可以处理脚本并通过简单的方式构建自己的转译器。 - 根据框架要求编写代码。