Anidetrix / rollup-plugin-styles

🎨 Universal Rollup plugin for styles: PostCSS, Sass, Less, Stylus and more.
https://anidetrix.github.io/rollup-plugin-styles
MIT License
242 stars 43 forks source link

Example on how to use (scoped) SCSS plus lit-element #155

Open brecke opened 3 years ago

brecke commented 3 years ago

Hi there,

I'm trying to import scss on a lit-element based web component, but I can't seem to make rollup-plugin-styles and rollup-plugin-lit-css to work. On the documentation there's an example for importing pure CSS but how would this work with SCSS?

To be clear:

Is there a way I can import SCSS in a scoped manner?

Thanks in advance

ViRuSTriNiTy commented 2 years ago

A solution that uses a workaround has been posted here:

https://github.com/bennypowers/rollup-plugin-lit-css/issues/23

I'm also still trying to find out the right way to do it.

ViRuSTriNiTy commented 2 years ago

The solution posted above does not seem to work anymore (at least for me) as it seems to target Lit 2.x.

I have analyzed the plugin code and its dependencies and my impression is that the documentation is misleading. It reads like you just need to add this and that module, add this and that line and it flies but it does not.

The underlying issue here is that rollup-plugin-lit-css just processes pure CSS as

https://github.com/bennypowers/lit-css/blob/74dc908cd05c177267cdd3df6bdca1c9821630e8/packages/rollup-plugin-lit-css/rollup-plugin-lit-css.ts#L33

along with the other filter code completely ignores .scss files by default and due to the hard-coded css extension check and the missing support for transforming the id the whole construct just breaks.

You could fiddle something together by manually altering the files in the node_modules folder so that it processes the files but then the plugin has no import of node-sass or sass whatsoever so the .scss file would not be parsed correctly.

I think it must be a 3 step solution in rollup.config.js like

plugins: [
    styles({
      mode: "emit"
    }),
    sassToCss(), // custom plugin that calls sass and compiles (aka transforms in terms of a plugin) the sass           
    litCss()
]

There are plugins that transform SASS to CSS but these are already working without rollup-plugin-styles or rollup-plugin-lib-css:

But these are somewhat outdated as both still use node-sass.

I still wonder how the tests in __tests__ can even run scss imports. Perhaps the secret is buried there 🙄

ViRuSTriNiTy commented 2 years ago

I have given up to solve this after multiple hours of searching the needle in the haystack.

You can use https://www.npmjs.com/package/@j1shin/rollup-plugin-lit-sass to achieve what you want, just follow the description of the package. All necessary steps are explained.