Split CSS files apart with code-splitting in create-react-app without the need to eject

I have implemented react-loadable to enable code-splitting in my application, as it has grown too large to manage without this feature. While the code-splitting is functioning properly, the CSS is being embedded within the new JavaScript chunks and loaded dynamically onto the page. Instead, I would like the CSS from each chunk to be in separate files, rather than being included in the JS files themselves. Is there a way to achieve this without having to eject and manually modify the Webpack configuration? I am hoping to avoid that if possible.

EDIT: After ejecting to inspect the actual Webpack configuration, I discovered the following comment which highlights the issue I am facing, but unfortunately does not provide a solution:

https://i.sstatic.net/VIruk.png

Please take note of the final sentence:

If you use code splitting, however, any async bundles will still use the "style" loader inside the async code so CSS from them won't be in the main CSS file.

Answer №1

It's my belief that the purpose of create-react-app is to provide convenient defaults for your project's build configuration and offer a foundational template for your application's project structure. The idea is to streamline the process of building your application without having to worry about setting up various build configurations like asset bundling with Webpack, JavaScript code transpilation with babel, and linting. While create-react-app is a helpful starting point for most web applications, it may not address specific performance needs that arise as your application scales. Just like following a workout or diet plan can lay the foundation for physical fitness, it's the individual customization that can make a significant difference in achieving optimal results. Similarly, actors, athletes, and others have personal coaches and dieticians to tailor their plans for success on an individual level.

  1. It's advisable to back up your source code on a version control system before making any changes to prevent any potential issues that may arise. Then, create a separate branch from the main branch to work on.

You have two options to consider:

  1. While most build configuration settings are contained within react-scripts node module, accessible in webpack.config.js, you could try editing this file directly without ejecting. Be cautious not to inadvertently alter any existing configurations. If needed, you can always revert back by reinstalling react-scripts. This approach allows you to experiment with customizations in a controlled environment.
  2. If customization requirements due to scalability demand it, you may need to eject from the default configurations provided by create-react-app. While this may seem daunting, it grants you the freedom to create a bespoke configuration tailored to your needs. Remember, create-react-app simply offers useful defaults, and ejecting puts you in full control of your project moving forward.

I wish you the best of luck in finding a solution that works for you. If you discover an elegant solution, please do share it with others. I'm open to being proven wrong :)

Answer №2

It appears that the issue lies with the version of create-react-app used to create the project, rather than with react-loadable. The problem seems to be related to the dynamic import() syntax and the lack of support for it in ExtractTextPlugin which is unable to find chunks from that syntax.

I conducted a test on my own machine using the latest version of create-react-app and found that the bundle is successfully created when loaded dynamically.

https://i.sstatic.net/LOQSW.png

For further information, you can refer to the issue regarding extract-text-webpack-plugin at: https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/455

Therefore, the recommended course of action is to transition to using MiniCssExtractPlugin, as it appears to be the only viable option for extracting the CSS in this scenario.

Edit: Please note that for those who may encounter this issue in the future, MiniCssExtractPlugin is not compatible with Webpack 3, necessitating an upgrade to version 4.

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

Is there a way to adjust the width of the datepicker on my device?

I've been attempting to adjust the width of the date picker, but I'm having trouble achieving it. Below is my code: import React from "react"; import ReactDOM from "react-dom"; import { DatePicker, RangeDatePicker } from &qu ...

Tips for updating an array in TypeScript with React:

Encountering an issue while updating my state on form submission in TypeScript. I am a newcomer to TS and struggling to resolve the error. enum ServiceTypeEnum { Replace = 'replace product', Fix = 'fix product', } interface IProduc ...

I require limitless onclick functionality, but unfortunately, transitions are not functioning as expected

I'm currently working on creating a dynamic photo gallery, but I've encountered a couple of issues that need to be addressed... The "onclick" function in my JavaScript only allows for a single click, whereas I need it to be able to handle mul ...

What is the best way to dynamically load content as it enters the viewport using JavaScript or jQuery?

