Create React App with Multiple Entry Points

The Create React App frameworks lets you easily build single page applications but it doesn’t support multiple entry points. To give you an example, if a website outputs separate home pages for mobile and desktop clients, the pages could be sharing some common React components between them, and it may thus not be practical to build two completely separate React applications.

Also see: Bundle React App with Gulp

CRA doesn’t support multiple entry points but there are couple of ways to solve this problem.

Option 1 Eject from the Create React App using the npm run eject command and update the entry inside webpack.config.js file to include multiple entry points.

Option 2 Use an alternate build tool like Vite.js that includes support for multiple entry points out of the box.

Option 3 Use the rewired app - it lets you easily make changes and small tweaks to the default Webpack configuration without ejecting the app.

Option 4 Use REACT_APP environment variables to specify the target component and then use ES5 dynamic imports to load the corresponding app component as shown in this example.

React Multiple Entry Points

Multiple Entry Points for Create React App

If you intend to use the Create React App configuration without ejecting it, here’s a simple workaround that will help you define multiple entry points and the output will be bundle in separate folders.

Inside the src folder, create two components.

// ./src/Desktop.js
import React from "react";

const Desktop = () => {
  return <h1>For Desktop</h1>;
};

export default Desktop;
// ./src/Mobile.js
import React from "react";

const Mobile = () => {
  return <h1>For Mobile</h1>;
};

export default Mobile;

The default entry file index.js looks something like this:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

Next, edit your package.json file and add commands, one per build target.

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "build:mobile": "cp src/Mobile.js src/App.js && npm run build && mv build build-mobile",
    "build:desktop": "cp src/Desktop.js src/App.js && npm run build && mv build build-desktop"
  }

Run npm run build:mobile when the build target is mobile or npm run build:desktop for the desktop entry point.

Packaging a React App With Spring Boot

In this article, we're going to discuss how to combine Create React App with Spring Boot to give you a single war file, so that we:

  • Can have our frontend and backend in a single war file with optimized productions build
  • And keep the benefits Create React App gives such as hot reloading

Setup

  1. Must have Java installed. Head over here to download a version.
  2. Must have Maven installed. For Mac, I used Homebrew (brew install maven), but otherwise, head here.
  3. Must have Node installed. For Mac, I used Homebrew (brew install node), but otherwise, head over here.

Note: my IDE of choice is IntelliJ. When working on react code, I usually switch over to VS Code. Feel free to use what makes you feel comfortable. 

How to Create React App in 5 Minutes

React.js is one of the most popular frontend frameworks nowadays. After mastering the theory behind React, you can only improve with time behind a keyboard. At first, building a React application may seem a little bit difficult, but creating your first application doesn’t have to be so complicated.

In this article, I’m going to create a simple React.js application with fetching data from an API in five minutes, step-by-step.