9

Is it possible to reference a module (already compile in umd or es format) and load it dynamically in a already compiled angular application?

  1. Main shell application hosted at: http://plugin_shell.mydomain.com
  2. A module (with a bunch of components, services, etc): compiled code is hosted at another url. let say: http://plugins/modules/myfirstplugin.umd.js
  3. When shell load. Load all modules, render specific components, refer to services, use class etc.

I tried to load the module with SystemJsNgModuleLoader.load, but it does seem to work with this kind of use cases.

Thanks

EDIT: Same question (no answer): How to dynamically load external angular 2 module (like served from an external module.bundle.js)

3
  • You're talking about a compiled ng application (aka webpack fragments) but you're also mentioning systemJS, which one are you using? Commented Aug 8, 2017 at 14:41
  • Well the shell is a webpack application. All the module, must be dynamic, so they are currently compiled as umd module with rollup.The problem here is that module will be updated and publish at an url. The shell must load them (and have the last version available) from the url. Commented Aug 8, 2017 at 14:44
  • github.com/webpack/webpack/issues/150 this might help you, but because I don't know how it can be done exactly, I won't post it as an answer. It seems like people achieved what you want to do on this issue. Commented Aug 8, 2017 at 14:46

1 Answer 1

3

You can do it like this:

@Component({
  providers: [
    {
      provide: NgModuleFactoryLoader,
      useClass: SystemJsNgModuleLoader
    }
  ]
})
export class ModuleLoaderComponent {
  constructor(private _injector: Injector,
              private loader: NgModuleFactoryLoader) {
  }

  ngAfterViewInit() {
    this.loader.load('app/t.module#TModule').then((factory) => {
      const module = factory.create(this._injector);
      const r = module.componentFactoryResolver;
      const cmpFactory = r.resolveComponentFactory(AComponent);

      // create a component and attach it to the view
      const componentRef = cmpFactory.create(this._injector);
      this.container.insert(componentRef.hostView);
    })
  }
}

Read Here is what you need to know about dynamic components in Angular for more details. Specifically Dynamic module loading and compilation section.

Sign up to request clarification or add additional context in comments.

35 Comments

but this code is good when module are baked in the application that was compiled. My shell is already compiled and module/component comes from another source entirely. For example I am trying to do in dev: const url: string = 'http://localhost:8082/core/core.umd.js'; System.import(url).then((module) => { ...
I have a similar requirement @ThePainnn were you able to solve this issue. if so please share the details. Thanks
@ThePainnn, Webpack can't do that, that's correct. I use SystemJS for that
@meno, no workaround because Webpack isn't designed to be a module loader. I use both webpack and SystemJS in my project.
@ANewGuyInTown, yes, it can load modules, but only those known at the build time. It can't load modules dynamically similar to how import() does it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.