Generate individual CSS files using postcss and rollup

When I import each css file in my JavaScript, I want it to generate a new css file for the build. For example:

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

This would result in creating dist/app.css and dist/admin.css. My configuration file for rollup looks like this:

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

Answer №1

Although it has not been officially documented, you can utilize the include and exclude options as outlined in the source code.

Below is an example demonstrating how to handle 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')
  })
]

If you need to exclude a specific file, you can do so with the following configuration:

plugins: [
  postcss({
    exclude: "**/bootstrap.css",
    extract: resolve('dist/app.css')
  }),
  postcss({
    include: "**/bootstrap.css",
    extract: resolve('dist/bootstrap.css')
  })
]

Answer №2

Introducing my latest creation: the rollup-plugin-lib-style plugin! This powerful tool is designed to generate CSS files individually for each style file, allowing you to easily import these generated CSS files as CSS modules.

Answer №3

Here is the final setup I came 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(),
]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging src css within the React public directory

I have set up my React application to import a stylesheet from the src folder using import './css/styles.css' at the top of App.js. In the public folder, I have created a basic /docs/index.html file that is linked to from the React app but opera ...

Learn how to apply inline styles to multiple objects within a single element using React

In my project using React, I am attempting to apply styles only to a specific element within another element. Here is an example with an h1 tag: const Header = () => { const firstName = "Ashraf"; const lastName = "Fathi"; const date = new Date() ...

What could be the reason for the empty space between the upper and middle cuts?

I am working on these two web pages: http://slacklog.heroku.com/ http://slacklog.heroku.com/signup/date As you can see, there is a gap between the top and middle images as well as the bottom and middle images on both pages. Below is my HTML code: <d ...

What steps should I take to adjust the text size in my media query?

I'm facing a challenge with a media query to decrease the font size of a text. It seems like a straightforward task, yet I can't pinpoint the issue. HTML: <div class="fp-featured"> <div class="fp-title">TITLE</div> <p class ...

Can a shape similar to an inverted circle be created using CSS and jQuery?

I'm stumped on how to create an inverted circle in the corners. I've included a picture for reference. https://i.stack.imgur.com/jeNdh.jpg Is it feasible to achieve this effect using CSS3 or maybe jQuery? ...

Could you please advise me on where I should apply line-height in order for it to work properly?

My div is structured like this: I am trying to decrease the spacing between the lines. I attempted adding a line-height attribute to both the parent div and one of the label elements like this: <div class="row" id="gadsDiv" runat=&q ...

Ways to update the title of a Magento widget

I've added a widget to showcase new products on my Home page and it's displaying perfectly. However, I want to get rid of the title "New Product" that comes with it. This pesky little thing needs to go! I'm having trouble locating the corr ...

My website has a navbar button that is not directing to the intended section of the screen

I have created this HTML code for my Navbar buttons: <button class="navbar-toggle" data-toggle = "collapse" data-target=".navHeaderCollapse"> <span class="icon-bar"></span> <span class="icon-bar">< ...

Exploring the world of child theme customization in WordPress

I am looking to make some modifications to my website. Working with a child theme, I have plans to adjust the CSS and make some structural changes. Modifying the CSS seems pretty straightforward as I just need to reference the classes or IDs and input new ...

What is the best way to design a group of radio buttons with corresponding labels at the top

I'm looking to create a structure like this in HTML: Does anyone know how I can achieve this layout so that the labels are aligned properly with the radio buttons? My main challenge is getting the horizontal labels above each radio button. I would a ...

The _rfs.scss width rule in Angular-Bootstrap-NgBootstrap is overriding the column width of the col-rules

The Dilemma I am currently working on an Angular project that incorporates Bootstrap and ng-bootstrap. My intention is to utilize the grid system within this project, but I encountered some bugs along the way. When trying to render the following HTML/TS ...

When using NextJs, running the commands 'npm build' and 'npm start' can sometimes cause style inconsistencies

Currently working on a project with NextJs (sorry, no screenshots allowed). Running 'npm run dev' for development works perfectly fine, but when I switch to 'npm run build' and then 'npm start', the website displays overlappin ...

Implementing CSS and HTML in Email Communication

I'm noticing a difference between how this appears in a browser versus an email. The text on the image looks perfect when viewed on Mozilla/Chrome, but when I use the same code in an email, the text ends up displaced below the image (right beneath it) ...

What is the best way to align two elements side by side using flexbox?

After trying to find a solution on this site without success, I realized that my situation is quite unique. I need to avoid changing the HTML code, minimize the use of classes, and refrain from using IDs. The current HTML structure I am working with inclu ...

How come my web page is not displaying the guages from guage.js?

I am utilizing guage.js to create a graph based on this Fiddle. However, upon adding the same code to my website, the rendering does not appear as expected: Upon inspecting in Chrome developer tools, I noticed that the element still exists but lacks heigh ...

Issue with IE 11: Including card image inside anchor tags in Bootstrap 4 causes unwanted white space

I'm having an issue with my Bootstrap 4 card markup where the image is wrapped inside an anchor tag. The problem arises when viewing it in IE 11, as there is a large white space underneath the image, while everything displays fine in Chrome, Edge, and ...

Looking for a solution to organize the dynamically generated list items in an HTML page

I am currently working on a movie listing website where all the movies are displayed in sequence based on their #TITLE#. The webpage is generated automatically by the software using a template file. Here is the section of code in the template file that sho ...

What is the best way to select the initial descendant that matches a specific

How can we target only the first matching descendant element, without affecting others further down the hierarchy? For example: If the HTML structure is like this: <div class="wrapper"> <table> <tbody> <tr> < ...

Displaying and hiding the top menu when the div is scrolled

I have developed a script that is designed to display the menu in a shaking motion and hide it as you scroll down. It functions properly when scrolling within the body of the webpage, but I am facing issues when attempting to do so with a div that has an o ...

Chrome has some issues with resizing the SVG Pattern element

Utilizing inline svgs, I have a svg circle filled with a pattern that should cover 100% of the container size. However, when the parent element is resized via JavaScript, the pattern no longer reflects the 100% width and height as expected. This issue seem ...