If you've ever wanted to leverage import()
from the browser (or node process) within an application that uses webpack - you have probably realized that webpack "hijacks" the import()
function and replaces it with its own implementation. This is great for static imports, but what if you want to dynamically import a module that webpack doesn't know about at build time?
I was running into this limitation the other day, and couldn't really find any good resources out there that helped me to workaround the limitation.
To note - the limitation is there because webpack defaults to attempting to statically analyze your code and build a dependency graph. This is great for performance and tree shaking, but not so great when you want to dynamically import a module in your application at runtime!
The solution that I found was to use new Function
(an often overlooked but incredible utility) to build our own import()
method that webpack doesn't know about.
Now you can use funport
to dynamically import modules in your application!
Here's a quick example: