javascript - typescript 中有类似 'babel.transform().code'的功能吗?

标签 javascript typescript webpack web-component

我需要设置 webpack 来加载网络组件。麻烦的是我还想用typescript。到目前为止,我找到的唯一解决方案是 web-components-loader .看起来它可以使用 babel 从 es6 转换代码。 typescript 是否有类似的可能性?

webComponentsLoader: {
    transformJs: rawCode => {
        return babel.transform(rawCode, {
            presets: ['es2015']
        }).code;
    }
}

最佳答案

可以使用编译器提供的transpile方法:

import * as ts from "typescript";

let compiledSource = ts.transpile("let x: string  = 'string'", {
    target: ts.ScriptTarget.ES2015
});

参见 the documentation on how to use the compiler API .

关于javascript - typescript 中有类似 'babel.transform().code'的功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46478887/

相关文章:

javascript - 无法使用 Snap.svg 在文本路径上设置 startOffset 属性

javascript - 从嵌套的 .then 函数获取数据

javascript - 使用 *ngFor 遍历数组,同时过滤特定属性

javascript - 使用 Webpack 为移动设备和桌面设备提供单独的 bundle

intellij-idea - Webpack 有时无法检测到使用 Intellij Idea 保存的更改

javascript - 为什么舍入值不同?

javascript - 使用 JavaScript 显示基于时间的甘特图

typescript - 我可以使用 Typescript 编译器 API 更改节点的源文件吗?

typescript - Joi 类型的声明合并

webpack - Webpack SASS 源映射是否损坏?