Rollup plugin postcss does not inject css imported from node_modules

9.2k Views Asked by At

Here is my rollup config

// rollup.config.js
const postcss = require('rollup-plugin-postcss');
const autoprefixer = require('autoprefixer');

module.exports = {
  rollup(config, _options) {
    config.plugins.push(
      postcss({
        plugins: [
          autoprefixer(),
        ],
        extensions: ['.css'],
        modules: false,
        extract: false,
      }),
    );
    return config;
  },
};

So if I import css file local from a relative path, it gets injected but I import from node_modules it doesn't

import React from 'react';
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
// The following works if I copy the file locally
// import './ReactToastify.css';

What am I doing wrong here?

1

There are 1 best solutions below

4
On

I came across exactly the same problem and I think I found the solution. The trick here is to use rollup-plugin-postcss (or also rollup-plugin-styles) in combination with @rollup/plugin-node-resolve.

My rollup.config.js looks something like this:

import { nodeResolve } from '@rollup/plugin-node-resolve'
import postcss from 'rollup-plugin-postcss'
// import styles from 'rollup-plugin-styles'

export default {
  ...
  plugins: [
    postcss(),
    // styles(),
    nodeResolve({
      extensions: ['.css']
    })
  ]
};

As far as I can tell, for my simple case, it doesn't matter if you use postcss or styles plugins. Both work the same.