10

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,
  },
};

1
  • I would be very much interested in the answer as well... Commented Mar 1, 2021 at 8:15

3 Answers 3

10

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')
  })
]
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work for me. Rollup is 1.32.1.
The usage of resolve is no more relevant. You can directly specify extract: "app.css".
5

I created a plugin - rollup-plugin-lib-style that generates CSS separately for every style file and imports these generated CSS files (as CSS modules).

Comments

1

This is what I ended up with

import resolve from "@rollup/plugin-node-resolve";
plugins: [
  external(),
  resolve(),
  commonjs(),
  typescript(),
  postcss({
    include: "src/index.css",
    extract: "index.css",
    minimize: true,
    sourceMap: true,
  }),
  postcss({
    include: "src/themes/file2.css",
    extract: "themes/file2.css",
    minimize: true,
    sourceMap: true,
  }),
  terser(),
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.