N
N
Ninja Mate2016-04-07 07:55:48
JavaScript
Ninja Mate, 2016-04-07 07:55:48

How to set a name for a chunk using require.ensure?

The application is divided into chunks using require.ensure and the whole process is driven by the react router.
The folder with the index to which the request is made

module.exports = function(location, cb) {
    if (typeof require.ensure == 'function') {
        /* Asynchronous loading of a component
        that is inside of require.ensure */
        require.ensure([], (require) => {
            cb(null, require('./Reports'), 'reports')
        })
    } else {
        /* Server side synchronous loading */
        cb(null, require('./Reports'), 'reports');

    }
}

router
<Route  path="Reports" getComponent={
                                        (location,cb)=>require('./Reports/index')(location,cb)
                                        }> </Route>

webpack
output: {
    path: path.join(__dirna
    filename: 'bundle.js',
   chunkFilename: '[name].bundle.js' //в моем случае имя выглядит 1.bundle.js
  },

Normally
//если задавать так, то все идет по плану и файл получается reports.bundle.js
//как сделать эту магию в моем случае?
componentDidMount(){
        var Reports;
        require.ensure([], function(require) {
            Reports = require('./Reports').default;
            render(<Reports/>, document.getElementById('fl'));
        }, 'reports');
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2016-04-07
@victorzadorozhnyy

The chunk name is given in the third parameter of require.ensure . Try like this:

module.exports = function(location, cb) {
  if (typeof require.ensure == 'function') {
    require.ensure([], (require) => {
      cb(null, require('./Reports'))
    }, 'reports'); // третий параметр
  } else {
    cb(null, require('./Reports'));
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question