Is it necessary to create a reference for a dynamic, lazy import that is used multiple times in a script? For example, if there are 2 event handlers importing different functions from the same module:
/* index.js */
window.addEventListener("click", (e) => {
import(/* webpackChunkName: "myChunk" */ "./my-module.js")
.then((module) => {
const result = module.stop();
console.log('result ', result)
})
});
// my-module.js
let value = 0;
export function start(){
value++
}
export function stop(){
console.log('stopping');
return value;
}
This piece of code appears to be working efficiently; the my-module script is only downloaded once, and the state remains available in subsequent calls. Is there an improved method for accomplishing this task? Should a reference be created to the module after its initial import? Are there any specific comments or techniques worth considering?