rollup-plugin-styles
🎨 Universal Rollup plugin for styles:
...and more!
Table of Contents
Installation
npm install -D rollup-plugin-styles
pnpm add -D rollup-plugin-styles
yarn add rollup-plugin-styles --dev
Usage
import styles from "rollup-plugin-styles";
export default {
output: {
assetFileNames: "[name]-[hash][extname]",
},
plugins: [styles()],
};
After that you can import CSS files in your code:
import "./style.css";
Default mode is inject
, which means CSS is embedded inside JS and injected into <head>
at runtime, with ability to pass options to CSS injector or even pass your own injector.
CSS is available as default export in inject
and extract
modes, but if CSS Modules are enabled you need to use named css
export.
import style from "./style.css";
import { css } from "./style.css";
In emit
mode none of the exports are available as CSS is purely processed and passed along the build pipeline, which is useful if you want to preprocess CSS before using it with CSS consuming plugins, e.g. rollup-plugin-lit-css.
PostCSS configuration files will be found and loaded automatically, but this behavior is configurable using config
option.
Importing a file
CSS/Stylus
@import "bulma/css/bulma";
@import "./custom";
@import "custom";
Sass/Less
You can prepend the path with ~
to resolve in node_modules
:
@import "~bulma/css/bulma";
@import "./custom";
@import "custom";
Also note that partials are considered first, e.g.
@import "custom";
Will look for _custom
first (with the approptiate extension(s)), and then for custom
if _custom
doesn't exist.
CSS Injection
styles({
mode: "inject",
mode: [
"inject",
{ container: "body", singleTag: true, prepend: true, attributes: { id: "global" } },
],
mode: ["inject", (varname, id) => `console.log(${varname},${JSON.stringify(id)})`],
});
styles({
mode: "extract",
mode: ["extract", "awesome-bundle.css"],
});
Emitting processed CSS
import styles from "rollup-plugin-styles";
import litcss from "rollup-plugin-lit-css";
export default {
plugins: [
styles({ mode: "emit" }),
litcss(),
],
};
styles({
modules: true,
modules: {},
autoModules: true,
autoModules: /\.mod\.\S+$/,
autoModules: id => id.includes(".modular."),
});
With Sass/Less/Stylus
Install corresponding dependency:
-
For Sass
support install node-sass
or sass
:
npm install -D node-sass
pnpm add -D node-sass
yarn add node-sass --dev
npm install -D sass
pnpm add -D sass
yarn add sass --dev
-
For Less
support install less
:
npm install -D less
pnpm add -D less
yarn add less --dev
-
For Stylus
support install stylus
:
npm install -D stylus
pnpm add -D stylus
yarn add stylus --dev
That's it, now you can import .scss
.sass
.less
.styl
.stylus
files in your code.
Configuration
See API Reference for Options
for full list of available options.
Why
Because alternatives did not look good enough - they are either too basic, too buggy or poorly maintained.
For example, the main alternative (and inspiration) is rollup-plugin-postcss, but at the time it is not actively maintained, has a bunch of critical bugs and subjectively lacks some useful features and quality of life improvements which should be a part of it.
With that said, here is the basic list of things which differentiate this plugin from the aforementioned one:
- Written completely in TypeScript
- Up-to-date CSS Modules implementation
- Built-in
@import
handler - Built-in assets handler
- Ability to emit pure CSS for other plugins
- Complete code splitting support, with respect for multiple entries,
preserveModules
and manualChunks
- Multiple instances support, with check for already processed files
- Proper sourcemaps, with included sources content by default
- Respects
assetFileNames
for CSS file names - Respects sourcemaps from loaded files
- Support for implementation forcing for Sass
- Support for partials and
~
in Less import statements - More smaller things that I forgot
License
MIT © Anton Kudryavtsev
Thanks