Asset Management Package for web app in Go language. Inspired by Rails Asset Pipeline.
Use Train to manage your asset's dependencies. Enables you to write javascript or stylesheet in the following way:
assets/javascripts/base.js
appName = "...";
assets/javascripts/app.js
//= require javascripts/base
$(function(){
// Do something cool
})
GET /assets/javascripts/app.js
appName = "...";
$(function(){
// Do something cool
})
assets/stylesheets/base.css
h1, h2{ padding:0; }
assets/stylesheets/app.css
/*
*= require stylesheets/base
*/
body{...}
GET /assets/stylesheets/app.css
h1, h2{ padding:0; }
body{...}
assets/javascripts/app.coffee
alert "Hello CoffeeScript!"
GET /assets/javascripts/app.js
alert("Hello CoffeeScript!");
assets/stylesheets/app.sass
body
color: red
GET /assets/stylesheets/app.js
body{
color: red; }
package main
import (
"fmt"
"github.com/shaoshing/train"
"net/http"
)
func main() {
http.Handle(train.Config.AssetsUrl, http.HandlerFunc(train.Handler))
fmt.Println("Listening to localhost:8000")
http.ListenAndServe(":8000", nil)
}
import "github.com/shaoshing/train"
func main() {
tmpl := template.New("index")
tmpl.Funcs(train.HelperFuncs)
tmpl.Parse(`
{{define "index"}}
{{javascript_tag "app"}}
{{stylesheet_tag "app"}}
{{end}}
`)
tmpl.Execute(os.Stdout, "index", nil)
//
// <script src="/assets/javascripts/base.js?12345"></script>
// <script src="/assets/javascripts/app.js?12345"></script>
//
// <link rel="stylesheet" href="/assets/stylesheets/base.css?12345">
// <link rel="stylesheet" href="/assets/stylesheets/app.css?12345">
}
Install the command line tool to bundle and compress assets automatically:
go build -o $GOPATH/bin/train github.com/shaoshing/train/cmd
train
-> clean bundled assets
-> copy assets from assets
-> bundle assets with require directive
-> compress assets
ls public/assets
The train tool will bundle your assets into the public/assets folder, with all files expaneded and compressed (by YUI compressor). You can then use any web servers (nginx, apache, or the Go's file server) to serve these static files. The template helpers will also stop expanding files if it found the public assets folder. That is, the following code:
{{javascript_tag "app"}}
{{stylesheet_tag "app"}}
Will become:
<script src="/assets/javascripts/app.js?12345"></script>
<link rel="stylesheet" href="/assets/stylesheets/app.css?12345">
Train is released under the MIT License.