Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tscomp

A Go Standard Library HTTP middleware for hot-compiling Typescript files.

Why

I like Typescript, but I don't like remembering to compile Typescript.

This middleware allows for seamless hot-compilation and static loading of compiled files with no code changes required.

Prerequiste

You must have the Typescript compiler (tsc) installed on your system.

Usage

The simplest way to make use of tscomp is to use the Intercept function

m := http.NewServeMux()
m.Handle("GET /static/", tscomp.Intercept("./static"))

This will automatically set up the compiler will default settings.

Equalivant to:

interceptor := tscomp.Handler{
   Cache: make(map[string]time.Time),
   Dir:   "./static",
   Next:  http.FileServer(http.Dir("./static")),
}
m.Handle("GET /static/", interceptor)

In your HTML file, this allows for .ts files to be "loaded":

<html>
<body>
   <script src="/static/ts/main.ts" type="text/javascript"></script>
</body>
</html>

Handler Chaining

Since the interceptor is an http.Handler, it can work with other handler chains.

m.Handle("GET /static/", http.StripPrefix("/static", tscomp.Intercept("./web")))

Intercept Options

If you want to customize how the tscomp.Handler functions, you can either create the Handler manually (the only required structure field is Handler.Dir); or, you can provide tscomp.Options to the Intercept function.

Disables the internal cache (compile every time):

m.Handle("GET /static/", tscomp.Intercept("./static", tscomp.WithNoCache()))

Override the Next handler in the chain:

func logger(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slog.Info("Got Request!", "method", r.Method, "path", r.URL.Path)

		next.ServeHTTP(w, r)
	})
}
m.Handle("GET /static/", tscomp.Intercept("./static", tscomp.WithNext(logger(http.Dir("./static")))))

Disabling Compilation

One usage pattern is allowing hot-compilation during development and normal filesystem loading once released. tscomp allows for both with no code changes.

When creating the tscomp.Handler, you can specify the NoCompile option to skip compilation all together.

The intended usage is something along the lines:

IsRelease := os.GetEnv("RELEASE") != ""

m.Handle("GET /static/", tscomp.Intercept("./static", tscomp.WithNoCompile(IsRelease)))

// or

interceptor := tscomp.Handler{
   Cache: make(map[string]time.Time),
   Dir:   "./static",
   Next:  http.FileServer(http.Dir("./static")),
   NoCompile: IsRelease,
}
m.Handle("GET /static/", interceptor)

If NoCompile is specified, then all the tscomp.Handler does is change the incoming file request from .ts to .js, then moves to the next handler in the chain.

About

A Go Standard Library HTTP middleware for hot-compiling Typescript files

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages