3

I want to import angular modules dynamically with variable string routes from backend service. For example my backend service sends to me this response when app is starting(using APP_INITIALIZER).

{
    "hostname": "a-tenant",
    "modules": {
        "home": {
            "class": "HomeAModule",
            "path": "home-a.module",
         },
    },
},

My app structure is:

 project structure

So i want to import a module like this

const path = `./tenants/${response.hostname}/home/${response.modules.home.path}`;

import(path).then(m => m[response.modules.home.class]);

My final import code should be like this in runtime:

import('./tenants/a-tenant/home/home-a.module').then(m => m.HomeAModule);

But i'm getting this error, i think webpack does not handle dynamic loads like this.

Thank you for your help :)

Error

2
  • 2
    Did you ever find a solution to this? I"m in a similar situation: stackoverflow.com/questions/62304831/… Commented Jun 10, 2020 at 13:34
  • No unfortunately it is not possible. I am importing modules with static paths right now. Commented Jun 11, 2020 at 13:11

1 Answer 1

2

Unfortunately, using dynamic string for dynamic imports is impossible

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

1 Comment

Here is a good explanation of why... Dynamic imports are fantastic for code-splitting on a granular level. They allow us to provide lazy-loading boundaries in our application. In the same time, because of their dynamical nature, they often will enable us to sneak in code that requires runtime data for resolution of the imported module ... we should be extremely cautious because we limit the capabilities of the tools that we’re using. We sacrifice automatic bundling of lazy-loaded chunks, type inference, and much more.

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.