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

Package detail

rollup-plugin-sass

elycruz64.1kMIT1.15.2TypeScript support: included

Rollup Sass files.

rollup, rollup-plugin, sass

readme

rollup-plugin-sass CI issues npm mit Coverage Status

Installation

npm install rollup-plugin-sass -D

Usage

// rollup.config.js
import sass from 'rollup-plugin-sass';

export default {
  input: 'index.js',
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  plugins: [sass()],
};

rollup.config.ts

Add allowSyntheticDefaultImports, or esModuleInterop (enables allowSyntheticDefaultImports), to tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterOp": true
  }
}

esModuleInterop reference

Write rollup.config.ts:

// rollup.config.ts
import sass from 'rollup-plugin-sass';

// ...

Profit.

Options

output

  • Type: Boolean|String|Function
  • Default: false
sass({
  // Default behavior disable output
  output: false,

  // Write all styles to the bundle destination where .js is replaced by .css
  output: true,

  // Filename to write all styles
  output: 'bundle.css',

  // Callback that will be called on generate bundle with two arguments:
  // - styles: the concatenated styles in order of imported
  // - styleNodes: an array of style objects:
  //  [
  //    { id: './style1.scss', content: 'body { color: red };' },
  //    { id: './style2.scss', content: 'body { color: green };' }
  //  ]
  output(styles, styleNodes) {
    writeFileSync('bundle.css', styles);
  },
});

insert

  • Type: Boolean
  • Default: false

If you specify true, the plugin will insert compiled CSS into <head/> tag, via utility function that it will output in your build bundle.

sass({
  insert: true,
});

processor

  • Type: Function

If you specify a function as processor which will be called with compiled css before generate phase.

import autoprefixer from 'autoprefixer';
import postcss from 'postcss';

sass({
  // Processor will be called with two arguments:
  // - style: the compiled css
  // - id: import id
  processor: (css) =>
    postcss([autoprefixer])
      .process(css)
      .then((result) => result.css),
});

The processor also support object result. Reverse css filled for stylesheet, the rest of the properties can be customized.

sass({
  processor(code) {
    return {
      css: '.body {}',
      foo: 'foo',
      bar: 'bar',
    };
  },
});

Otherwise, you could do:

import style, { foo, bar } from 'stylesheet';

Create CSS modules using processor cssModules output

When returning a cssModules property inside a processor's output, the plugin will change the module's default export to the value of cssModules instead of the compiled CSS code.

The following example uses postcss-modules to create css modules:

import postcss from 'postcss';
import postcssModules from 'postcss-modules';

sass({
  async processor(css, id) {
    let cssModules = {};
    const postcssProcessResult = await postcss([
      postcssModules({
        getJSON: (_, json) => {
          if (json) cssModules = json;
        },
      }),
    ]).process(css, { from: id });

    return { css: postcssProcessResult.css, cssModules };
  },
});

Which allows you to write something like:

import style from 'stylesheet';

style['some-classes'];

Exporting sass variable to *.js

Example showing how to use icss-utils to extract resulting sass vars to your *.js bundle:

const config = {
  input: 'test/fixtures/processor-promise/with-icss-exports.js',
  plugins: [
    sass({
      processor: (css) => {
        const pcssRootNodeRslt = postcss.parse(css);
        const extractedIcss = extractICSS(pcssRootNodeRslt, true);
        const cleanedCss = pcssRootNodeRslt.toString();
        const out = { css: cleanedCss, ...extractedIcss.icssExports };
        // console.table(extractedIcss);
        // console.log(out);
        return out;
      },
    }),
  ],
};

See the Input file for example on how to access the exported vars.

runtime

  • Type: Object
  • Default: sass

If you specify an object, it will be used instead of sass. You can use this to pass a different sass compiler (for example the node-sass npm package).

api

  • Type: 'legacy'|'modern'
  • Default: 'legacy'
sass(); // default to legacy

sass({ api: 'modern' });

sass({
  api: 'modern',
  options: {
    style: 'compressed',
  },
});

options

  • Type: Object

[!NOTE] The content of options is sensible to the value specified in api option

Options for sass or your own runtime sass compiler.

If you specify data, the plugin will treat as prepend sass string. Since you can inject variables during sass compilation with node.

sass({
  options: {
    data: '$color: #000;',
  },
});

[!TIP] If your are working with npm packages, consider to use NodePackageImporter

import * as sass from 'sass';

sass({
  options: {
    importers: [new sass.NodePackageImporter()],
  },
});

include

  • Type: string | string[]
  • Default: ['**/*.sass', '**/*.scss']

Glob of sass/css files to be targeted.

sass({
  include: ['**/*.css', '**/*.sass', '**/*.scss'],
});

exclude

  • Type: string | string[];
  • Default: 'node_modules/**'

Globs to exclude from processing.

sass({
  exclude: 'node_modules/**',
});

License

MIT elycruz, BinRui.Guan

changelog

1.2.9

Fixes:

9b938ee - moved insertStyle test to correct file b940849 - insertStyle no longer throws a fatal error when window is undefined

Other:

c9332d0 - #88 - Added 'master' branch to 'dev-build-and-test' git workflow file.

1.2.6

Fixes:

  • Reverted rollup-pluginutils version back to recently upgraded @rollup/pluginutils version, minus one major version from latest version - The version is set to one major behind to ensure backwards compatibility with existing projects.
    We also upgraded to this version, of @rollup/pluginutils, to allow users, who have started to use rollup-plugin-sass@1.2.5, to continue supplying RegExp | string | string[] for include and exclude properties (rollup-pluginutils doesn't accept RegExp for these props).
  • Updated plugin's source to ensure that include and exclude always have a default when their incoming values are directly set to undefined - previous update had removed these default, since they were set in options merge call, however merging in undefined prop. values weren't taken into account in that update - this update fixes that change.

1.1.0

New Features

  • support rollup@1.x (#61)
  • output options only works with bundle.write()

0.9.2

Bug Fixes

  • allow options.exclude to be empty string

0.9.1

Bug Fixes

  • fix ci error
  • remove node-sass in devDependencies

0.9.0

New Features

  • switch default sass compiler to sass from node-sass (#56)

0.8.1

Bug Fixes

  • async resolve hangs in edge case #55

0.8.0

New Features

  • add options.runtime support different sass compiler runtime

0.7.2

Bug Fixes

  • can not resolve file id start with ~ (#38)

0.7.0

New Features

  • the processor support object result