PostCSS Url Resolver
PostCSS plugin that resolves urls (CSS imports and images) via http requests.
This plugin is a combination of postcss-import-url
, postcss-url-mapper
and postcss-base64
. It borrows code from all of them and adds some extra features.
Features:
- Recursively resolves
@import
andurl(...)
of remote files. - Optionally inlines images in base64.
- Isomorphic. Works in Node and the browser.
- HTTP client agnostic. It must be provided as a parameter. This also allows providing custom cache or headers.
Requirements:
- Native
Promise
or a polyfill.
Examples:
/* http://some.remote/file.css */ ;
/* Input example */ ;
/* Output example */
If options.base64
was specified (true
), the background-image
would look like:
Usage
This plugin is isomorphic (Node + browser environments). For sake of versatility, it does not bundle any http-request package. Therefore, it must be provided as a parameter.
Node
var postcss = ;var urlResolver = ;var hh = ;
Browser (Webpack)
;;;
Note: axios
can also be used in Node environments.
Parameters
request
: Function called to make an HTTP request. It gets a parsed URL object as its only parameter. Must return a promise which resolves to the response body (content). Required.recursive
: Whether@import
should be resolved recursively. Defaulttrue
.base64
: Resolves and inlines images in base 64. Defaultfalse
.exclude
: A RegExp matching urls that won't be resolved. Defaultnull
.
See PostCSS docs for examples for your environment.