从节点使用 TypeScript 编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12729779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:12:18  来源:igfitidea点击:

Use TypeScript compiler from node

node.jstypescript

提问by Ilia Choly

It's pretty easy to do this with coffee-script.

使用咖啡脚本很容易做到这一点。

var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n  var a;\n\n  a = 1;\n\n}).call(this);\n'

Is there a way to do this with typescript?

有没有办法用打字稿做到这一点?

Edit:also posted on codeplex

编辑:张贴在codeplex

回答by topek

It seems that nowadays there is a simpler solution, you can do:

现在似乎有一个更简单的解决方案,您可以这样做:

let ts = require('typescript');
let source = ts.transpileModule('class Test {}', {}).outputText;

This results in:

这导致:

"use strict";
var Test = (function () {
    function Test() {
    }
    return Test;
}());

回答by joshuapoehls

Since TypeScript's NPM module doesn't export any public interface, the only way to do this currently is to execute the tscprocess.

由于 TypeScript 的 NPM 模块不导出任何公共接口,因此目前执行此操作的唯一方法是执行该tsc过程。

var exec = require('child_process').exec;

var child = exec('tsc main.ts',
                function(error, stdout, stderr) {
                    console.log('stdout: ' + stdout);
                    console.log('stderr: ' + stderr);
                    if (error !== null) {
                      console.log('exec error: ' + error);
                    }
                });

An issuehas been opened to request a public interface for the TypeScript module.

已打开一个问题来请求TypeScript 模块的公共接口。

回答by Olivier Lalonde

better-requirecould help you achieve this.

Better-require可以帮助您实现这一目标。

It lets you require() typescript files - no pre-compilation needed - and a bunch of other file formats (coffeescript, clojurescript, yaml, xml, etc.)

它允许您使用 require() 打字稿文件 - 无需预编译 - 以及一堆其他文件格式(coffeescript、clojurescript、yaml、xml 等)

require('better-require')();
var myModule = require('./mymodule.ts');

Disclosure: I wrote better-require.

披露:我写了更好的要求。

回答by Edwin Yip

Check this github projectby niutech, it can convert TypeScript code to JS code on the fly in browser, but I guess it can be easily be modified to work in node.js.

检查niutech的这个github项目,它可以在浏览器中即时将TypeScript代码转换为JS代码,但我想它可以很容易地修改为在node.js中工作。

I found it while I'm investigating the possibility of support TypeScript in my live, firebug-inspired code editor.

我在研究在受萤火虫启发的实时代码编辑器中支持 TypeScript 的可能性时发现了它。

回答by Paul Go

Not answering the question directly, but since Googling for "run TypeScript from node directly" brings up this StackOverflow page, I figure I should add that you're able to do this with ts-node: https://github.com/TypeStrong/ts-node

没有直接回答这个问题,但由于谷歌搜索“直接从节点运行 TypeScript”会出现这个 StackOverflow 页面,我想我应该补充一点,你可以使用 ts-node 来做到这一点:https: //github.com/TypeStrong /ts-节点

回答by cancerbero

Official documentation about how to use TypeScript transpiler API to generate JavaScript source from a .ts file:

关于如何使用 TypeScript transpiler API 从 .ts 文件生成 JavaScript 源代码的官方文档:

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-transpiling-a-single-file

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-transpiling-a-single-file

Official documentation about how to use TypeScript compiler API to compile a .ts file or a TS project to

关于如何使用 TypeScript 编译器 API 编译 .ts 文件或 TS 项目的官方文档

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-a-minimal-compiler

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-a-minimal-compiler

(the later doesn't answer the original question but is very common to access /modify the AST and then transpile to object language so it could be usefull)

(后者没有回答原始问题,但访问/修改 AST 然后转换为对象语言非常常见,因此它可能很有用)