Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minify css using rollup plugin node-sass

I know that node-sass can minify css by default. But what option is there for a pipeline like mine, that creates a js and a separate css artifact?

const resolve = require('rollup-plugin-node-resolve');
const babel = require('rollup-plugin-babel');
const replace = require('rollup-plugin-replace'); // use to setup project enviroment variables
const sass = require('rollup-plugin-sass');
const json = require('rollup-plugin-json');
const image = require('rollup-plugin-image');
const reactSvg = require('rollup-plugin-react-svg');
const fs = require('fs');

const babelOptions = JSON.parse(fs.readFileSync('.babelrc'));

const js = {
  input: 'src/index.js',
  output: {
    file: 'lib/index.js',
    format: 'es',
  },
  plugins: [
    json(),
    reactSvg(),
    image(),
    sass({
      output: 'lib/styles.css',
    }),
    babel({
      ...babelOptions,
      babelrc: false,
      exclude: ['node_modules/**'],
      runtimeHelpers: true,
      comments: false,
    }),
    resolve({
      jsnext: true,
      module: true,
    }),
  ],
};

export default [js];

As you see, I only have an index.js file as input and output, but on the way I'm outputting the css separately. But how can I make that artifact being minified?

Or do you see any flaws in this kind of approach?

like image 926
codepleb Avatar asked Sep 08 '26 22:09

codepleb


1 Answers

You can actually use the rollup-plugin-scss plugin

import scss from 'rollup-plugin-scss';

and set the option to minify using outputStyle: "compressed"

// rollup.config.js

export default {
  //...,
  plugins: [
    //...,
    scss({
      output: 'lib/styles.css',
      outputStyle: "compressed",
    }),
    //...,
  ],
  //...,
}

to output a minified .css file.

like image 128
Ishan Madhusanka Avatar answered Sep 11 '26 02:09

Ishan Madhusanka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!