Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-assets

assetsjs128.1kMIT6.0.0

PostCSS plugin to manage assets

assets, base64, css, image, path, postcss, postcss-plugin, size, url

readme

postcss-assets

PostCSS Assets is an asset manager for CSS. It isolates stylesheets from environmental changes, gets image sizes and inlines files.

Unix Build Status Windows Build Status Coverage

Table of contents

Installation

npm install postcss-assets --save-dev

Usage

Gulp PostCSS

gulp.task('assets', function () {
  var postcss = require('gulp-postcss');
  var assets  = require('postcss-assets');

  return gulp.src('source/*.css')
    .pipe(postcss([assets({
      loadPaths: ['images/']
    })]))
    .pipe(gulp.dest('build/'));
});

Grunt PostCSS

var assets  = require('postcss-assets');

grunt.initConfig({
  postcss: {
    options: {
      processors: [
        assets({
          loadPaths: ['images/']
        })
      ]
    },
    dist: { src: 'build/*.css' }
  },
});

Note: all of the listed options below are parameters for the assets object, not the top level postcss options object.

URL resolution

These options isolate stylesheets from environmental changes.

Load paths

To make PostCSS Assets search for files in specific directories, define load paths:

var options = {
  loadPaths: ['fonts/', 'media/patterns/', 'images/']
};

Example:

body {
  background: resolve('foobar.jpg');
  background: resolve('icons/baz.png');
}

PostCSS Assets would look for the files relative to the source file, then in load paths, then in the base path. If it succeed, it would resolve a true URL:

body {
  background: url('/media/patterns/foobar.jpg');
  background: url('/images/icons/baz.png');
}

Base path

If the root directory of your site is not where you execute PostCSS Assets, correct it:

var options = {
  basePath: 'source/'
};

PostCSS Assets would treat source directory as / for all URLs and load paths would be relative to it.

Base URL

If the URL of your base path is not /, correct it:

var options = {
  baseUrl: 'http://example.com/wp-content/themes/'
};

Relative paths

To make resolved paths relative to the input file, set a flag:

var options = {
  relative: true
};

To relate to a particular directory, set it as a string:

var options = {
  relative: 'assets/css'
};

Cachebuster

PostCSS Assets can bust assets cache:

var options = {
  cachebuster: true
};

Example:

body {
  background: resolve('/images/icons/baz.png');
}

PostCSS Assets will change urls depending on asset’s modification date:

body {
  background: url('/images/icons/baz.png?14a931c501f');
}

To define a custom cachebuster pass a function as an option:

var options = {
  cachebuster: function (filePath, urlPathname) {
    return fs.statSync(filePath).mtime.getTime().toString(16);
  }
};

If the returned value is falsy, no cache busting is done for the asset.

If the returned value is an object the values of pathname and/or query are used to generate a cache busted path to the asset.

If the returned value is a string, it is added as a query string.

The returned values for query strings must not include the starting ?.

Busting the cache via path:

var options = {
  cachebuster: function (filePath, urlPathname) {
    var hash = fs.statSync(filePath).mtime.getTime().toString(16);
    return {
      pathname: path.dirname(urlPathname)
        + '/' + path.basename(urlPathname, path.extname(urlPathname))
        + hash + path.extname(urlPathname),
      query: false // you may omit this one
    }
  }
};

Image dimensions

PostCSS Assets calculates dimensions of PNG, JPEG, GIF, SVG and WebP images:

body {
  width: width('images/foobar.png'); /* 320px */
  height: height('images/foobar.png'); /* 240px */
  background-size: size('images/foobar.png'); /* 320px 240px */
}

To correct the dimensions for images with a high density, pass it as a second parameter:

body {
  width: width('images/foobar.png', 2); /* 160px */
  height: height('images/foobar.png', 2); /* 120px */
  background-size: size('images/foobar.png', 2); /* 160px 120px */
}

Inlining files

PostCSS inlines files to a stylesheet in Base64 encoding:

body {
  background: inline('images/foobar.png');
}

SVG files would be inlined unencoded, because then they benefit in size.

Full list of options

Option Description Default
basePath Root directory of the project. .
baseUrl URL of the project when running the web server. /
cachebuster If cache should be busted. Pass a function to define custom busting strategy. false
loadPaths Specific directories to look for the files. []
relative Directory to relate to when resolving URLs. When true, relates to the input file. When false, disables relative URLs. false
cache When true, if the input file not been modifed, use the results before cached. false

changelog

Change log

6.0.0

Breaking

Under the hook

5.0.0

Breaking

4.2.0

Features:

4.1.0

Features:

  • relative option supports what relativeTo option did in 3.x: if a string is passed, paths are generated relatively to a path in that string. A behavior of relating to input files by passing true is kept untouched.

Under the hood:

4.0.1

Bugfixes

4.0.0

Breaking

  • Removes relativeTo option, introduces relative one.

    There is no need to specify a particular file to relate to anymore - set relative to true and Assets would resolve URLs relatively to the current CSS file.

    This solves the issue when you have stylesheets in different folders and want relative paths — previously there was an option to relate only to a single directory.

    (https://github.com/assetsjs/postcss-assets/issues/42)

Features

Bugfixes

Under the hood

  • Coverage hits 100%.
  • Automated tests against the latest stable nodejs, v0.12 and v4.
  • Builds are automatically tested on Windows by AppVeyor.
  • Switches tests from Mocha to AVA.
  • Uses Calipers for image measurement instead of image-size.
  • Replaces custom function mapper with postcss-functions.
  • Extracts assets processing logic to the Assets module.
  • Cleans up dependencies.

3.0.3

Bugfixes

3.0.2

Bugfixes

3.0.1

Bugfixes

Under the hood

  • Uses ESLint instead of JSHint/JSCS.

3.0.0

API updates

2.1.4

Since private image-size fork was removed, releases 2.1.0—3.0.2 has stopped working. While 3.0.3 release fixes this for the 3.0.x versions, this release is fixing the same for the 2.1.x.

Bugfixes

2.1.3

Bugfixes

2.1.2

Under the hood

2.1.1

Bugfixes

Under the hood

  • covers 99% of the code;
  • explains code with comments.

2.1.0

API updates

Bugfixes

Under the hood

  • uses Gulp for development routines;
  • validates code style with JSHint and JSCS.

2.0.0

API updates

Under the hood

1.1.4

Allows to use common PostCSS plugin API (https://github.com/borodean/postcss-assets/issues/6) Fixes quotes when inlining SVG (https://github.com/borodean/postcss-assets/pull/14)

1.1.3

Base64-encodes with Buffer.

1.1.2

Cachebuster recognizes resolved paths.

1.1.1

Uses PostCSS 4.0.

1.1.0

Introduces cachebuster.

1.0.0

  • width, height and size functions are introduced to measure image dimesions with high density pixels support;
  • inline function introduced to inline files;
  • url function automagic is removed;
  • inline.maxSize option is removed;
  • all modifiers are removed.

0.9.1

Inlines SVG as UTF-8, not Base64.

0.9.0

Hello, world.