Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create separate CSS files in postcss & rollup

I want each css file I import in my JS to create a new css file for my build. For instance:

import "./app.css";
import "./admin.css";

would create dist/app.css and dist/admin.css. I am using rollup and here is my config file:

import commonjs from "@rollup/plugin-commonjs";
import postcss from "rollup-plugin-postcss";
import resolve from "@rollup/plugin-node-resolve";
import { terser } from "rollup-plugin-terser";

import postcssImport from "postcss-import";
import postcssNested from "postcss-nested";
import autoprefixer from "autoprefixer";

const dev = process.env.WP_ENV === "development";

export default {
  input: "src/main.js",
  output: {
    sourcemap: dev,
    format: "iife",
    name: "main",
    file: "dist/main.bundle.js",
  },
  plugins: [
    resolve({
      browser: true,
    }),
    postcss({
      plugins: [postcssImport(), postcssNested(), autoprefixer()],
      extract: true,
      sourceMap: "inline",
      minimize: !dev,
    }),
    commonjs(),
    !dev && terser(),
  ],
  watch: {
    clearScreen: false,
  },
};

like image 303
Jamie Avatar asked Apr 22 '26 09:04

Jamie


1 Answers

It has not been documented but you can use include, exclude according to their source code.

An example here for two css files:

import { resolve } from "path"
plugins: [
  postcss({
    include: "**/admin.css",
    extract: resolve('dist/admin.css')
  }),
  postcss({
    include: "**/app.css",
    extract: resolve('dist/app.css')
  })
]

And if you want to exclude one file:

plugins: [
  postcss({
    exclude: "**/bootstrap.css",
    extract: resolve('dist/app.css')
  }),
  postcss({
    include: "**/bootstrap.css",
    extract: resolve('dist/bootstrap.css')
  })
]
like image 190
lastr2d2 Avatar answered Apr 24 '26 02:04

lastr2d2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!