Webpack import module at runtime without knowing which during build

Viewed 205

I am building a jsFiddle-like application and I want the user to be able to enter the URL of a js module that I can then import in the runtime.

So somewhere in the code I do something like import(userModuleUrl).then(...)

I want to use webpack to bundle the app, but now webpack hijacks the original import ES6 function and is not very happy not knowing about the userModuleUrl at compile time.

I can’t use externals because I can’t know ahead of time all the urls the user may import.

I only wish there was a way for webpack to not interpret that line, or to give me a function that’s equivalent to the raw ES import

1 Answers

I ended up doing it this way:

const importFunc = new AsyncFunction('url','return import(url);');
const script = await importFunc(url);

you'll need to allow your app to run eval though... Which is fine for a jsFiddle-like app.

Related