Require.ensure doesn't seem to work, is it available in CRA?
See original GitHub issueThis is based on acdlite quick & dirty code splitting with react-router 4:
You can reproduce with:
// ACDLiteComponent.js
import React from 'react';
function asyncComponent(pathToComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
const setState = this.setState.bind(this);
if (!this.state.Component) {
require.ensure([], () => {
var Component = require(pathToComponent).default;
AsyncComponent.Component = Component;
setState(() => ({ Component: Component }));
})
}
}
render() {
const { Component } = this.state;
if (!Component) {
return null;
}
return <Component {...this.props} />;
}
}
}
export default asyncComponent;
This is being used in index.js of CRA:
import React from 'react';
import Router from 'react-router/BrowserRouter';
import Match from 'react-router/Match';
import ReactDOM from 'react-dom';
import asyncComponent from './ACDLiteAsyncComponent';
const Home = () => asyncComponent('./C/HomePage.js');
const Gallery = () => asyncComponent('./C/GalleryPage.js');
ReactDOM.render(
<Router>
<div>
<Match exactly pattern="/" component={Home}/>
<Match pattern="/gallery" component={Gallery}/>
</div>
</Router>,
document.getElementById('root')
);
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Dependencies that use require.ensure for backward ...
Importing dependencies into an app created with create-react-app that use require.ensure as a fallback option causes the following warning ...
Read more >Employers' Guide – Payroll Deductions and Remittances
Payroll guide for employers, trustees, and payers who need information on deducting and remitting CPP, EI and income tax from amounts paid.
Read more >How does webpack's require.ensure work?
Webpack statically parses the source code and adds all modules mentioned in the first argument array and all require d within callback function...
Read more >The Congressional Review Act (CRA): Frequently Asked ...
The CRA does not define what would constitute a rule that is ... In the case of some major rules, it appears that...
Read more >Community Reinvestment Act (CRA)
CRA requires the FDIC to assess an institution's record of helping to meet the credit needs of the local communities in which the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Custom loaders like
bundle-loader
are not supported in CRA. Please userequire.ensure()
syntax directly.Try this: