|
1 | | -# Goja Typescript Transpiler and Evaluator (with AMD module support) |
2 | | -This package provides a simple interface using [github.com/dop251/goja](github.com/dop251/goja) under the hood to allow you to transpile Typescript to Javascript in Go. In addition it provides an evaluator with a built-in AMD module loader which allows you to run Typescript code against a compiled typescript bundle. This package has no direct dependencies besides testing utilities and has a 95% test coverage rate. |
| 1 | + |
3 | 2 |
|
4 | | -Feel free to contribute. This package is fresh and may experience some changes before it's first tagged release. |
| 3 | +This package provides a thin wrapper around [goja](https://github.com/dop251/goja) (a native Javascript runtime for Go). There are no direct dependencies besides goja, and [testify](https://github.com/stretchr/testify) (for testing only). This package supports the following features: |
| 4 | +* Typescript compilation and evaluation. |
| 5 | +* A context-aware evaluation API to support cancellation. |
| 6 | +* AMD-style modules using the built-in [Almond module loader](https://github.com/requirejs/almond). |
| 7 | +* Custom Typescript version registration with built-in support for versions 3.8.3, 3.9.9, 4.1.2, 4.1.3, 4.1.4, 4.1.5, 4.2.2, 4.2.3, 4.2.4, and 4.7.2. |
| 8 | +* 90%+ test coverage |
5 | 9 |
|
6 | | -## Transpiling Examples |
7 | | -### Transpile Strings |
8 | | -```go |
9 | | -output, err := typescript.TranspileString("let a: number = 10;", nil) |
10 | | -// output: var a = 10; |
11 | | -``` |
| 10 | +## Installation |
12 | 11 |
|
13 | | -### Transpile Reader |
14 | | -```go |
15 | | -output, err := typescript.Transpile(reader, nil) |
16 | | -``` |
| 12 | + go get github.com/clarkmcc/go-typescript |
17 | 13 |
|
18 | | -### Custom Typescript Compile Options |
19 | | -You can optionally specify alternative compiler options that are used by Typescript. Any of the options [https://www.typescriptlang.org/docs/handbook/compiler-options.html](https://www.typescriptlang.org/docs/handbook/compiler-options.html) can be added. |
20 | | -```go |
21 | | -output, err = typescript.TranspileString(script, typescript.WithCompileOptions(map[string]interface{}{ |
22 | | - "module": "none", |
23 | | - "strict": true, |
24 | | -})) |
25 | | -``` |
26 | | - |
27 | | -### Custom Typescript Version |
28 | | -You can optionally specify which typescript version you want to compile using. These versions are based on the Git tags from the Typescript repository. If you're using a version that is supported in this package, you'll need to import the version package as a side-effect and will automatically be registered to the default registry. |
29 | | -```go |
30 | | -import _ "github.com/clarkmcc/go-typescript/versions/v4.2.2" |
31 | | - |
32 | | -func main() { |
33 | | - output, err := typescript.Transpile(reader, typescript.WithVersion("v4.2.2")) |
34 | | -} |
35 | | -``` |
36 | | - |
37 | | -### Custom Typescript Source |
38 | | -You may want to use a custom typescript version. |
39 | | - |
40 | | -```go |
41 | | -func main() { |
42 | | - output, err := typescript.TranspileString("let a:number = 10;", |
43 | | - WithTypescriptSource("/* source code for typescript*/")) |
44 | | -} |
45 | | -``` |
46 | | - |
47 | | -## Evaluate Examples |
48 | | -### Basic Evaluation |
49 | | -You can evaluate pure Javascript code with: |
50 | | - |
51 | | -```go |
52 | | -result, err := Evaluate(strings.NewReader("var a = 10;")) // returns 10; |
53 | | -``` |
54 | | - |
55 | | -### Transpile and Evaluate |
56 | | -Or you can transpile first: |
57 | | - |
58 | | -```go |
59 | | -result, err := Evaluate(strings.NewReader("let a: number = 10;"), WithTranspile()) // returns 10; |
60 | | -``` |
61 | | - |
62 | | -### Run Script with AMD Modules |
63 | | -You can load in an AMD module bundle, then execute a Typescript script with access to the modules. |
64 | | - |
65 | | -```go |
66 | | -// This is the module we're going to import |
67 | | -modules := strings.TrimSpace(` |
68 | | - define("myModule", ["exports"], function (exports, core_1) { |
69 | | - Object.defineProperty(exports, "__esModule", { value: true }); |
70 | | - exports.multiply = void 0; |
71 | | - var multiply = function (a, b) { return a * b; }; |
72 | | - exports.multiply = multiply; |
73 | | - }); |
74 | | -`) |
75 | | - |
76 | | -// This is the script we're going to transpile and evaluate |
77 | | -script := "import { multiply } from 'myModule'; multiply(5, 5)" |
78 | | - |
79 | | -// Returns 25 |
80 | | -result, err := EvaluateCtx(context.Background(), strings.NewReader(script), |
81 | | - WithAlmondModuleLoader(), |
82 | | - WithTranspile(), |
83 | | - WithEvaluateBefore(strings.NewReader(amdModuleScript))) |
84 | | -``` |
| 14 | +## Examples |
| 15 | +* [Transpile Typescript](examples/typescript_evaluate_test.go) |
| 16 | +* [Transpile and Evaluate Typescript](examples/typescript_evaluate_test.go) |
| 17 | +* [AMD Modules](examples/typescript_amd_modules_test.go) |
| 18 | +* [Context Cancellation](examples/typescript_context_test.go) |
0 commit comments