gopherjslib

package module
v0.0.0-...-1ae7130 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: MIT Imports: 10 Imported by: 3

README

gopherjslib

Go Reference

Package gopherjslib provides helpers for in-process GopherJS compilation.

All of them take the optional *Options argument. It can be used to set a different GOROOT or GOPATH directory or to enable minification.

Example compiling Go code:

import "github.com/shurcooL/gopherjslib"

...

code := strings.NewReader(`
	package main
	import "github.com/gopherjs/gopherjs/js"
	func main() { println(js.Global.Get("window")) }
`)

var out bytes.Buffer

err := gopherjslib.Build(code, &out, nil) // <- default options

Example compiling multiple files:

var out bytes.Buffer

builder := gopherjslib.NewBuilder(&out, nil)

fileA := strings.NewReader(`
	package main
	import "github.com/gopherjs/gopherjs/js"
	func a() { println(js.Global.Get("window")) }
`)

builder.Add("a.go", fileA)

// And so on for each file, then:

err = builder.Build()

Deprecated: The intermediate API layer implemented by this package has proven to be unhelpful and is now unmaintained. Use packages github.com/gopherjs/gopherjs/build and github.com/gopherjs/gopherjs/compiler or command github.com/gopherjs/gopherjs directly instead.

Installation

go get github.com/shurcooL/gopherjslib

License

Documentation

Overview

Package gopherjslib provides helpers for in-process GopherJS compilation.

All of them take the optional *Options argument. It can be used to set a different GOROOT or GOPATH directory or to enable minification.

Example compiling Go code:

import "github.com/shurcooL/gopherjslib"

...

code := strings.NewReader(`
	package main
	import "github.com/gopherjs/gopherjs/js"
	func main() { println(js.Global.Get("window")) }
`)

var out bytes.Buffer

err := gopherjslib.Build(code, &out, nil) // <- default options

Example compiling multiple files:

var out bytes.Buffer

builder := gopherjslib.NewBuilder(&out, nil)

fileA := strings.NewReader(`
	package main
	import "github.com/gopherjs/gopherjs/js"
	func a() { println(js.Global.Get("window")) }
`)

builder.Add("a.go", fileA)

// And so on for each file, then:

err = builder.Build()

Deprecated: The intermediate API layer implemented by this package has proven to be unhelpful and is now unmaintained. Use packages github.com/gopherjs/gopherjs/build and github.com/gopherjs/gopherjs/compiler or command github.com/gopherjs/gopherjs directly instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(reader io.Reader, target io.Writer, options *Options) error

Build builds JavaScript based on the go code in reader, writing the result to target. The first error during the built is returned. All errors are typed.

reader must not be nil
target must not be nil
options may be nil (defaults)

func BuildPackage

func BuildPackage(dir string, target io.Writer, options *Options) error

BuildPackage builds JavaScript based on the go package in dir, writing the result to target. Note that dir is not relative to any GOPATH, but relative to the working directory. The first error during the built is returned. dir must be an existing directory containing at least one go file

target must not be nil
options may be nil (defaults)

Types

type Builder

type Builder interface {
	// Add adds the content of reader for the given filename
	Add(filename string, reader io.Reader) Builder

	// Build builds and returns the first error during the built or <nil>
	Build() error
}

Builder builds from added files

func NewBuilder

func NewBuilder(target io.Writer, options *Options) Builder

NewBuilder creates a new Builder that will write to target.

target must not be nil
options may be nil (defaults)

func NewPackageBuilder

func NewPackageBuilder(dir string, target io.Writer, options *Options) (Builder, error)

NewPackageBuilder creates a new Builder based on the go package in dir.

type ErrorCompiling

type ErrorCompiling string

ErrorCompiling is the error that is returned if compilation fails

func (ErrorCompiling) Error

func (e ErrorCompiling) Error() string

type ErrorImportingDependencies

type ErrorImportingDependencies string

ErrorImportingDependencies is the error that is returned if dependency import fails

func (ErrorImportingDependencies) Error

type ErrorMissingTarget

type ErrorMissingTarget struct{}

ErrorMissingTarget is the error that is returned, if the target is <nil>

func (ErrorMissingTarget) Error

func (e ErrorMissingTarget) Error() string

type ErrorParsing

type ErrorParsing struct {
	FileName, Message string
}

ErrorParsing is the error that is returned, if there is an error when parsing file FileName

func (ErrorParsing) Error

func (e ErrorParsing) Error() string

type Options

type Options struct {
	GOROOT string // defaults to build.Default.GOROOT
	GOPATH string // defaults to build.Default.GOPATH
	Minify bool   // should the js be minified
}

Options is the subset of build.Options, that is exposed to the user of gopherjslib and is optional.

Jump to

Keyboard shortcuts

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