tc39 / proposal-dynamic-import

import() proposal for JavaScript
https://tc39.github.io/proposal-dynamic-import/
MIT License
1.87k stars 47 forks source link

proposal: reload and unload a dynamic module #81

Open Fenzland opened 4 years ago

Fenzland commented 4 years ago

Background

If we want to build a real time develop system. When you change your code, and fire the event or hook, the browser reload the module automatically. Now we can use the module system of Webpack, and it works well. But with ESModules, there is a big problem, a module will never be reloaded or unloaded. The best solution by now, is import the module dynamically, and when it changes, add a ?v=timestamp to load the module again. But by this, the old module still never unloaded, it cause memory leak.

Proposal

Add ways to reload and unload a dynamic module. When we reload a module during it loading, just return a new promise with the loading value, but not send a new request. When we unload a module, it will not be removed immediately, but only remove from module list, and the module object will be collected by GC like a normal object.

Questions

Only can reload or unload dynamic module? or can dynamically upload static modules?

Syntax suggestion

import('./dynamic-module.js');
import.reload('./dynamic-module.js'); // returns the same as previous
import.unload('./dynamic-module.js'); // returns Promise<Boolean> or maybe Promise<undefined>
pauldraper commented 4 years ago

But by this, the old module still never unloaded, it cause memory leak. only remove from module list

There is no module list, per se.