Giter Club home page Giter Club logo

Comments (3)

clarkmcc avatar clarkmcc commented on July 17, 2024 2

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.

nwidger avatar nwidger commented on July 17, 2024

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 panicing by adding an empty case *ast.DebuggerStatement: to compiler.compileStatement in compiler_stmt.go, but I doubt this is correct.

from goja.

nwidger avatar nwidger commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.