pipeline

package module
v0.0.0-...-a10173d Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2016 License: MIT Imports: 16 Imported by: 0

README

beego-pipeline

Asset compilation and compression for golang beego framework inspired by django-pipeline.

Requirements
  • NodeJS
  • Less - Globally installed sudo npm -g install lessc
  • Yuglify - Globally installed sudo npm -g install yuglify

Configuration

The configuration is stored in a pipeline.json file located in the conf/ folder in your beego application. Example usage:

{
  "css": {
    "base": {
      "root": "static/",
      "sources": [
        "css/base.css",
        "css/mixins.css"
      ],
      "output": "dist/app.css"
    },
    "output": {
      "sources": [
        "css/base.css",
        "css/less1.less",
        "css/less2.less"
      ],
      "output": "dist/output.css"
    }
  },
  "js": {}
}

We can specify the following keys: css, js. Each mapping inside represents the output of compiled/compressed/versioned file.

The root key can be omited and will default to "static/". This is the location where your assets reside if they are different from the default.

Sources configuration supports wild card asset discovery. e.g: css/*.css

IMPORTANT: Watch out so that any source file doesn't have the same path/name as the output file. The app will complain if you mistakenly override a source file with an output file.

Configuration in the code

You must include the pipeline and any compilers/compressor in the code. At the moment there is only yuglify support for compressor cause this supports both css and js.

In main.go import pipeline and any compiler/compressor that you require:


package main

import (
  ...
  "github.com/astaxie/beego"
  _ "github.com/bogh/beego-pipeline"
  _ "github.com/bogh/beego-pipeline/compilers/less"
  _ "github.com/bogh/beego-pipeline/compressors/yuglify"
)

func main() {
  beego.Run()
}

Override command parameters

WIP

Usage

Pipeline will automatically register to template functions pipeline_css and pipeline_js. Using this you can load the groups by name in your templates. E.g.:

<!doctype html>
<html>
<head>
  <title>Beego pipeline example</title>
  {{ pipeline_css "app" }}
</head>
<body>
  ...

  {{ pipeline_js "app" }}
</body>
</html>

If you run in dev mode then the assets won't be compressed and they will be added idividually. Assets that don't need compilation (e.g.: basic .css files) and any file that has no matching compiler.

<link href="/static/css/base.css">
<link href="/static/css/mixins.css">

In case of a compiled file. If the extension is .less a file with the same name but correct asset extension will be generated. e.g. app.less => app.css. In the same folder.

If you run in any other mode the assets will be compressed into the output setting of the group and the file named will be appended with a hash generated from the file contents. e.g.: css/app.css => app.9fa8429f0f816065.css. The output of the helpers would be:

<link href="/static/app.9fa8429f0f816065.css">
Auto generation

Pipeline will automatically watch the source files for changes and regenerate that group in dev mode.

TODO

  • Sass compiler
  • TypeScript compiler

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCompiler = errors.New("No compiler found")

	ErrAssetNotFound = errors.New("No asset found.")
)
View Source
var (
	DefaultCompiler = new(NopCompiler)
)

Functions

func PipelineCss

func PipelineCss(name string) (template.HTML, error)

func PipelineJs

func PipelineJs(name string) (template.HTML, error)

func RegisterCompiler

func RegisterCompiler(c Compiler)

func RegisterCompressor

func RegisterCompressor(c Compressor)

Types

type Asset

type Asset string

Used for constants to define the asset type set

const (
	AssetCss Asset = "css"
	AssetJs        = "js"
)

type AutoCloseReader

type AutoCloseReader struct {
	io.ReadCloser
}

Reader that closes it self when reaches EOF

func (*AutoCloseReader) Read

func (a *AutoCloseReader) Read(p []byte) (n int, err error)

type Collection

type Collection map[string]*Group

A map of asset groups by name

type Collections

type Collections map[Asset]Collection

type Compiler

type Compiler interface {
	Match(Asset, string) bool

	// Return true if the asset must be compiled in order to work
	RequireCompile() bool

	Compile(string) (io.Reader, error)
}

type Compressor

type Compressor interface {
	Match(Asset) bool
	// Should compress and concatenate the file in paths and save them in the
	// output
	Compress(io.Reader) (io.Reader, error)
}

Define compressor interface

type Config

type Config struct {
	Collections
	Watcher *fsnotify.Watcher
}

Hold a map of asset types each containing a collection

func (*Config) GetAssetGroup

func (c *Config) GetAssetGroup(asset Asset, name string) (*Group, error)

func (*Config) GetAssetTpl

func (c *Config) GetAssetTpl(asset Asset) string

type ErrOverridingPath

type ErrOverridingPath struct {
	Path string
}

func (*ErrOverridingPath) Error

func (e *ErrOverridingPath) Error() string

type Executor

type Executor struct {
	CmdPath string
	CmdArgs string
}

func NewExecutor

func NewExecutor(path, args string) *Executor

func (*Executor) BuildCmd

func (e *Executor) BuildCmd(extraArgs ...string) *exec.Cmd

func (*Executor) Pipe

func (e *Executor) Pipe(cmd *exec.Cmd, r io.Reader) (io.Reader, error)

type Group

type Group struct {
	// Location inside the AppPath directory
	// specify this in case the root of static folder is not the default "/static"
	Root    string `json:",omitempty"`
	Sources []string

	Output string
	// contains filtered or unexported fields
}

Keep configuration for an asset output

func (*Group) AbsPath

func (g *Group) AbsPath(path string) string

Return absolute path for provided path, prepending AppPath and Root

func (*Group) NormAsset

func (g *Group) NormAsset(path string, asset Asset) string

Returns a path for a source that will change the extension to match the asset If the path is the same error is returned. File shouldn't be overriden

func (*Group) NormAssets

func (g *Group) NormAssets(paths []string, asset Asset) ([]string, error)

func (*Group) OutputPath

func (g *Group) OutputPath() string

Normalized Output

func (*Group) ResultPaths

func (g *Group) ResultPaths(asset Asset) ([]string, error)

Determine the Result path and return the value TODO: This method will calculate the version hash

func (*Group) RootedPath

func (g *Group) RootedPath(paths ...string) string

func (*Group) SourcePaths

func (g *Group) SourcePaths() ([]string, error)

func (*Group) VersionedPath

func (g *Group) VersionedPath() string

type NopCompiler

type NopCompiler struct{}

func (*NopCompiler) Compile

func (n *NopCompiler) Compile(filepath string) (io.Reader, error)

func (*NopCompiler) Match

func (n *NopCompiler) Match(asset Asset, filepath string) bool

func (*NopCompiler) RequireCompile

func (n *NopCompiler) RequireCompile() bool

func (*NopCompiler) String

func (n *NopCompiler) String() string

type Processor

type Processor struct {
	Asset      Asset
	Collection Collection
}

func NewProcessor

func NewProcessor(asset Asset, collection Collection) *Processor

func (*Processor) Compile

func (p *Processor) Compile(group *Group, write bool) (io.Reader, error)

If `write` is true then save each file to it's own path

func (*Processor) Compress

func (p *Processor) Compress(in io.Reader) (io.Reader, error)

Accepts an io.Writer and returns an io.Reader

func (*Processor) GetAsset

func (p *Processor) GetAsset() string

func (*Processor) GetCompiler

func (p *Processor) GetCompiler(path string) Compiler

func (*Processor) GetCompressor

func (p *Processor) GetCompressor() Compressor

func (*Processor) Process

func (p *Processor) Process() error

func (*Processor) ProcessGroup

func (p *Processor) ProcessGroup(group *Group) error

func (*Processor) Watch

func (p *Processor) Watch() error

Watch files in this group for changes and recompile

func (*Processor) Write

func (p *Processor) Write(path string, r io.Reader) error

Write data from reader to the group file

Directories

Path Synopsis
compilers

Jump to

Keyboard shortcuts

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