You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose you have a module which declared a module-scope variable:
constresource=[];
Later, you want the module to be hot-loadedable. To let it working , webpack suggests that you can:
// Use previous resource object, or create a new one.constresource=import.meta.hot.data['r']??[];// Save the data before module is about to reloadimport.meta.hot.dispose((data)=>voiddata['r']=resource);
I don't know about you, I personally feel it's trivial.
Now, if we got the planning const declarator, we can simply it into:
@hotReload(import.meta)// Oh, just one line, simplier and clear!constresource=[];
where, @hotReload can be implemented as:
exportfunctionhotReload(importMeta){return(initializer,context)=>{// context.kind --> like other decoarators// context.name --> the variable's name// initializer --> the variable's initializer functionif(context.kind!=='constDeclarator'){thrownewErrror(`@hotReload can only be attached to const declarator!`);}constvalue=importMeta.hot.data[context.name]??initializer();importMeta.hot.dispose((data)=>voiddata[context.name]=value);returnvalue;};}
The text was updated successfully, but these errors were encountered:
Have you heard webpack's Hot Module Reloading(HMR) tech?
Suppose you have a module which declared a module-scope variable:
Later, you want the module to be hot-loadedable. To let it working , webpack suggests that you can:
I don't know about you, I personally feel it's trivial.
Now, if we got the planning const declarator, we can simply it into:
where,
@hotReload
can be implemented as:The text was updated successfully, but these errors were encountered: