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?
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.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With