2023-07-24
2047
#create react app#react
Andrew James
19505
Jul 24, 2023 ⋅ 7 min read

Creating multiple entry points in Create React App without ejecting

Andrew James Frontend Engineer @ Coinbase

Recent posts:

Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 ⋅ 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 ⋅ 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 ⋅ 7 min read
Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 ⋅ 10 min read
View all posts

12 Replies to "Creating multiple entry points in Create React App without ejecting"

  1. This could be used to dynamically import assets (images, logos, backgrounds, fonts..) and bundle only that are conditionally used? Or maybe there is a better way to do that? Thank you!

    I come from Angular 2-4 that has a angular-cli.json file, and you could define multiple asset paths to be included from a parameter input called “app” (in latest angular versions “project”) and in React, this is managed by Webpack but if you start with CRA and don’t want to eject.. it’s a bit cumbersome. And this could be a solution.

  2. You are right. I did some tries and tests, and seems that if you do an image or asset conditionally import goes well (it’s not included in the final bundle), but if you try to with a JS file, it’s including anyway to the bundle. So, I think this post needs to be updated..

  3. Thanks for this! I was under the impression that the dynamic import would tree-shake the unused imports.

    I should have some time next week to update the post

  4. Great concept. But as others have posted build process is including all files in project regardless of the dynamic import entry file. Have you found a workaround?

  5. I’m additionally having the issue that css files from the non-included containers are being bundled in, causing clashes between styles.

  6. Nice article Andrew.

    It led me to look into React.lazy as an alternative that renders a dynamic import as a regular component.

    I’m pretty sure the React.lazy approach would work here (wrapped in a React Suspense with fallback) and only bundle in the dynamic import. This will likely resolve the issue people are having in these comments where the bundle currently contains both files?

    I’m not sure if a code snippet will work in this comment, but I’ll try:

    const BuildTarget = React.lazy(() => import(`./${process.env.REACT_APP_BUILD_TARGET}`));

    Reference: https://reactjs.org/docs/code-splitting.html#reactlazy

  7. Thanks a lot for this! I had been searching all day for a “monorep” solution. However, I was unable to get your final solution to work as I was hitting a compile error with Sass that I could not resolve. Instead I modified the last step to just use a switch statement:

    // To add a new app, import it here…
    import ExampleReactApp from “./apps/example-react-app/App”;
    import ImsApp from “./apps/ims/App”;

    // …then add a line to the switch statement below, ensuring the “name” exactly matches the entry in the .env file
    let app;
    switch (process.env.REACT_APP_BUILD_TARGET) {
    case “ims”:
    app = ;
    break;
    default:
    app = ;
    break;
    }

    ReactDOM.render({app}, document.getElementById(“root”));

  8. thank you for the PR, does that mean when the name is explicit, the import can be target. Otherwise it’ll try to include all of them? I’ll give it a shot, but seems making perfect sense.

Leave a Reply