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.
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.
source:https://ift.tt/3hlcMso
Comments
Post a Comment