Load JS modules conditionally (SystemJS)

Viewed 891

I am currently using jspm and SystemJS to load ES6 modules. However, I would like to be able to

  • Scan the page for certain selectors (e.g., id, data-plugin)

  • Map these selectors to their module dependencies

  • Load only those modules

My thought was to handle these imports through the single entry point, System.import('src/main'), that has access to the document. I could then find the relevant selectors, map those selectors to modules, and then import those modules.

src/main would look something like this:

['d3', 'jquery'].forEach(function(dependency) {
    import dependency;
});

This is not a viable solution as it is invalid syntax. Is there a better way to achieve dynamic module loading in this sense?

1 Answers
Related