assets

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildWithConfig added in v1.1.3

func BuildWithConfig(cfg BuildConfig) error

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.

func BundlerWithConfig

func BundlerWithConfig(cfg BundlerConfig) echo.MiddlewareFunc

BundlerWithConfig provide bundle files which are built on startup and stored in memory in the middleware

Types

type BuildConfig added in v1.1.3

type BuildConfig struct {
	EntryPoints     []api.EntryPoint
	Outdir          string
	InlineSourcemap bool
	Define          map[string]string
	Format          api.Format
	// This will be invoked for a build and can be used to check errors/warnings
	OnBuild FuncBuildResult
}

type BundlerConfig

type BundlerConfig struct {
	EntryPoints     []string
	Outfile         string
	InlineSourcemap bool
	Define          map[string]string
	Format          api.Format
	// This will be invoked for a build and can be used to check errors/warnings
	OnBuild   FuncBuildResult
	OnRequest FuncRequest
}

BundlerConfig asset bundler configuration which provides the bare minimum to keep things simple

type FuncBuildResult

type FuncBuildResult func(result api.BuildResult, timeTaken time.Duration)

FuncBuildResult callback used to return result when bundling

type FuncRequest

type FuncRequest func(req *http.Request, contentLength, code int, timeTaken time.Duration)

FuncRequest callback for each request made which returns an asset

Jump to

Keyboard shortcuts

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