question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Require.ensure doesn't seem to work, is it available in CRA?

See original GitHub issue

This 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:closed
  • Created 7 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Mar 31, 2017

Custom loaders like bundle-loader are not supported in CRA. Please use require.ensure() syntax directly.

0reactions
dmitrySheshkocommented, Mar 31, 2017

Try this:

  1. npm install bundle-loader --save-dev
  2. const Home = () => asyncComponent(‘bundle-loader!./C/HomePage.js’);
  3. function asyncComponent(cb) {…}
  4. componentWillMount() { cb((component) => { var Component = component.default; AsyncComponent.Component = Component; setState(() => ({ Component: Component })); }); }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found