A fast, in-memory, easy-to-configure asset pipeline, for
HuffPostData stories.
Takes a JSON configuration as input. Outputs an AssetBucket. (Read more on both
below.)
The idea is to compile all your assets (images, stylesheets, scripts) within
milliseconds. This tasteful feature list:
- SCSS
compilation.
- Simple JavaScript bundling:
require()
-ed JavaScript is included inline. - UglifyJS2 compression. (This can be
slow; use
UGLIFY=false
or UGLIFY=true
environment variables to control
it.) - Filename globbing lets you specify
filenames by pattern.
- md5sum-digested URLs: they let you cache forever and deploy at will, without
worrying about race conditions.
- URL helpers: given a key, AssetBucket will return the URL (including hostname)
or "href" (URL minus hostname).
Usage
'use strict'
const AssetPipeline = require('hpd-asset-pipeline')
const configuration = {
host: 'https://assets.example.org',
baseHref: '/my-project',
basePath: `${__dirname}/assets`,
assets: [
{
logic: 'digest',
glob: 'images/**/*.{png,jpg,gif,svg,ico}'
},
{
logic: 'digest',
glob: 'javascripts/{social,stats}.js'
},
{
logic: 'scss',
glob: 'stylesheets/index.scss'
},
{
logic: 'javascript',
glob: 'javascrippts/app.js'
},
{
logic: 'raw',
glob: 'javascripts/pym.min.js'
}
]
}
AssetPipeline.render(configuration, (err, bucket) => {
if (err) throw err
const website = bucket.toWebsite()
bucket.hrefTo('javascripts/social.js')
bucket.urlTo('javascripts/social.js')
bucket.dataUriFor('images/logo.png')
bucket.dataFor('images/logo.png')
}
SCSS helper functions
Our stylesheets are Sass.
We have a couple of helper functions:
asset-url(key)
: creates a url()
value pointing to the specified asset.
Example: background-image: asset-url('images/header.jpg')
will produce
background-image: url(/images/header-0f0f0f0f0f.jpg)
asset-data-url(key)
: creates a url(data:[mime];base64,[data])
value
containing all the bytes of the specified asset, with the asset's MIME type.
Example: background-image: asset-url('images/highlight.png')
will produce
background-image: url('data:image/png;base64,XXXXXXXXXX...')
asset-url()
forces an extra HTTP request each time a browser loads the
stylesheet. Use it for large assets.
asset-data-url()
makes the stylesheet larger, since it includes the file
contents. The page won't render until the entire stylesheet has transferred. Use
it for assets under a few kilobytes in size.
Error handling
Compilation failures (such as missing require()
or invalid SCSS) will halt
all compilation and return an Error.
hrefTo()
, urlTo()
, dataUriFor()
and dataFor()
will throw Errors
when the assets they refer to do not exist.
Logic implementation
These aren't "plugins" (yet). Each "logic" is an Object which a Function member
named sync
or async
. (Prefer sync.)
The sync()
method accepts two arguments: bucket
(an AssetBucket, with
.baseHref
and .baseUrl
properties, plus .hrefTo()
et al for the
.assets
which were compiled in previous steps); and paths
(an Array of
String paths, from glob()
). It may throw an Error. Otherwise, it will return
an Array of Asset
objects as output.
The async()
method accepts a third argument, callback
; in case of error,
it calls callback(new Error(...))
.
License
Copyright (c) 2017 The Huffington Post, released under the MIT license.
See LICENSE.