tc39 / proposal-dynamic-import

import() proposal for JavaScript

Home Page:https://tc39.github.io/proposal-dynamic-import/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proposal: reload and unload a dynamic module

Fenzland opened this issue · comments

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>

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.