Skip to content
Asset pipeline/precompiler for Golang
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information. Added MIT license Nov 28, 2018 Added readme Nov 28, 2018
config.go Initial commit Nov 26, 2018
middleware.go Added support for cache control as a gin-gonic middleware Nov 27, 2018


This is a web asset compiler written in Golang to be used in a web asset pipeline.


Coming from Rails, I was used to having all my Javascript and CSS assets precompiled as well as uniquely identified for cache-friendliness and lowering the number of requests required to view a website.

The only Golang-based solutions I was able to find were either purpose-built for a specific use case or simply didn't have the features I needed.

How is this different?

You provide a list of files that you would like to be compiled into one file, in the order you'd like them compiled in, and the library will combine, optionally minify them using, and generate an output file with the sha256 signature of the file in the filename. Your application can consume the generated byte buffers and handle them as you please, or you can have the library write the files out to a specified directory.

How do I use it?

The simplest use case is to generate assets offline for use in a Golang web application. For example:

package main

import precompiler ""

func main() {
		Files: []string{
		Minify:    true,
		OutputDir: "assets/",

Currently only Javascript (.js) and CSS (.css) files are supported.

Config key Use
Files An array of strings representing paths to files you want compiled
Minify Bool, optionally minify the files with
OutputDir String, the directory you'd like to write the compiled files to (with trailing slash)

Note that if an output dir is specified, "css" and "js" subdirectories will be used/created to hold the resulting files. Leaving it blank will cause it to not write any files to disk.

Compile() returns map[FileType]*CompileResult, error where FileType is one of the supported types, e.g. precompiler.CSS, precompiler.JS and CompileResult is

type CompileResult struct {
	Bytes      []byte
	Hash       string
	OutputPath string

for use with handling the assets yourself if you don't want the library writing them to a file.

Once the compiled files have been generated, you would set your HTML file/template to use them with e.g.:

<link rel="stylesheet" href="/assets/css/app-55d3344ad20eb62608617d8c6c80ed662647a8d11b600a522f7cfe85c1e3ed58.min.css">
<script src="/assets/js/app-9db393ed26ecff7f3c64a37df9168c09036d1f1d1bf380a74f442106e4101629.min.js"></script>

Or create a template function to retrieve the correct filenames so you don't have to update your template every time you re-generate.

Gin-gonic middleware

There is a middleware available to automatically set a Cache-Control header for infinite lifetime of these assets. You can use it like:

r := gin.New()
r.Static("/assets", "assets")

Where "/assets" is the base path (with leading slash) for all assets on your website. Note that every request starting with this path will have very long cache headers set, not just CSS/JS files.

Other integrations

If used as an offline generation tool, the resulting files can easily be used with a bindata/packr-style bundling system so that the app can be deployed as a single binary.


Contributions are quite welcome. This is a fairly simple helper library at the moment, but I'd be happy to implement any customizations for it to work for other peoples' apps than just my own.

You can’t perform that action at this time.