Closed lilili87222 closed 7 months 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())
panic: TypeError: Value is not an object: undefined at
There will be another problem,if there are many modules ,how to handle dependenses?
@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.
//md.ts
//md_test.ts
//main.go