clarkmcc / go-typescript

Compile and run Typescript code natively in Go
GNU General Public License v3.0
104 stars 11 forks source link

Failing to evaluate an AMD module #12

Closed lilili87222 closed 7 months ago

lilili87222 commented 1 year ago

//md.ts

    function factory(exports) {
    class Person {
        name: string;

        constructor(name: string) {
            this.name = name
        }

        public greet(): string {
            return `Hello ${this.name}!`
        }
    }
        exports.multiply = Person ;
    }
    define("myModule", ["exports"], factory);

//md_test.ts

    import { Person } from 'myModule';
    const me = new Person("John Doe******")
    me.greet()

//main.go

    func buildCommand2() {
        mdFile, _ := os.Open("./tscript/md2.ts")
        if mdFile == nil {

        }
        mdTestFile, _ := os.Open("./tscript/md2_test.ts")
        js, e := typescript.Transpile(mdTestFile)
        fmt.Println(e, js)

        result, err := typescript.Evaluate(mdTestFile,
            typescript.WithTranspile(),
            typescript.WithAlmondModuleLoader(),
            typescript.WithEvaluateBefore(mdFile),
        )
        if err != nil {
            panic(err)
        }
        fmt.Println(result.ToInteger())
    }
clarkmcc commented 1 year ago

I think your issue is probably because you're evaluating the mdFile without first transpiling it. The following should work:

// Open both the files
mdFile, err := os.Open("md.ts")
if err != nil {
    panic(err)
}
mdTestFile, err := os.Open("md_test.ts")
if err != nil {
    panic(err)
}

// Transpile the module file
script, err := typescript.Transpile(mdFile)
if err != nil {
    panic(err)
}

// Transpile + evaluate the test file
result, err := typescript.Evaluate(mdTestFile,
    typescript.WithTranspile(),
    typescript.WithAlmondModuleLoader(),
    // NOTE: Scripts passed to evaluate before should be Javascript, not Typescript
    typescript.WithEvaluateBefore(strings.NewReader(script)),
)
if err != nil {
    panic(err)
}
fmt.Println(result.ToString())
lilili87222 commented 1 year ago

panic: TypeError: Value is not an object: undefined at :4:10(19)

There will be another problem,if there are many modules ,how to handle dependenses?

clarkmcc commented 1 year ago

@lilili87222 I don't know that this is the best solution for handling a big project with many modules and dependencies. For example, there's no async runtime in here, this is just a scripting engine, so the best application of this project is for projects that need a simple scripting engine.

That said, I do use this in production around the world today to compile and evaluate several million scripts/day and for that application it seems to work well. The way I handle dependencies is I compile all the dependencies using tsc to AMD modules, then I provide that massive bundle to the runtime with typescript.WithEvaluateBefore(...), and then I run the smaller, simpler Typescript script that actually imports and calls those dependencies.

This project was not intended to be a project bundler, it was meant to be a scripting engine. If you combine it with an existing bundler like tsc for all your dependencies, I think you can get what you're looking for.

If you'd like to tell me more about your project, I'd be happy to try and point you in the right direction.