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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 ⋅ 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 ⋅ 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 ⋅ 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 ⋅ 5 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