Comments (3)
Your examples @nwidger were invaluable.
I ran into issues formatting the string correctly before it get's passed into ts.transpile
and ended up writing a library for it that we use internally at my company. I just published the library here for anyone that wants a quick an easy way to compile Typescript using goja, https://github.com/clarkmcc/go-typescript:
output, err := typescript.TranspileString("let a: number = 10;", nil) // output: var a = 10;
This is a seriously cool project, thank you for all the work you've put into this!
from goja.
Looks like it's blowing up on line 2935 character 17 of typescriptServices.js
:
debugger;
I was able to get the first vm.RunString
to finish without panic
ing by adding an empty case *ast.DebuggerStatement:
to compiler.compileStatement
in compiler_stmt.go
, but I doubt this is correct.
from goja.
I had to modify the test program to properly call the TypeScript compiler methods. Here's a version that prints out the transpiled TypeScript code (assuming the empty case
statement has been added to compiler.compileStatement
:
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/dop251/goja"
)
func main() {
buf, err := ioutil.ReadFile("typescriptServices.js")
if err != nil {
log.Fatalln(err)
}
vm := goja.New()
v, err := vm.RunString(string(buf))
if err != nil {
log.Fatalln(err)
}
str := `'' +
'interface Person {' +
' firstName: string;' +
' lastName: string;' +
'}' +
'' +
'function greeter(person: Person) {' +
' return "Hello, " + person.firstName + " " + person.lastName;' +
'}' +
'' +
'var user = { firstName: "Jane", lastName: "User" };' +
'' +
'document.body.innerHTML = greeter(user);' +
''`
v, err = vm.RunString(`
var content = ` + str + `;
var compilerOptions = { module: ts.ModuleKind.System };
var res1 = ts.transpileModule(content, {compilerOptions: compilerOptions, moduleName: 'myModule2'});
var res2 = ts.transpile(content, compilerOptions, /*fileName*/ undefined, /*diagnostics*/ undefined, /*moduleName*/ "myModule1");
res2;
`)
if err != nil {
log.Fatalln("compile:", err)
}
fmt.Println(v)
}
which outputs:
System.register("myModule1", [], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
function greeter(person) { return "Hello, " + person.firstName + " " + person.lastName; }
var user;
return {
setters: [],
execute: function () {
user = { firstName: "Jane", lastName: "User" };
document.body.innerHTML = greeter(user);
}
};
});
Voila, a working TypeScript to Javascript transpiler in Go! This is a very cool project, thanks for all the hard work you've done on it so far!
from goja.
Related Issues (20)
- Support for bigint? HOT 1
- AST Walk / Visitor pattern HOT 1
- Does goja support WebAssembly? or somehow run bytecode on goja? HOT 1
- Is the Web Platform in scope? HOT 1
- How to define async function in Goja HOT 4
- Subscribe *Promise Pending Status HOT 1
- How to Throw an Error From the Golang Function
- What Is Types Marshaling Among JS and GOLANG Spaces.
- Support for setting a Promise as handled to true HOT 3
- how to impove performance for a amount of constant literal in a javascript file HOT 4
- vm reuse problem HOT 2
- Request for Assistance: Obtaining Abstract Syntax Tree (AST) with Goja HOT 1
- How to limit the number of for loops HOT 1
- Fibers/Coroutines HOT 4
- Builtin Date locale's methods ignore locale argument HOT 1
- Export JS array to Go slice HOT 2
- if i want to let users do js on my server like cloudflare workers, is it possible to use your repo to do so? HOT 1
- abnormal string input to js function HOT 1
- Go binding for function rest params does not work as expected HOT 2
- how to register custom object into goja like before? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goja.