Heidi_DE Heidi_DE - 12 days ago 5
AngularJS Question

Angular 2 - moving modules to their own project

I am attempting to create portal like functionality with angular 2 which will provide base navigation and global services such as authentication, but allow other developers to create their own modules which fundamentally plug into the portal.

I am using angular-cli and have made a quick proof of concept with the modules being created inside the project (see tree below) which are lazy loaded in app.module via the router (with loadChildren). Each child module has its own router and is fundamentally decoupled from the parent portal (app.module).

I ultimately want to move these child modules into their own projects, but have absolutely no clue about where to start and there seems to be very little information relating to this online. If anyone knows of an example or can demonstrate how to set this up I´d be very grateful.

Edit: I would like to continue to use the Angular-CLI functionality to do this if it is possible.

Here is my directory structure. Module 1, 2 & 3 need to move into their own projects.

+-- app
│   +-- app.component.css
│   +-- app.component.html
│   +-- app.component.spec.ts
│   +-- app.component.ts
│   +-- app.module.ts
│   +-- module1
│   │   +-- dataflows
│   │   │   +-- dataflows.component.css
│   │   │   +-- dataflows.component.html
│   │   │   \-- dataflows.component.ts
│   │   +-- module1.component.css
│   │   +-- module1.component.html
│   │   +-- module1.component.ts
│   │   +-- module1.module.ts
│   │   \-- other
│   │   +-- other.component.css
│   │   +-- other.component.html
│   │   \-- other.component.ts
│   +-- index.ts
│   +-- module2
│   │   +-- module2.component.css
│   │   +-- module2.component.html
│   │   +-- module2.component.ts
│   │   \-- module2.module.ts
│   \-- module3
│   +-- dummy1
│   │   +-- dummy1.component.css
│   │   +-- dummy1.component.html
│   │   \-- dummy1.component.ts
│   +-- module3.component.css
│   +-- module3.component.html
│   +-- module3.component.ts
│   +-- module3.module.ts
│   \-- dummy2
│   +-- dummy2.component.css
│   +-- dummy2.component.html
│   \-- dummy2.component.ts
+-- index.html

Answer

I have finally figured out how to get this working using SystemJS instead of Webpack/Angular CLI.

I basically moved the modules into their own project and used "npm run tsc" using the outFile: property in the tsconfig.json file in the sub-project. You also have to set "module": "system" in the tsconfig.json file

I then manually copied the resulting compiled module in the root of the main project.

In order for the lazy loading to function I am using loadChildren: 'test.module' in the router configuration and adding:

bundles: {
    'test.module.js': ['test.module']
} 

to my systemjs.config.js

Hope this helps someone else who is trying to achieve this kind of setup.

Comments