Skip to content

Instantly share code, notes, and snippets.

@benedictjohannes
Last active April 1, 2020 17:12
Show Gist options
  • Save benedictjohannes/33f93ccd2a66b9c150460c525937a8d3 to your computer and use it in GitHub Desktop.
Save benedictjohannes/33f93ccd2a66b9c150460c525937a8d3 to your computer and use it in GitHub Desktop.
Workaround to enable switching main entry point of Create-React-App by .env
// package.json
{
//...
"scripts": {
"startfirst": "REACT_APP_MAIN_COMPONENT=FirstMainComponent react-scripts start",
"buildfrst": "REACT_APP_MAIN_COMPONENT=FirstMainComponent react-scripts build",
"startsecond": "REACT_APP_MAIN_COMPONENT=SecondMainComponent react-scripts start",
"buildsecond": "REACT_APP_MAIN_COMPONENT=SecondMainComponent react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
//...
}
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
let {Suspense} = React
let appstring = './' + process.env.REACT_APP_MAIN_COMPONENT
const DynamicallyLoaded = React.lazy( () => import(`${appstring}`) )
const DynamicLoader = () => <div className='h100 w100'>
<Suspense fallback={<div>&nbsp;</div>}>
<DynamicallyLoaded/>
</Suspense>
</div>
ReactDOM.render(<DynamicLoader />, document.getElementById('root'));
serviceWorker.unregister();
// src/FirstMainComponent.js
// src/SecondMainComponent.js
@benedictjohannes
Copy link
Author

CRA devs did not pulled this commit which would have allowed switching main component of CRA.

Usiing React lazy loading, switching main entry point is an achievable workaround, albeit not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment