Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Svelte warning "Unused CSS selector"

Tags:

css

svelte

The approach of my graphic designer for formatting our Svelte application is having a systematic set of classes in LESS, importing the appropriate LESS file in the component or page, and then applying those classes wherever he needs them. As a result we have an abundant amount of unused classes, which we might use at a later point.

The great thing about Svelte is that unused CSS is not compiled, so all those (yet) redundant classes are not in the way anyway. However, whenever we compile, we get a big list of warnings: "Unused CSS selector". This is a major nuisance, because it makes it harder to notice when an actual error is created. Plus it just looks ugly.

I checked the documentation and there is a way to suppress warnings, but that only works for the HTML part.

Is there any way of getting rid of these warnings? Note that we use Svelte Preprocess.

like image 738
Mielipuoli Avatar asked Mar 13 '20 22:03

Mielipuoli


3 Answers

I found this solution a bit smoother which I modified slightly:

// rollup.config.js
...
svelte({
    ...
    onwarn: (warning, handler) => {
        const { code, frame } = warning;
        if (code === "css-unused-selector")
            return;

        handler(warning);
    },
    ...
}),
...
like image 100
Fractalf Avatar answered Nov 12 '22 18:11

Fractalf


I use a simple explicit ignore mechanism:

First, define the warning key and a regex to capture the meat of the ignore message. Then, loop over ignore patterns and match at least one.

// rollup.config.js
...
const warnIgnores = {
  'css-unused-selector': {
    capture: /.*"(.*)"$/,
    ignore: [
      /^\.p\d+/,
      /^\.sm\d+/,
      /^\.md\d+/,
      /^\.lg\d+/,
      /^\.xg\d+/,
      /^\.all\d+/,
      /^\.row(::after)?/
    ]
  }
}
...
svelte({
  ...
  // Explicitely ignore warnings
  onwarn: (warning, handler) => {
    const { message, code } = warning;
    const patterns = warnIgnores[code];
    if (patterns != undefined) {
      /* Find the meat. */
      const meat = message.match(patterns.capture);
      if (meat != null) {
        for (var i = 0; i < patterns.ignore.length; i++) {
          if (meat[1].match(patterns.ignore[i]) != null) {
            return;
          }
        }
      }
    }
    handler(warning);
  },
  ...
});

Explicit is better than implicit!

like image 21
pylover Avatar answered Nov 12 '22 20:11

pylover


There doesn't seem to be any possibility to turn off this warning in a proper way. However, there is a workaround.

In node_modules/svelte/compiler.js, remove line 24842 or put it in comments:

this.stylesheet.warn_on_unused_selectors(this);

You will have to do this again when you update or reinstall svelte.

like image 2
Mielipuoli Avatar answered Nov 12 '22 19:11

Mielipuoli