Home > Blockchain >  How to dynamically import d3?
How to dynamically import d3?

Time:12-14

I know that modules can normally be dynamically imported with the following syntax (source):

if (condition) {
    import('something')
    .then((something) => {
       console.log(something.something);
    });
}

However, the d3 library has to be imported with this syntax, not the normal import syntax:

import * as d3 from "d3";

How do I combine the two of these to dynamically import the d3 library in an ES6 environment?

CodePudding user response:

Thanks to Andy, I discovered that my desired behavior is already happening behind the scenes!

It returns a promise which fulfills to an object containing all exports from moduleName, with the same shape as a namespace import (import * as name from moduleName): a sealed object with null prototype.

Hope this can help someone else, cheers!

  • Related