Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name webpack chunks from react-loadable

I've successfully added react-loadable library in my project to enable code splitting, the only problem I've found is that the chunks generated by webpack are not named, they are given integer names.

My code for react-loadable use is

const AppRootLoadable = Loadable({
  loader: () => import(/* webpackChunkName: "app" */ './App'),
  loading: () => null,
  render(loaded) {
    const Component = loaded.default;
    return <Component />;
  },
});

I've added the comment to tell webpack 3 that I want this chunk to be named app. Have I done something wrong?

like image 412
Khriz Avatar asked Oct 05 '17 11:10

Khriz


People also ask

What is the use of Webpack in react?

The React docs and Webpack docs have examples of this in its base form: This allows it to be handled asynchronously, requested at runtime from a user’s browser. When import () is used Webpack creates a new chunk for the imported code, splitting it from the main bundle.

What is the webpackchunkname hint for?

One of these is webpackChunkName, which can be used to give a meaningful name to the output chunk. For example, this import: Would result in this chunk: Without the webpackChunkName hint, we get a anonymous chunk ID which is difficult to identify:

What is vendor chunking in react?

Another strategy is Vendor chunking. This is an automatic strategy to pull out common node module dependencies into a ‘vendors’ chunk that is also used (and advised) but will not be covered here. It is enabled by default in Create React App, though a future post may look at how to customise this further.

What is code splitting or chunking in Webpack?

The answer is Code Splitting or Chunking i.e we can find a middle ground between these two things by increasing the number of requests while keeping them bundled. My discussion will follow my favourite bundling tool webpack. But this concept of chunking is there in all other bundlers like browserify, requirejs or almondjs.


1 Answers

Ok, after 4 days I found the solution. I needed to add the chunkFilename line to my webpack config:

output: {
  path: path.join(__dirname, './../public'),
  filename: 'bundle.js',
  publicPath: '/',
  chunkFilename: '[name].[chunkhash].js'
},

Then it works. I found it in the webpack github page


EDIT:

Addtional information for substitutions can be found in the webpack documentation for output.chunkFilename and for output.filename

like image 102
Khriz Avatar answered Oct 17 '22 21:10

Khriz