postcss-bgimage
Plugin for PoscCSS to remove or keep only images by url() to optimize page loading
postcss-bgimage
PostCSS plugin which removes
background-image
properties withurl()
values or leaves only its. It allows to separate your layouts CSS from the images CSS to boost a speed of showing a page.
💥 Note! The plugin only removes CSS declarations. Do not expect cleaning empty rules after that. Use special plugins for it (csso, cssnano and other).
Installation
$ npm install postcss-bgimage --save-dev
or
$ yarn add postcss-bgimage --dev
Usage
Any way of using PostCSS. For example, Gulp PostCSS:
const gulp = ;const postcss = ;const rename = ;const bgImage = ; { return gulp ;} { return gulp ;} exportscompile = gulp;
Result
Common usage
Input
/* style.css */
Output
/* style.top.css */
/* style.bottom.css */
background
Usage with shortcut Input
/* style.css */
Output
/* style.top.css */
/* style.bottom.css */
Using in nested at-rules
Input
/* style.css */ { @) } }
Output
/* style.top.css */ { @) } }
/* style.bottom.css */ { @) } }
Ignore rules
To ignore some CSS rules use comment /* bgImage: ignore */
. For example:
Input
/* style.css */ { /* bgImage: ignore */ @) } }
Output
/* style.top.css */ { /* bgImage: ignore */ @) } }
/* style.bottom.css */ { /* bgImage: ignore */ @) } }
Using of the resulting files in HTML
postcss-bgimage test postcss-bgimage test Page content
Or (in a case of small size) you can inject top CSS in <head>
with <style>
to decrease the number of blocking HTTP requests.
postcss-bgimage test postcss-bgimage test Page content
Options
mode
(required) Mode of the plugin.
cutter
- Removesbackground-image:
properties with external references throughurl()
from source CSS.cutterInvertor
- Removes all CSS rules withoutbackground-image
and leaves only this property for other ones.
Test
$ npm test
or
$ yarn test
License
MIT © Alexander Antonov [email protected]