如何在浏览器中编译TypeScript代码? [英] How to compile TypeScript code in the browser?

查看:246
本文介绍了如何在浏览器中编译TypeScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在浏览器中运行TypeScript编译器,以便在浏览器中将TS转换为JS 100%。用例将实现一个在线TypeScript IDE,该IDE在100%的客户端运行,并且具有一个播放按钮来执行该项目。因此,我需要将项目转换为JavaScript,以便浏览器执行代码。

Is it possible to run the TypeScript compiler in the browser for transpiling TS to JS 100% in the browser. The use case would be implementing an online TypeScript IDE that runs 100% client side and it has a "Play" button to execute the project. So I need to transpile the project to JavaScript in order for the browser to execute the code.

我认为它应该与加载相关的打字稿JS文件,创建正确的类的实例(编译器?)并调用一样简单一种或两种方法。

I presume it should be as simple as loading the relevant typescript JS files, creating an instance of the right class (compiler?) and calling a method or two.

在浏览器中加载编译器的方式是什么? TypeScript编译器API参考文档在哪里?我应该从哪里开始挖掘?

What would be the means suitable to load the Compiler in the browser? Where is the TypeScript Compiler API Reference Documentation ? Where should I start digging in ?

这不是要求使用任何特定工具,而是用这种特定计算机语言执行此操作的任何方式,因此成为话题。

This isn't asking for any specific tool, but ANY way to do this with this particular computer language, and thus is on topic.

推荐答案

您可以使用 typescript-script https://github.com/basarat/typescript-script

但是不要在生产中这样做,因为它会很慢。

However do not do this in production as it is going to be slow.

您可以使用webpack(或类似的模块捆绑器)在浏览器中加载npm软件包。

You can use webpack (or a similar module bundler) to load npm packages in the browser.

这篇关于如何在浏览器中编译TypeScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