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 [email protected] to no avail.
In the code which did work, the app was bootstrapped using [email protected] (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

18
  • so what exactly is the problem? I don't think anyone has time to create an Angluar-CLI project for you Commented Nov 2, 2017 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) Commented Nov 3, 2017 at 7: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? Commented Nov 3, 2017 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.. Commented Nov 6, 2017 at 6: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? Commented Nov 6, 2017 at 6:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.