As you may know, in ECMAScript modules exports create a so called live view on a given variable. For example, when we change an exported variable some time later in the code, the change will be visible in modules that import it:
export let x = 1;
x = 2;
-----------
import { x } from "./x.mjs";
console.log(x) // logs 2
But things are a bit different with a default
export. A default
export is associated with a particular value, not a variable name. So when we do:
let x = 1;
export default x;
x = 2;
---------------------
import x from "./x.mjs";
console.log(x) // logs 1
We get the old value.
How to make default
export behave like named export, that is, how to enforce it to be a live view on a given variable?
Playground: glitch.com
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…