assets

package
v0.0.0-...-598a1f7 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: AGPL-3.0, MIT Imports: 14 Imported by: 0

README

Assets

Assets provides asset compilation, concatenation and fingerprinting. Asset details are stored in a file at secrets/assets.json by default.

Usage

Use the assets package to organize assets however you like within your src folder, and output them in compressed form in your public/assets folder when you come to deploy your app.

  // Load asset details from json file on each run
  err := appAssets.Load()
  if err != nil {
    // If no assets loaded, compile for the first time (produces files in public/assets)
    err := appAssets.Compile("src", "public")
    if err != nil {
      server.Fatalf("#error compiling assets %s", err)
    }
  }

// Use the asset helpers to generate fingerprinted assets (either one fingerprinted file in production or a list of all files in development) - this is similar to the Rails asset pipeline.

  view.Helpers["style"] = appAssets.StyleLink
  view.Helpers["script"] = appAssets.ScriptLink

Documentation

Overview

Package assets provides asset compilation, concatenation and fingerprinting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScriptLink(name string) template.HTML

ScriptLink returns an html tag for a given file path

func StyleLink(name string) template.HTML

StyleLink returns an html tag for a given file path

Types

type Collection

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

Collection holds the complete list of groups

func New

func New(compiled bool) *Collection

New returns a new assets.Collection

func (*Collection) Compile

func (c *Collection) Compile(src string, dst string) error

Compile images, styles and scripts asset folders from src into dst (minifying and amalgamating)

func (*Collection) FetchOrCreateGroup

func (c *Collection) FetchOrCreateGroup(name string) *Group

FetchOrCreateGroup returns the named group if it exists, or creates it if not

func (*Collection) File

func (c *Collection) File(name string) *File

File returns the first asset file matching name - this assumes files have unique names between groups

func (*Collection) Group

func (c *Collection) Group(name string) *Group

Group returns the named group if it exists or an empty group if not

func (*Collection) Load

func (c *Collection) Load() error

Load the asset groups from the assets json file Call this on startup from your app to read the asset details after assets are compiled

func (*Collection) MarshalJSON

func (c *Collection) MarshalJSON() ([]byte, error)

MarshalJSON generates json for this collection, of the form {group:{file:hash}}

func (*Collection) Save

func (c *Collection) Save() error

Save the assets to a file after compilation

func (c *Collection) ScriptLink(names ...string) template.HTML

ScriptLink converts a set of group names to one script tag (production) or to a list of script tags (development)

func (c *Collection) StyleLink(names ...string) template.HTML

StyleLink converts a set of group names to one style link tag (production) or to a list of style link tags (development)

type File

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

File stores a filename and hash fingerprint for the asset file

func NewFile

func NewFile(p string) (*File, error)

NewFile returns a new file object

func (*File) AssetPath

func (f *File) AssetPath(dst string) string

AssetPath returns the path of this file within the assets folder

func (*File) Copy

func (f *File) Copy(dst string) error

Copy our bytes to dstpath

func (*File) LocalPath

func (f *File) LocalPath() string

LocalPath returns the relative path of this file

func (*File) MarshalJSON

func (f *File) MarshalJSON() ([]byte, error)

MarshalJSON generates json for this file, of the form {group:{file:hash}}

func (*File) Newer

func (f *File) Newer(dst string) bool

Newer returns true if file exists at path

func (*File) Script

func (f *File) Script() bool

Script returns true if this file is a js file

func (*File) String

func (f *File) String() string

String returns a string representation of this object

func (*File) Style

func (f *File) Style() bool

Style returns true if this file is a CSS file

type Group

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

Group holds a name and a list of files (images, scripts, styles)

func (*Group) AddAsset

func (g *Group) AddAsset(p, h string)

AddAsset adds this asset to the group

func (*Group) Compile

func (g *Group) Compile(dst string) error

Compile compiles all our files and calculates hashes from their contents The group hash is a hash of hashes

func (*Group) MarshalJSON

func (g *Group) MarshalJSON() ([]byte, error)

MarshalJSON generates json for this collection, of the form {group:{file:hash}}

func (*Group) ParseFile

func (g *Group) ParseFile(p string, dst string) error

ParseFile adds this asset to our list of files, along with a fingerprint based on the content

func (*Group) RemoveFiles

func (g *Group) RemoveFiles(dst string) error

RemoveFiles removes old compiled files for this group from dst

func (*Group) ScriptName

func (g *Group) ScriptName() string

ScriptName returns a fingerprinted group name for scripts

func (*Group) ScriptPath

func (g *Group) ScriptPath(dst string) string

ScriptPath returns a fingerprinted group path for scripts

func (*Group) Scripts

func (g *Group) Scripts() []*File

Scripts returns an array of file names for styles

func (*Group) String

func (g *Group) String() string

String returns a string represention of group

func (*Group) StyleName

func (g *Group) StyleName() string

StyleName returns a fingerprinted group name for styles

func (*Group) StylePath

func (g *Group) StylePath(dst string) string

StylePath returns a fingerprinted group path for styles

func (*Group) Styles

func (g *Group) Styles() []*File

Styles returns an array of file names for styles

Directories

Path Synopsis
internal
cssmin
Package cssmin minifies CSS.
Package cssmin minifies CSS.
jsmin
Package jsmin implements JavaScript minifier.
Package jsmin implements JavaScript minifier.

Jump to

Keyboard shortcuts

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