PostCSS Assets is an asset manager for CSS. It isolates stylesheets from environmental changes, gets image sizes and inlines files.
- Installation
- Usage
- URL resolution
- Cachebuster
- Image dimensions
- Inlining files
- Full list of options
- Full list of modifiers
npm install postcss-assets --save-dev
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/'));
});
var assets = require('postcss-assets');
grunt.initConfig({
postcss: {
options: {
processors: [
assets({
loadPaths: ['images/']
})
]
},
dist: { src: 'build/*.css' }
},
});
These options isolate stylesheets from environmental changes.
To make PostCSS Assets search for files in specific directories, define load paths:
var options = {
loadPaths: ['fonts/', 'media/patterns/', 'images/']
};
Example:
body {
background: url('foobar.jpg');
background: url('icons/baz.png');
}
PostCSS Assets would look for the files 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');
}
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.
If the URL of your base path is not /
, correct it:
var options = {
baseUrl: 'http://example.com/wp-content/themes/'
};
To make resolved paths relative, define a directory to relate to:
var options = {
relativeTo: 'assets/css'
};
PostCSS Assets can bust assets cache, changing urls depending on asset’s modification date:
var options = {
cachebuster: true
};
body {
background: url('/images/icons/baz.png?14a931c501f');
}
To define a custom cachebuster pass a function as an option:
var options = {
cachebuster: function (path) {
return fs.statSync(path).mtime.getTime().toString(16);
}
};
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 */
}
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.
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. | [] |
relativeTo |
Directory to relate to when resolving URLs. When false , disables relative URLs. |
false |