concatjs

package
v0.0.0-...-c18bc5f Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package concatjs provides a simple way of serving JavaScript sources in development.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeConcatenatedJS

func ServeConcatenatedJS(manifestPath string, root string, preScripts []string, postScripts []string, fs FileSystem) http.Handler

ServeConcatenatedJS returns an http.Handler that serves the JavaScript files listed in manifestPath in one concatenated, eval separated response body.

This greatly speeds up development load times due to fewer HTTP requests, but still for easy debugging by giving the eval'ed fragments URLs through sourceURL comments.

Example usage:

  http.Handle("/app_combined.js",
	     concatjs.ServeConcatenatedJS("my/app/web_srcs.MF", ".", [], [], nil))

Relative paths in the manifest are resolved relative to the path given as root.

Types

type FileCache

type FileCache struct {
	// contains filtered or unexported fields
}

FileCache caches a set of files in memory and provides a single method, WriteFiles(), that streams them out in the concatjs format.

func NewFileCache

func NewFileCache(root string, fs FileSystem) *FileCache

NewFileCache constructs a new FileCache. Relative paths in the cache are resolved relative to root. fs injects file system access, and will use the real file system if nil.

func (*FileCache) WriteFiles

func (cache *FileCache) WriteFiles(w io.Writer, files []string) error

WriteFiles updates the cache for a list of files, then streams them into an io.Writer.

type FileSystem

type FileSystem interface {
	StatMtime(filename string) (time.Time, error)
	ReadFile(filename string) ([]byte, error)
	ResolvePath(root string, file string) (string, error)
}

FileSystem is the interface to reading files from disk. It's abstracted into an interface to allow tests to replace it.

type RealFileSystem

type RealFileSystem struct{}

RealFileSystem implements FileSystem by actual disk access.

func (*RealFileSystem) ReadFile

func (fs *RealFileSystem) ReadFile(filename string) ([]byte, error)

ReadFile reads the specified file using the real filesystem.

func (*RealFileSystem) ResolvePath

func (fs *RealFileSystem) ResolvePath(root string, file string) (string, error)

ResolvePath resolves the specified path within a given root by joining root and the filepath. This is only works if the specified file is located within the given root in the real filesystem. This does not work in Bazel where requested files aren't always located within the specified root. Files would need to be resolved as runfiles.

func (*RealFileSystem) StatMtime

func (fs *RealFileSystem) StatMtime(filename string) (time.Time, error)

StatMtime gets the last modification time of the specified file.

Jump to

Keyboard shortcuts

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