rollup-plugin-postcss icon indicating copy to clipboard operation
rollup-plugin-postcss copied to clipboard

Unused compiled CSS classes aren't eliminated

Open StarpTech opened this issue 4 years ago • 6 comments

As titled. In the example below you can see that unused styles are bundled- I'd expect that rollup is able to remove them in context of CSS-Modules.

var styles = {"button":"Button__button","unused":"Button__unused"};

// eslint-disable-next-line no-unused-vars
function Button() {
  return /*#__PURE__*/React.createElement("div", {
    className: styles.button
  });
}

export default Button;

StarpTech avatar May 25 '20 09:05 StarpTech

It is worthy, let me do some research about implementation.

SASUKE40 avatar May 26 '20 02:05 SASUKE40

Related https://github.com/developit/microbundle/issues/632

StarpTech avatar May 26 '20 07:05 StarpTech

@StarpTech I believe this is related to https://github.com/rollup/rollup/issues/2201#issuecomment-520122966

Anidetrix avatar May 26 '20 07:05 Anidetrix

@Anidetrix thanks!

StarpTech avatar May 26 '20 08:05 StarpTech

It's not really a rollup thing. Property hoisting is handled by terser.

$ cat button.js
var styles = {"button": "Button__button", "unused": "Button__unused"};
function Button() {
    return /*#__PURE__*/React.createElement("div", {
        className: styles.button
    });
}
export default Button;
$ terser button.js --module -bc passes=3
export default function() {
    return React.createElement("div", {
        className: "Button__button"
    });
}

Or if you prefer the rollup CLI:

$ rollup button.js -p terser='{module:true,compress:{passes:3},output:{beautify:true}}' --silent
export default function() {
    return React.createElement("div", {
        className: "Button__button"
    });
}

I generally use terser --module -mc passes=3,unsafe,pure_getters depending on the code base, but YMMV.

kzc avatar May 27 '20 16:05 kzc

some update about this? sound a very nice addon

CSDev0 avatar Mar 02 '22 13:03 CSDev0