I am really struggling to find any good examples for typescript and code splitting.
There are babel plugins that cover this but really thin on the ground for typescript examples.
I am using react so I would like to use React.Lazy but I cannot find anything that covers the webpack code splitting side of things
I did find this old example but it uses the now deceased CommonsChunkPlugin:
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
minChunks: function (module) {
// this assumes your vendor imports exist in the node_modules directory
return module.context && module.context.includes("node_modules");
}
})
],
It uses react-loadable but obviously cannot use the babel plugin so the example is manually adding the magic webpack comments:
export const LoadableHello = Loadable({
loader: () => import(/* webpackChunkName: "hello" */ "./Hello"),
loading: () => <div>loading ...</div>
});
Can anyone help me understand:
- How can I setup codesplitting for dynamic imports in the webpack side of things:
- Can I use React.Lazy with typescript?
I think I need to make sure ts is not removing the comments.
React.lazyin a Typescript projects. It works fine!React.lazyand a dynamic import like:const Component = React.lazy(() => import('./Component'));assuming your React version is 16.6 +