Multi-project build and dynamically loading module

2019-03-30 04:09发布

问题:

I have a potentially large Angular 2 application which we are going to split into multiple pieces (it can be considered as bundles within the WebPack terminology). Moreover we are going to build and deploy them separately. So that different pieces of the application can be dynamically loaded from the main application (not within the same web application - within multiple running services which can be on different physical hosts).

Webpack does bundling pretty fast and good. But as I know from the output it uses complicated internal format (using module numbers mapped to the actual paths/names). Also code splitting is done via some magic by mapping the module ids to chunk names which can be then dynamically loaded.

The problems here with webpack I see:

  1. If it uses numbers for building I cannot easily build separate pieces which can re-use some modules as the module numbers can be conflicted. I know about recordsPath but it's like a hack within multi-project system (As in this case I need to support and maintain the records for the whole system).
  2. I cannot simply load/import dynamically any external module using webpack (what I need to have it working like it's done in System.js - System.import('http://localhost:9900/data/index.js') or something like this. This actually doesn't work in webpack. I can emulate this by code splitting but that means it just separate some part of existing (not external) code and also you cannot do like this for external application.
  3. I cannot re-use the common modules easily in different projects. For ex. I can have module 'A' in the main project and can have projects p1 and p2 which needs to import 'A'. I could make 'A' as external but it can be simply some small service from the main application.

So any idea if it's possible using webpack1/2 or I should look at tools like jspm, System.js, browserify etc.? I like the way how webpack doing ts job (configuration it flexible, it works fast and it can bundle many things) but those issues make some complications for such use-cases.

Thanks in advance.