How do I make React components import extracted scss modules files with Rollup?

Viewed 479

I'm making a React Component Library with Rollup with the following component setup:

//component A
import styles from './stylesA.module.scss'

const ComponentA = () => {
  return(
    <div className={styles.red}>Component A Styled</div>
  )
}

export { ComponentA }

//stylesA.module.scss
.red {
  color: red;
}

How do I configure Rollup so that my output components auto-import the generated .css files into my component? Note that I don't want the styles to be bundled into the .js files nor do I want to import the styles manually when I import the final package with import 'my-library/dist/stylesA.css'

My rollup.config.js looks like this for the moment:

// rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import postcss from 'rollup-plugin-postcss';
import pkg from './package.json'


export default {
  input: 'src/index.js',
  output: [{
    dir: './dist',
    format: 'cjs'
  }],
  plugins: [
    resolve(),
    babel({
      exclude: 'node_modules/**',
      babelHelpers: 'bundled'
    }),
    postcss({
      modules: true,
      extract: true,
      minimize: true
    })
  ],
  external: Object.keys(pkg.peerDependencies || {})
};
0 Answers
Related