DEV Community

Discussion on: How to Create and Publish a React Component Library

Collapse
 
oscarcornejo profile image
Oscar Cornejo Aguila

Hi Alex, very good publication, but I was left with a question, how can I work or include sass in my component library?

I would appreciate the help!

Collapse
 
alexeagleson profile image
Alex Eagleson • Edited

Great question, and it took a bit of time to figure out.

Getting SCSS to work with the rollup bundle is extremely easy. All you need to do from the current state of the tutorial is update rollup.config.js in the dts() plugin config section. Change the external value from just CSS to:

external: [/\.(css|less|scss)$/],

With that alone you can now change Button.css to Button.scss and import Button.scss in your Button component. That's it! The Postcss plugin for Rollup that is already installed will take care of it for you.

Getting it to work with Stoyrbook however is another beast altogether and involves re-installing Storybook using the Webpack 5 option instead of the default webpack 4. More than I can fit in a comment here, but I may update the blog post itself.

I have however updated the repository with those changes, so it should work out of the box with Storybook if you clone it now.

Cheers.

Collapse
 
farihaaftab0 profile image
FarihaAftab0 • Edited

Will you please tell how to make it work with css modules.. how to resolve module.scss extension?

Thread Thread
 
alexeagleson profile image
Alex Eagleson

Simply add modules: true on your Rollup postcss plugin. There's a section for modules on the plugin NPM docs:

npmjs.com/package/rollup-plugin-po...

Collapse
 
oscarcornejo profile image
Oscar Cornejo Aguila

Thanks Alex, I was really lost on how to add sass to my component library, and although I followed the steps that storybook recommends, I had not realized that the errors that kept happening were due to the webpack version, thank you very much for this update. 👏👏
Greetings from Chile! 🙌

Collapse
 
alexeagleson profile image
Alex Eagleson

New SCSS section has been added to the post.

Some comments have been hidden by the post's author - find out more