2

I would like to import Angular4 modules dynamically from a remote server. This is to create a system that works with a plugins architecture. It is therefore important that the 'host app' shouldn't have to declare these modules in it's EntryComponents. I've managed to get the results I want, using this guide:

https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e

and the code provided at
https://github.com/maximusk/Here-is-what-you-need-to-know-about-dynamic-components-in-Angular

with some minor changes which then loads dependencies from the same server.

My problem comes in when I try to migrate this logic to my Anglular-CLI app and so webpack comes into play.

Posts and articles I've looked at includes (which were the most useful so far);
- Get and load a remote component/module in an angular 4.4 app
- How to load dynamic external components into Angular application

If anyone can help me to achieve the following, I would consider this a win:

  1. Create a new Angular Cli project
  2. Dynamically import a module (component) from a remote destination (not the same app), preferably that could have a dependency to another module
  3. Render it in app.component.ts

If any special versions are used, please list them as well.
I've been using the latest Angular4 cli and SystemJS@0.20.19 to no avail.
In the code which did work, the app was bootstrapped using SystemJS@0.19.39 (Although I also couldn't get this to work in the CLI basic app)

EDIT:

Using these steps:
ng new stack-overflow-example
npm install systemjs (this installs the latest version)
ng g dynamic-host
copy "fetch"-code as it worked in systemjs bootstrapped app - see below
declare var SystemJS; at top of dynamic-host component

It goes into the SystemJS import function but never triggers the .then nor does it output anything further. I've tried with systemjs v0.19.39 as well, then I get a
404 - http://localhost:4200/traceur
error.

I tried diving further into SystemJS, but I knowledge is a bit too limited to make any sense of what I see.

  public fetchModule() {
    return new Promise((resolve, reject) => {
      console.info(`[${this.constructor.name}] - Before import`);
      SystemJS.import('https:/some-remote-server/some-remote-module.module.ts')
        .then((_module) => {
          // never gets to this point
          console.info(`[${this.constructor.name}] - After import - then`);
        })
        .catch((err) => {
          reject(err);
          console.info(`[${this.constructor.name}] - After import - catch`);
        });
    });
  }

I've been struggling with this for quite some time, so whatever is unclear or should you need any more information, please let me know. I'm really keen to get this working on my side.

EDIT 2:
Acting on your feedback, I've created a clean 'plugin' which is just a clean angular module exporting a component which displays something in the html.
I've tried both 'tsc' and 'node_modules/.bin/ngc -p tsconfig-aot.json', with both methods logging the result

[DynamicHostComponent] - After import - catch
Error: Unable to dynamically transpile ES module
A loader plugin needs to be configured via SystemJS.config({ transpiler: 'transpiler-module' }).
upon trying to:

SystemJS.import('some-server/widget-without-deps/index.js')

As you can see, it at least now comes out of the System.import..

These are the files on the server using ngc
These are the files on the server using tsc

  • so what exactly is the problem? I don't think anyone has time to create an Angluar-CLI project for you – Max Koretskyi Nov 02 '17 at 14:47
  • I've added more information as to what I've done. As to what the problem is exactly, it either does nothing without any feedback, or it gives errors which I cannot make much sense of (as per the the edit) – Ivor Thord-Gray Nov 03 '17 at 07:31
  • so you get this logged `console.info(`[${this.constructor.name}] - Before import`);`, correct? and when `SystemJS.import('https:/some-remot...` is executed do you see a request in `Network` tab? – Max Koretskyi Nov 03 '17 at 11:40
  • That is correct. 'Before import' is logged, 'After import' not. In the network tab, the module seems to be fetched. I have the ts file in there with success code 200 after it. It doesn't seem to fetch the dependencies though as it does with your example (which I got working with this same files/modules). It feels like it doesn't transpile properly as I should load 'plugin-typescript', which I cannot seem to get right. That is just something I've found however and don't know if that is still on the correct path or not.. – Ivor Thord-Gray Nov 06 '17 at 06:14
  • My example loads already transpiled files, I don't do transpilation on client side. Is that the case for you? Also no errors in the consoe? – Max Koretskyi Nov 06 '17 at 06:45
  • I've been struggling with this for so long, not quite sure what I tried anymore. I'll give this a shot again and just get back to you. (Knowing what to expect already helps a lot). Anything specific that it needs to be transpiled as? Umd? Bundled? Do I need to have the ngfactory files in the same directory? – Ivor Thord-Gray Nov 06 '17 at 10:18
  • you said `ngfactory` files, are you trying to load AOT compiled files? just make sure that you load `.js` files using SystemJS, not `.ts` – Max Koretskyi Nov 06 '17 at 10:42
  • I've edited my question in order to log images. There are actually no ngfactory files. One of the transpiling methods are infact ngc, however I tried doing a clean tsc as well. See Edit 2 for a more comprehensive description – Ivor Thord-Gray Nov 06 '17 at 14:37
  • I might be coming right.. I seem to be able to load files now and they are calling each's dependencies. Problem I'm facing now is that the @angular/dependency is not being found at http://localhost:4200/@angular/core. I've tried specifying "paths": {"@angular/core": ["../node_modules/@angular/core"]} in tsconfig.json, but it doesn't seem to pick up there. Any ideas? – Ivor Thord-Gray Nov 07 '17 at 05:42
  • you need to configure systemjs, see [this configuration](https://github.com/maximusk/angular-seed/blob/master/systemjs.config.js) – Max Koretskyi Nov 07 '17 at 05:57
  • Still getting that issue. The reasoning behind it as I understand it is; The app compiles and then webpack bundles everything. When systemJS imports the module, it asks for @angular/*, which I need to map to node_modules. Because webpack has bundled everything, there is no node_modules available anymore, hence the error. So either I need webpack to not bundle everything, or I need a way for systemJS to get it in the bundled.js files. I believe this is a core of the problem between webpack and systemJS that I have. – Ivor Thord-Gray Nov 07 '17 at 08:17
  • `node_modules` are still available... sorry, it seems that you need to read more about the technologies you use, I can't seem to help you with so many questions – Max Koretskyi Nov 07 '17 at 09:13
  • Thanks for the effort – Ivor Thord-Gray Nov 07 '17 at 09:24
  • no problem, come back with more specific questions :) – Max Koretskyi Nov 07 '17 at 09:52
  • @IvorThord-Gray did you get some results? The only way I have managed to load external modules in angular 4/5 has been configuring the application itself with systemjs, not with webpack or angular-cli. Then System.load works perfectly for a library umd.js Do you have new results or advances? – Dani Andújar Nov 15 '17 at 15:39
  • @user3757628 - Unfortunately not :( Tried many approaches and each time running into some wall or other. Only way I got it working thus far is as you mentioned. – Ivor Thord-Gray Nov 16 '17 at 08:26
  • @IvorThord-Gray thansk for you response. When you get something new that works, would you mind to share it? – Dani Andújar Nov 16 '17 at 08:50
  • I have a reproduction here: https://github.com/thyb/repro-dynamic-loading with the same configuration (except it's an electron application which load local files dynamically instead of remote files). Blocked by StaticInjectorError as you can see here: https://stackoverflow.com/questions/47342589/angular5-dynamic-component-loading-containing-material-component-in-template – Thibaud Arnault Dec 05 '17 at 19:33

0 Answers0