Search code examples
postcssrollupjs

Nested @import CSS statements not getting rolled up using rollup-plugin-postcss


I have this source file as src/mike.js

import '@ckeditor/ckeditor5-ui/theme/globals/globals.css'
export default function () {
    console.log('Hello world');
} 

@ckeditor/ckeditor5-ui/theme/globals/globals.css looks like this:

@import "./_hidden.css";
@import "./_reset.css";
@import "./_zindex.css";

And I have this rollup config:

import resolve from 'rollup-plugin-node-resolve';
import postcss from 'rollup-plugin-postcss'


export default {
    input: 'src/mike.js',
    output: {
        file: 'public/bundle2.js',
        format: 'cjs'
    },
    plugins: [resolve(), postcss({
        plugins: []
    })]
};

The rolled up public/bundle2.js looks like this:

'use strict';

function styleInject(css, ref) {
  // plugin function, removed for clarity
}

var css = "/*\n * Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.\n * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license\n */\n\n@import \"./_hidden.css\";\n@import \"./_reset.css\";\n@import \"./_zindex.css\";\n";
styleInject(css);

function mike () {
    console.log('Hello world');
}

module.exports = mike;

So rollup-plugin-postcss did not follow the nested @import statements here.

How to make this work?


Solution

  • Alright, the answer is that PostCSS itself needs plugins to handle @import statements. So the rollup config you need is:

    import resolve from 'rollup-plugin-node-resolve';
    import postcss from 'rollup-plugin-postcss'
    
    import postcssImport from 'postcss-import';
    
    
    export default {
        input: 'src/mike.js',
        output: {
            file: 'public/bundle2.js',
            format: 'cjs'
        },
        plugins: [resolve(),
            postcss({
                plugins: [postcssImport()]
            })]
    };