Create-react-app: couldn't make require.ensure to work

Created on 12 Jan 2017  路  6Comments  路  Source: facebook/create-react-app

Hi there,
I'm trying to use dynamic routing with require.ensure but am unable to make it work. Here is my code snippet:

routes.js

import React from 'react';
import { Router, Route, browserHistory } from 'react-router';

import App from './App';

const componentRoutes = {
  component: App,
  path: '/',
  childRoutes: [
    {
      path: 'Login',
      getComponent: (a, cb) => require.ensure([], require => {cb(null, require('./Login'));})
    },
    {
      path: 'Report',
      getComponent: (a, cb) => require.ensure([], require => {cb(null, require('./Report'));})
    }
  ]
};

const Routes = () => {
  return (
    <Router history={browserHistory} routes={componentRoutes} />
  );
};

export default Routes;

There is neither error on the terminal nor on the Chrome's console. The page just renders the App component.

Am I missing something?

Most helpful comment

For example require('./Login') looks suspicious because if you use ES6 exports there, you likely want require('./Login').default instead.

All 6 comments

Can you create a standalone example reproducing this without RR?

Can you create a standalone example reproducing this without RR?

Sorry @gaearon I didn't get your question clearly?
I'm trying to use the Dynamic Routing feature in CRA as System.import isn't yet supported on CRA.

I understand, but can you create an example that doesn't use React Router but still reproduces the issue? I don't know React Router API well so it's hard for me to tell from an example like this if it's a problem with CRA or if you're just misusing RR API in some way.

For example require('./Login') looks suspicious because if you use ES6 exports there, you likely want require('./Login').default instead.

require('./Login').default did the job.

Thank you @gaearon

It's weird RR didn't throw an error on this. (Which is why I wanted to isolate it鈥攚asn't sure if RR handles such errors.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  路  3Comments

adrice727 picture adrice727  路  3Comments

Aranir picture Aranir  路  3Comments

stopachka picture stopachka  路  3Comments

barcher picture barcher  路  3Comments