shan kulkarni shan kulkarni - 1 month ago 44
AngularJS Question

Angular2 cli (Webpack) creating chunks for different modules

I am working on an Angular2 application with large no of separate modules. The module specific files(js,css,html) are fetched only when module is created dynamically (...using moduleFactory and createComponent).

So I need to bundle my app in such a way that instead of having single bundle.js, I want each module as a separate chunk so that the chunk(js) is only fetched when the module is created.

how can I achieve this in angular cli ??
will CommonsChunkPlugin be helpful?

Answer

You can setup your project with webpack directly without using angular-cli and use require.ensure() to include external file dynamically. Webpack will detect require.ensure() and will create a separate chunk for it.