Webpack supports the import() syntax that conforms to the ECMAScript proposal for dynamic imports. This syntax uses promises to asynchronously load modules.
The problem is that the promise is resolved as soon as the specific module is loaded, without waiting for the module's dependencies to load (which can be any type of asset, including JS & CSS).
Example code:
import('./myModule.js').then(myModule => {
myModule.sayHello(); // This will be called before someCSS.css has been loaded
});
myModule.js
import './someCSS.css'; // <-- I need to know when this is loaded (there can be more than one asset)
export default class myModule {
sayHello() {
alert('Hello!');
}
}
How can I detect when the module, and all related assets, have been loaded? Something like an onload
event for async assets?
The method returns Promise, which allows you to determine whether a script was loaded or an error occurred while loading (for example):