I have implemented a stunning animation to the h1 element using a function, but now I want the animation to trigger only when the h1 element enters the viewport as the user scrolls down. Currently, the animation occurs as soon as the page is loaded, even ...

Is it necessary for HTML "buttons" to follow the same box-model as other elements?

I recently encountered an issue with the button element and the input element when using a type of button. In both Firefox and Chrome, I noticed a behavior that seems like a bug in recent releases. However, as form elements often have exceptions to W3 rule ...

CSS query: How to eliminate the extra space at the top of a webpage?

How can I eliminate the gray area visible here: The padding in style.css is currently set to: padding: 0; I have attempted to modify this by changing the following: #page { margin-top: 0; } I have tried variations such as: #page { margin-top: 5px !im ...

What causes the cleanup function in React hooks to be triggered upon reopening a previously closed tab?

There seems to be an issue with closing a tab and then undoing that action. This causes all the cleanup functions in the component to execute, resulting in the abortion of new fetches needed to load the component. This behavior is observed only on Safari ...

Transparent background with opaque text

I noticed that my div has a gray background with some opacity. <div className="absolute w-full h-full top-0 left-0 right-0 bottom-0 bg-slate-500 opacity-50 z-10"> <p className="relative top-1/2 px-8 text-center hea ...

Improved the nested Immutable function within the immutable.js library

What is the best way to update a specific item within a nested immutable Map without altering the entire tree? For example: var tree = Map({ branch1:Map([]), branch2:Map({ 1: Map({id:1,value:'hello'}) }), }); How can I efficiently update the ...

The drawbacks of Next.js do not appear to distinguish it from other server-side rendering frameworks

I am diving into SSR now and conducting research to guide my agency in choosing between Angular, React, or Vue as our primary F/E framework. It seems like React and Next are the frontrunners for us right now. I'm zeroing in on some drawbacks of using ...

What is the reason for the lack of overlap between types in an enum?

I'm having trouble understanding why TypeScript is indicating that my condition will always be false. This is because there is no type overlap between Action.UP | Action.DOWN and Action.LEFT in this specific scenario. You can view the code snippet and ...

Validating the similarity of classes with JQuery

Currently, I am working on creating a quiz game using HTML, CSS, JQuery, and potentially JavaScript. I am looking to implement an if statement to check if a dropped element is placed in the correct div (city). My approach involves utilizing classes to comp ...

Having trouble accessing a CSS file through HTML

Is there a reason why I am facing difficulties accessing main.css from app.html to enable Tailwind CSS? When I try putting it in the style brackets in the .svelte file itself, it throws an error. Can anyone explain why this is happening? <link rel= ...

I am experiencing difficulty deleting the node_modules folder from my react app. What steps should I take to successfully remove it from my system?

              After utilizing the create-react-app command to create a react app, I decided it was time to delete the application. However, when attempting to remove the entire folder, I encountered an issue where certain files within the node_modu ...

When the page is resized for mobile, the Bootstrap modal shifts downwards

I am encountering an issue with a modal that pops up on my webpage when clicked. It is perfectly centered in a browser view and smaller views. However, when I resize the page for phones, the modal shifts down the page and requires scrolling to see it. How ...

What are some methods for applying border styles to the first and last row of a table using Material UI?

In my current project, I am utilizing the combination of Material UI and React Table. Recently, I was asked to implement an expandable row feature, which I successfully added. Once the user expands the row, we need to display additional table rows based on ...

What is the best method for showcasing various content using a uniform accordion style in React?

What is the most efficient way to display various content within multiple accordions? view image description here This is the current approach I am taking in my project, where shipping information and delivery options will involve different textboxes, labe ...

How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises ...

Setting up the left-side menu with subcategories on admin-on-rest platform

Can anyone provide me with an example of setting up sub-menus in the admin-on-rest left-bar menu? Reference: ...

Tips on fixing image movement when scrolling and switching resolutions

I am currently working on developing my HTML/CSS portfolio. However, I am facing an issue where the images on the site move around whenever the scroll level or resolution size is changed. I want the images to remain in their respective blocks, displayed at ...