Creating unique CSS bundles with Webpack 5: A guide

I am working on a React project with a custom webpack configuration, and I need to generate multiple packages with different styles. While the application remains the same in each package, I want to include a specific CSS file in each generated package. For instance, within my repository, I have various CSS files:

- src
  - index.js
  - index.html
  - themes
    - theme1
        theme1.css
    - theme2
        theme2.css
    - theme3 
        theme3.css

After building, the structure should look like this: (within the "build" folder)

- build
     index.html
     main.css // containing styles from theme2, for example
     bundle.js

I am unsure how to achieve this using webpack or which plugin to use. Any assistance would be greatly appreciated. Thank you

Answer №1

To have different CSS files separated, you can specify multiple entries in your webpack configuration file.

For example:

webpack.config.js

module.exports = {
  entry: {
    main: "./path/to/your_main.js",
    "css-theme-1": "./path/to/your/theme.css",
    "css-theme-2": "./path/to/your/another-theme.css",
  }
}

You can also organize them into separate folders by setting the filename properties in the mini-css-extract-plugin.

For more information, check out:

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

Navigating DOM Elements Using React

I am working on a Navigation Component that consists of 3 Sub-Components: NavigationHeader NavigationList NavigationBtn Within the NavigationBtn element, I have a Button to which I want to attach an event listener: onClick={btnClickHandler} The Clickhan ...

Switch the background color of the checkbox div or label with a single click

I have successfully implemented the following code, with one small issue. When I select the checkbox, the background color of the div changes from #fff to #ffe600 as expected. However, after submitting the form and refreshing the page, the background color ...

Seamless MUI Slide animation effortlessly transitions elements out

Currently, I am experimenting with implementing the Slide transition from React MUI on a button over a card. The animation itself is functioning properly, but there is an issue where the content abruptly snaps to the top when the element mounts due to its ...

`Inconsistency between image and text alignment within Bootstrap buttons on my navigation bar`

After searching Stackoverflow without finding a solution to my issue, I decided to try something new Surprisingly, I tried using codepen <nav> <button class="btn btn-primary"> signed in as fermi <img src="https://ui-avatars.com/api/ ...

What is the process for testing bootstrap techniques in a node environment?

An issue arose with a React component where the componentDidMount() function includes the following line: $('[data-toggle="tooltip"]').tooltip(); During testing, an error message is displayed: TypeError: $(...).tooltip is not a function The ...

Customizable column layout in react-grid-layout

I am looking to implement a drag and drop feature in which users can place items into a grid without fixed columns. The goal is that if an item is dragged from outside the grid boundaries and dropped to the right (an empty area), the grid will automaticall ...

Generating dynamic HTML elements and incorporating animated positioning

Alright, folks. I've got a bit of a tricky situation here. So, I have this file named "thedot." My PHP script reads the contents of this file and divides it into three sections, which we call dots. These dots are then displayed on the page in rows of ...

What is the method for dividing a card image in half using bootstrap/css?

I'm attempting to divide this image in half and display the designed area on a card. Here is a screenshot for reference, but I'm unsure of how to achieve it: https://i.sstatic.net/7nNO4.png Below is the code I have so far: <div class="card ...

Prevent elements from overlapping within a flexbox container

I've encountered an issue with resizing the window causing the logos to overlap the portrait image. Is there a way to fix the logos relative to the portrait? Below is the code: /* Defaults for Main Part of Pages */ body, div, h1, p { margin: 0; ...

Deliver static image to React Native application using Express

I'm looking to deliver a static image from my Node.js Express server to my React Native application. I attempted to utilize express.static, but encountered the following error: Failed prop type: Invalid prop source supplied to Image. How can I su ...

How can you incorporate a horizontal line beneath your text, similar to the one shown in the

https://i.sstatic.net/OzMVr.jpg I have a code snippet that needs a horizontal border similar to the one shown in the image <section class="about"> <div class="container"> <h1 class="text-center"> ...

Varied spacing between horizontal and vertical items in a list

The issue with the spacing between horizontal and vertical list items is perplexing. Despite having identical margin, padding, height, and width, they seem to display differently. While manual adjustment of margins or paddings can resolve this, I am curiou ...

Eliminate the option to locate columns in the column selector of the mui DataGridPro

Currently, I am creating a data table for a personal project. Utilizing the DataGridPro element from material ui has been quite satisfying, especially with the column selector feature. However, I wish to eliminate the "find column" section. Is it feasibl ...

Having trouble initializing a React app: npm gets stuck at "discovering 0 security issues"

When creating my react app using the command "create-react-app app-name", I noticed that npm reported "found 0 vulnerabilities". Despite this, my directory was successfully created with 2 files: package.json and package-lock.json, as well as a folder nam ...

Tips for preventing memory leaks in React Native applications

I have been struggling to address a memory leak issue in React Native. Despite following numerous tutorials, I have not been able to resolve it. Let me share my original code: useEffect(() => { const unsubscribe = navigation.addListener('focus ...

Concealing search components using JavaScript

I'm looking to hide elements that appear below the search field in the example code below. The code is sourced from W3: https://www.w3schools.com/howto/howto_js_filter_lists.asp. I only want the search elements to display when a user starts typing in ...

What is the best way to invoke a function within a component class from my App.tsx file?

I am a beginner in the world of React Native, currently working on creating a mobile app using Expo. My challenge lies in calling a function from a component class within my App.tsx file. I specifically do not want this function to be static because I nee ...

The background surpasses the size of the iframe

I am currently designing a Joomla website and have created a custom module that displays an 800x500 image. Additionally, I have another module with an iframe displaying video content from livestream.com. I am wondering if it is possible to use the larger ...

You cannot assign a type in react-chartjs-2

Currently, I am working on a project using React and TypeScript. However, I have encountered an error related to types in my code. Can someone guide me on how to resolve this issue? The 'position' variable is set as a string data type. For more ...

Tips for creating shaped images using clip-path

Can an image be clipped to match a specific shape using the clip-path CSS property? Original Image https://i.stack.imgur.com/rYeuk.jpg Desired Result with CSS https://i.stack.imgur.com/6FYfn.png ...