Aaron Meese
Aaron Meese

Reputation: 2213

How to dynamically import d3?

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?

Upvotes: 0

Views: 237

Answers (1)

Aaron Meese
Aaron Meese

Reputation: 2213

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!

Upvotes: 2

Related Questions