Skip to content

Instantly share code, notes, and snippets.

@rusco
Created November 16, 2023 09:54
Show Gist options
  • Save rusco/ae10662ce41acdd9f003f34cd64b0a51 to your computer and use it in GitHub Desktop.
Save rusco/ae10662ce41acdd9f003f34cd64b0a51 to your computer and use it in GitHub Desktop.
convert typescript to javascript with esbuild
// date: 16.11.2023
// file: convert typescript to javascript with esbuild
package main
import (
"fmt"
"os"
"github.com/evanw/esbuild/pkg/api"
)
// build once
func main_build() {
result := api.Build(api.BuildOptions{
//Outfile: my.js
EntryPoints: []string{"my.ts"},
Outdir: "dist",
Bundle: true,
Write: true,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
Sourcemap: api.SourceMapLinked,
})
if len(result.Errors) != 0 {
print(result.Errors)
os.Exit(1)
}
}
// watch
func main() {
ctx, err := api.Context(api.BuildOptions{
//Outfile: my.js
EntryPoints: []string{"my.ts"},
Outdir: "dist",
Bundle: true,
Write: true,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
Sourcemap: api.SourceMapLinked,
})
if err != nil {
os.Exit(1)
}
err2 := ctx.Watch(api.WatchOptions{})
if err2 != nil {
os.Exit(1)
}
fmt.Printf("watching...\n")
<-make(chan struct{})
}
/* go.mod
module esbuildrunner
go 1.21.4
require github.com/evanw/esbuild v0.19.5
require golang.org/x/sys v0.14.0 // indirect
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment