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

Package detail

rollup-plugin-styles

Anidetrix134.2kMIT4.0.0TypeScript support: included

Universal Rollup plugin for styles: PostCSS, Sass, Less, Stylus and more

rollup, rollup-plugin, css, css-modules, postcss, sass, scss, less, stylus

readme

rollup-plugin-styles

npm version monthly downloads count required rollup version build status code coverage license financial contributors

🎨 Universal Rollup plugin for styles:

...and more!

Table of Contents

Installation

# npm
npm install -D rollup-plugin-styles
# pnpm
pnpm add -D rollup-plugin-styles
# yarn
yarn add rollup-plugin-styles --dev

Usage

// rollup.config.js
import styles from "rollup-plugin-styles";

export default {
  output: {
    // Governs names of CSS files (for assets from CSS use `hash` option for url handler).
    // Note: using value below will put `.css` files near js,
    // but make sure to adjust `hash`, `assetDir` and `publicPath`
    // options for url handler accordingly.
    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.

// Injects CSS, also available as `style` in this example
import style from "./style.css";
// Using named export of CSS string
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 from `node_modules` */
@import "bulma/css/bulma";
/* Local import */
@import "./custom";
/* ...or (if no package named `custom` in `node_modules`) */
@import "custom";

Sass/Less

You can prepend the path with ~ to resolve in node_modules:

// Import from `node_modules`
@import "~bulma/css/bulma";
// Local import
@import "./custom";
// ...or
@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", // Unnecessary, set by default
  // ...or with custom options for injector
  mode: [
    "inject",
    { container: "body", singleTag: true, prepend: true, attributes: { id: "global" } },
  ],
  // ...or with custom injector
  mode: ["inject", (varname, id) => `console.log(${varname},${JSON.stringify(id)})`],
});

CSS Extraction

styles({
  mode: "extract",
  // ... or with relative to output dir/output file's basedir (but not outside of it)
  mode: ["extract", "awesome-bundle.css"],
});

Emitting processed CSS

// rollup.config.js
import styles from "rollup-plugin-styles";

// Any plugin which consumes pure CSS
import litcss from "rollup-plugin-lit-css";

export default {
  plugins: [
    styles({ mode: "emit" }),

    // Make sure to list it after this one
    litcss(),
  ],
};

CSS Modules

styles({
  modules: true,
  // ...or with custom options
  modules: {},
  // ...additionally using autoModules
  autoModules: true,
  // ...with custom regex
  autoModules: /\.mod\.\S+$/,
  // ...or custom function
  autoModules: id => id.includes(".modular."),
});

With Sass/Less/Stylus

Install corresponding dependency:

  • For Sass support install node-sass or sass:

    # npm
    npm install -D node-sass
    # pnpm
    pnpm add -D node-sass
    # yarn
    yarn add node-sass --dev
    # npm
    npm install -D sass
    # pnpm
    pnpm add -D sass
    # yarn
    yarn add sass --dev
  • For Less support install less:

    # npm
    npm install -D less
    # pnpm
    pnpm add -D less
    # yarn
    yarn add less --dev
  • For Stylus support install stylus:

    # npm
    npm install -D stylus
    # pnpm
    pnpm add -D stylus
    # yarn
    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

changelog

4.0.0 (2022-01-14)

Bug Fixes

  • code generation: imports should precede exports (814ccdb)
  • remove debugging log (e09ac09)
  • sass!: remove fibers support (8992356)

Features

Performance Improvements

  • removed deprecated rollup API (46b73e6)
  • sourcemap: switch to source-map-js (0488634)
  • Use Set to optimize performance (b402687)

BREAKING CHANGES

  • node: Now support v12.20.0 and up

3.14.1 (2021-01-28)

Bug Fixes

  • filter out only related files (0976e8f)
  • more reliable chunk hash augmentation (91f9b8f), closes #143

3.14.0 (2021-01-25)

Features

  • pass resulting filename to sourcemap transformer when possible (5570cf4)

3.13.0 (2021-01-24)

Bug Fixes

  • respect esm and styles for sync resolves (0c51253)

Features

3.12.2 (2021-01-11)

Bug Fixes

  • occasional wrong order in extract mode (52cb377), closes #153

3.12.1 (2020-12-30)

Bug Fixes

  • keep references inside declaration (a6f9b35), closes #159
  • postcss related types (df78c78)

3.12.0 (2020-12-28)

Bug Fixes

  • resolving url outside of first order place (c61f0ce), closes #126
  • less: respect paths options (a72ab4c), closes #125
  • avoid partial names replacement when aliasing (f5aa1e2)
  • utils: async resolve types (08ae382)

Features

3.11.0 (2020-10-10)

Bug Fixes

Features

Reverts

  • Revert "chore(release): 3.10.1 [skip ci]" (dc16939)

3.10.1 (2020-10-07)

Bug Fixes

  • correctly resolve file imports with query params (#148) (71bfe87), closes #132

3.10.0 (2020-06-26)

Bug Fixes

  • extract: missing parts of extracted file (9afa9ac)
  • runtime: safer option check (78a5922)
  • url: consider all possible basedirs (62353b2)

Features

  • .d.ts generation and treeshakeable injector options (2990cb0)
  • postcss: support both object and array for plugin (7ce53b9)

3.9.0 (2020-06-23)

Bug Fixes

  • very rare double code extraction (d89a5fe)

Features

  • replace postcss-load-config with custom implementation (50b19bb)
  • respect rollup's 2.18.0 option changes (53072b0)

Performance Improvements

  • misc optimizations (df1d1ee)
  • modules: simpler internal implementation (c1f92e1)

Reverts

  • Revert "chore(release): 3.8.3 [skip ci]" (49b18ff)

3.8.2 (2020-06-11)

Bug Fixes

  • missing/overwritten files (b4dc69c)
  • loaders: clearly show what options are public (2ab86be)
  • partials: incorrect path without dir in url (68d40be)

Performance Improvements

3.8.1 (2020-06-11)

Bug Fixes

  • fix overwrite style file in preserveModules mode (2c13345)
  • use path.parse instead of regex (135b658)

3.8.0 (2020-06-08)

Features

  • sass: sync mode support with dart implementation (91846bc)

Performance Improvements

  • overall considerable performance improvements (6c08d55)
  • replace concat-with-sourcemaps with custom function (a1b04d2)
  • resolved modules caching (7eab36d)

3.7.1 (2020-05-31)

Bug Fixes

  • respect content option for minification (59b3c07)

3.7.0 (2020-05-31)

Features

  • option to disable sourcesContent in sourcemaps (afc440b)

3.6.1 (2020-05-27)

Bug Fixes

  • respect programmatic manual chunks as well (311e0dc)

3.6.0 (2020-05-27)

Bug Fixes

  • build: disable minification for use with patch-package (fbba337)

Features

  • code splitting: rollup's manualChunks option support (5aeb350)
  • injector: ability to set attibutes (653e91c)

Performance Improvements

  • replace some sets with arrays (2b7c28e)

3.5.5 (2020-05-23)

Bug Fixes

  • changelog: repairs after github breakage (b01e058)

3.5.4 (2020-05-23)

Bug Fixes

3.5.3 (2020-05-20)

Bug Fixes

  • small misc fixes and changes (7cbb05a)

3.5.2 (2020-05-20)

Bug Fixes

3.5.1 (2020-05-20)

Bug Fixes

  • typenames too long for typedoc (13e2d49)

3.5.0 (2020-05-20)

Bug Fixes

  • sass: properly call custom resolvers (002366f)
  • sourcemap: broken single line URL comments extraction (fd8ebf9)

Features

  • importer: extensions override (74fcd39)
  • url: automatically inline assets in inject mode (db7dc9a)

Performance Improvements

  • overall improvements and optimizations (a460dd0)
  • types: trimmed down vendor types (6a52be8)

3.4.3 (2020-05-17)

Bug Fixes

  • code splitting: potential over-extraction (0b94909)

3.4.2 (2020-05-17)

Bug Fixes

  • correct filename for single file /w preserveModules (837f9ed)

3.4.1 (2020-05-17)

Bug Fixes

  • correct multi-file detection (d92d6da)

3.4.0 (2020-05-16)

Features

  • code splitting rework /w preserveModules support (8f44a7a)

3.3.4 (2020-05-15)

Bug Fixes

  • stylus: proper sourcesContent (f9b4774)
  • stylus: respect local node_modules (eebbbed)

Performance Improvements

3.3.3 (2020-05-14)

Bug Fixes

  • multi-entry: correct and consistent order of single extracted file (553ca85)
  • do not depend on directory structure for hashing (18c3022)
  • uniform sass error message (49655a4)

3.3.2 (2020-05-13)

Bug Fixes

  • onExtract func check and desc (4bfaac5)

3.3.1 (2020-05-13)

Bug Fixes

3.3.0 (2020-05-12)

Bug Fixes

  • minimize exports in extract mode as well (ab919d7)

Features

3.2.5 (2020-05-11)

Bug Fixes

  • loaders: run sourcemap loader first (b651be3)

Performance Improvements

3.2.4 (2020-05-10)

Bug Fixes

  • sourcemaps: hashing and fixes for inlining in extract mode (c2bb12d)
  • better match Rollup's hashing method (1825045)

3.2.3 (2020-05-09)

3.2.2 (2020-05-09)

Bug Fixes

  • sass: do not modify non-module url (fd022c6)

3.2.1 (2020-05-09)

Bug Fixes

  • respect output.file for main CSS chunk (19eb9d7)

3.2.0 (2020-05-08)

Features

  • import: resolve extensions (9005ab8)

3.1.0 (2020-05-07)

Bug Fixes

  • injector: external @babel/runtime (129aff5)

Features

  • modules: customizable autoModules (c3298de)

3.0.6 (2020-05-07)

Bug Fixes

  • sourcemap: more reliable detection (a419361)

3.0.5 (2020-05-06)

Bug Fixes

3.0.4 (2020-05-05)

Bug Fixes

3.0.3 (2020-05-05)

Bug Fixes

  • url handler: working default assetDir (7187a27)

3.0.2 (2020-05-05)

Bug Fixes

3.0.1 (2020-05-05)

Bug Fixes

  • normalize assetFileNames dir (f551a76)

3.0.0 (2020-05-05)

Features

BREAKING CHANGES

  • Some breaking changes occured:
  • output.assetFileNames is now respected for both CSS files and assets from CSS
  • extract's path now should be relative to output dir/output file's basedir, but not outside of it
  • extract's path no longer passed to PostCSS as to, added dedicated to option instead
  • Removed hash option from url handler (now handled by Rollup's assetFileName)
  • assetDir option for url handler no longer affects resulting URL
  • signature changes for onExtract As always, please consult documentation and API Reference for details

2.2.4 (2020-05-04)

Bug Fixes

2.2.3 (2020-05-04)

Bug Fixes

  • do not bundle mime-types (e444f04)

2.2.2 (2020-05-04)

Bug Fixes

  • styles-url: clarify which url is unresolved (aefc263)

2.2.1 (2020-05-04)

Bug Fixes

  • skip parsing of invalid sourcemaps (a96d01f)

2.2.0 (2020-05-03)

Features

  • injector: one less polyfill needed for ie8 (97796d8)

2.1.1 (2020-05-03)

Bug Fixes

  • injector: pass selector instead of element as container (63bc959)

2.1.0 (2020-05-02)

Features

  • extended browser support (>=ie9, or >=ie8 with polyfills) (d849278)

2.0.5 (2020-05-02)

Bug Fixes

  • revert <no source> removal (8def44e)

2.0.4 (2020-05-02)

Bug Fixes

  • remove <no source> from sourcemap (b1c32e8)

2.0.3 (2020-05-01)

2.0.2 (2020-05-01)

Bug Fixes

  • avoid postcss warning about no plugins (dbd72b7)

2.0.1 (2020-05-01)

Bug Fixes

2.0.0 (2020-05-01)

Features

BREAKING CHANGES

  • styles: A lot of API and behavior changes, please consult README and API Reference

1.5.2 (2020-04-06)

Bug Fixes

  • better lf placement for output (663c184)

1.5.1 (2020-04-06)

Bug Fixes

  • test and warning for already processed files (19fd942)

1.5.0 (2020-04-06)

Features

  • allow multiple instances of the plugin (aa0d4d1)

1.4.0 (2020-04-04)

Features

  • dir in css modules class names (94d10b2)

1.3.15 (2020-03-30)

Bug Fixes

  • p-queue: proper default size (156ebff)

1.3.14 (2020-03-30)

Performance Improvements

  • global p-queue for all loaders (ccac951)

1.3.13 (2020-03-29)

Bug Fixes

  • fix production build, also speed up builds (b5b2e4f)

1.3.12 (2020-03-29)

Bug Fixes

1.3.11 (2020-03-29)

Performance Improvements

1.3.10 (2020-03-27)

Bug Fixes

  • proper declaration file (0b5502b)

1.3.9 (2020-03-24)

Bug Fixes

  • less: add plugins to less loader options type (8b502ef)

1.3.8 (2020-03-24)

Performance Improvements

  • runtime: less operations (7375fe7)

1.3.7 (2020-03-21)

Performance Improvements

  • postcss: less allocations (c9ac3f5)

1.3.6 (2020-03-20)

Bug Fixes

  • less: use provided less in importer (66f0fce)

1.3.5 (2020-03-20)

Bug Fixes

  • types: fix less typings (c129dec)

1.3.4 (2020-03-20)

Performance Improvements

  • less: less allocations in importer (bf3710a)

1.3.3 (2020-03-20)

Bug Fixes

  • less: remove less from bundle (9dafe0a)

1.3.2 (2020-03-20)

Bug Fixes

  • less: fileManager inheritance (aba12a4)

1.3.1 (2020-03-20)

Bug Fixes

Features

1.2.6 (2020-03-19)

Performance Improvements

  • relace import-cwd with resolve (dd4310e)

1.2.5 (2020-03-18)

Bug Fixes

  • respect rollup's sourcemap (cc6ad34)

1.2.4 (2020-03-18)

Bug Fixes

  • deps: more precise pkg versions (403158c)

1.2.3 (2020-03-18)

Bug Fixes

  • path-utils: fix relativePath (e22ca71)

1.2.2 (2020-03-18)

Bug Fixes

  • extract: allow paths relative to cwd (bdfc266)

Performance Improvements

  • path: unneded normalization (df03c59)

1.2.1 (2020-03-17)

Bug Fixes

1.2.0 (2020-03-17)

Features

  • styles: postcss plugins without require (7db718f)

1.1.11 (2020-03-16)

Bug Fixes

  • readme: tagline wording (0a55020)

1.1.10 (2020-03-16)

Bug Fixes

1.1.9 (2020-03-16)

Bug Fixes

1.1.8 (2020-03-15)

Performance Improvements

  • postcss: less type checking (5611655)
  • sourcemap: less operations (178e2d4)

1.1.7 (2020-03-15)

Performance Improvements

  • project: refactoring and optimizations (d3405aa)

1.1.6 (2020-03-15)

Bug Fixes

  • build: add dependabot and dependency badge (761e3ed)

1.1.5 (2020-03-14)

Bug Fixes

  • ci: do not release twice (fc719f2)

1.1.4 (2020-03-14)

Bug Fixes

  • node: wrong supported version of Node.js (3fb9f80)

1.1.3 (2020-03-14)

Bug Fixes

  • runtime: working singleTag option (5d6a5c8)

1.1.2 (2020-03-13)

Performance Improvements

  • styles: do not call path.join twice (272bb8b)

1.1.1 (2020-03-13)

Bug Fixes

1.1.0 (2020-03-13)

Bug Fixes

  • styles: fix CSS injector (f4ccef4)

Features

  • styles: add singleTag option for CSS injection (b7e4ca8)

Performance Improvements

  • styles: small CSS injector optimization (fbdd413)

1.0.5 (2020-03-13)