Is it possible to combine multiple components into one file?
💬 Questions and Help
Hey i am wondering whether there's a way to combine multiple loadable components into a single file.
What i tried is importing the components and creating a default export but didnt work :D
import A from './A';
import B from './B';
export default {
A,
B,
};
Not sure what else i could or if this is more of a webpack question! In any case any help very much appreciated!
Hey @oorestisime :wave:, Thank you for opening an issue. We'll get back to you as soon as we can. Please, consider supporting us on Open Collective. We give a special attention to issues opened by backers. If you use Loadable at work, you can also ask your company to sponsor us :heart:.
This might be more of a Webpack question, indeed. You can use comments as hints for Webpack to determine the chunk names, as documented here: https://webpack.js.org/guides/code-splitting/
The hint works like this:
import(/* webpackChunkName: "mychunkname" */ 'myfile.js')
Or within a loadable call:
const component = loadable(() => import(/* webpackChunkName: "mychunkname" */ 'myfile.js'));
Now, when you want to have another file bundled in the same chunk, you can just give it the same name:
const componentOne = loadable(() => import(/* webpackChunkName: "mychunkname" */ 'myOneFile.js'));
const componentTwo = loadable(() => import(/* webpackChunkName: "mychunkname" */ 'mySecondFile.js'));
Both these files should now be placed inside the 'mychunkname' chunk.
Yep. This works. Probably giving you the best control upon code distribution - you can easily move code between chunks just changing comments.
Another solution is to use named imports (and exports) - https://loadable-components.com/docs/api-loadable-component/#optionsresolvecomponent