typescript

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2021 License: GPL-3.0 Imports: 11 Imported by: 3

README

Goja Typescript Transpiler

This package provides a simple interface using github.com/dop251/goja under the hood to allow you to transpile Typescript to Javascript in Go. This package has no direct dependencies besides testing utilities and has a 95% test coverage rate.

Example

For more examples, see the examples/ directory of this repository

Transpile Strings
output, err := typescript.TranspileString("let a: number = 10;", nil)
// output: var a = 10;
Transpile Reader
output, err := typescript.Transpile(reader, nil)
Custom Typescript Compile Options

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 can be added.

output, err = typescript.TranspileString(script, nil, typescript.WithCompileOptions(map[string]interface{}{
    "module": "none",
    "strict": true,
}))
Custom Typescript Version
Default Registry

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.

import _ "github.com/clarkmcc/go-typescript/versions/v4.2.2"

func main() {
    output, err := typescript.Transpile(reader, nil, typescript.WithVersion("v4.2.2"))
}
Custom Registry

You may want to use a custom version registry rather than the default registry.

import version "github.com/clarkmcc/go-typescript/versions/v4.2.2"

func main() {
    registry := versions.NewRegistry()
    registry.MustRegister("v4.2.3", version.Source)
    
    output, err := typescript.TranspileString("let a:number = 10;", &typescript.Config{
        TypescriptSource: program,
    })
}
Custom Version

Need a different typescript version than the tags we support in this repo? No problem, you can load your own:

program, err := goja.Compile("typescript", "<typescript source code here>", true)
output, err := typescript.Transpile(reader, &typescript.Config{
    CompileOptions:   map[string]interface{}{},
    TypescriptSource: program,
    Runtime:          goja.New(),
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transpile

func Transpile(reader io.Reader, cfg *Config, opts ...OptionFunc) (string, error)

Transpile transpiles the bytes read from reader using the provided config and options

func TranspileCtx

func TranspileCtx(ctx context.Context, script io.Reader, cfg *Config, opts ...OptionFunc) (string, error)

TranspileCtx compiles the bytes read from script using the provided context. Note that due to a limitation in goja, context cancellation only works while in JavaScript code, it does not interrupt native Go functions.

func TranspileString

func TranspileString(script string, cfg *Config, opts ...OptionFunc) (string, error)

TranspileString compiles the provided typescript string and returns the

Types

type Config

type Config struct {
	CompileOptions   map[string]interface{}
	TypescriptSource *goja.Program
	Runtime          *goja.Runtime
	// contains filtered or unexported fields
}

Config defines the behavior of the typescript compiler.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig creates a new instance of the Config struct with default values and the latest typescript source code.s

func (*Config) Initialize

func (c *Config) Initialize() error

type OptionFunc

type OptionFunc func(*Config)

OptionFunc allows for easy chaining of pre-built config modifiers such as WithVersion.

func WithCompileOptions

func WithCompileOptions(options map[string]interface{}) OptionFunc

WithCompileOptions sets the compile options that will be passed to the typescript compiler.

func WithRuntime

func WithRuntime(runtime *goja.Runtime) OptionFunc

WithRuntime allows you to over-ride the default runtime

func WithVersion

func WithVersion(tag string) OptionFunc

WithVersion loads the provided tagged typescript source from the default registry

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL