Categorygithub.com/wolfeidau/echo-esbuild-middleware
modulepackage
1.1.3
Repository: https://github.com/wolfeidau/echo-esbuild-middleware.git
Documentation: pkg.go.dev

# README

echo-esbuild-middleware

This module provides an echo middleware which automatically bundles assets using esbuild.

GitHub Actions status Go Report Card Documentation

Why?

I am currently using to enable me to add typescript based stimulus controllers to a website while keeping the bundling process simple and integrated into the development lifecycle of the service so I can "watch" and rebuild everything.

Given the single NPM module provided by esbuild my node_modules folder is also lean, which means less security issues.

Usage

The following example uses the middleware to provide bundle.js via a script tag like <script src="/bundle.js"></script>.

	e := echo.New()

    // register the asset bundler which will build then serve any asset files
    e.Use(assets.BundlerWithConfig(assets.BundlerConfig{
		EntryPoints:     []string{"testassets/src/index.ts"},
		Outfile:         "bundle.js",
		InlineSourcemap: true,
		Define: map[string]string{
			"process.env.NODE_ENV": `"production"`,
		},
		OnBuild: func(result api.BuildResult, timeTaken time.Duration) {
			if len(result.Errors) > 0 {
				log.Fatal().Fields(map[string]interface{}{
					"errors": result.Errors,
				}).Msg("failed to build assets")
			}
		},
        OnRequest: func(req *http.Request, contentLength, code int, timeTaken time.Duration) {
            log.Info().Str("path", req.URL.Path).Int("code", code).Str("timeTaken", timeTaken.String()).Msg("asset served")
        },
	}))

The next example builds files and stores them locally in an assets directory, this is typically used to bundle one ore more entry points prior to startup of the application.


	// register the asset bundler which will build then serve any asset files
	err := BuildWithConfig(BuildConfig{
		EntryPoints: []api.EntryPoint{
			{
				InputPath:  "testassets/src/index.ts",
				OutputPath: "bundle",
			},
		},
		InlineSourcemap: true,
		Define: map[string]string{
			"process.env.NODE_ENV": `"production"`,
		},
		OnBuild: func(result api.BuildResult, timeTaken time.Duration) {
			if len(result.Errors) > 0 {
				log.Fatal().Fields(map[string]interface{}{
					"errors": result.Errors,
				}).Msg("failed to build assets")
			}
		},
		Outdir: "public/js",
	})
    if err != nil {
        ...
    }

License

This code was authored by Mark Wolfe and licensed under the Apache 2.0 license.

# Functions

BuildWithConfig this function will trigger a build using esbuild using similar simplified params to the middleware, however the output will be written to a local directory which is typically a public assets folder.
BundlerWithConfig provide bundle files which are built on startup and stored in memory in the middleware.

# Structs

No description provided by the author
BundlerConfig asset bundler configuration which provides the bare minimum to keep things simple.

# Type aliases

FuncBuildResult callback used to return result when bundling.
FuncRequest callback for each request made which returns an asset.