Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uglify): workaround for reduce_funcs #1153

Merged
merged 1 commit into from
Jun 26, 2018

Conversation

wardpeet
Copy link
Member

@wardpeet wardpeet commented Jun 21, 2018

Fixes #1065

It's a small workaround for the VNode bug. When using a const in combination with the function. Uglify doesn't see this as a function so won't inline it.

Notice the VNode variable still being VNode.

I ran: printf '(function(){\n%s\nreturn preact;})()' "$(cat dist/preact.esm.js)" | sed '/export/d' | npx uglify-es --compress
before:

!function(){var options={},stack=[],EMPTY_CHILDREN=[];function h(nodeName,attributes){var lastSimple,child,simple,i,children=EMPTY_CHILDREN;for(i=arguments.length;i-- >2;)stack.push(arguments[i]);for(attributes&&null!=attributes.children&&(stack.length||stack.push(attributes.children),delete attributes.children);stack.length;)if((child=stack.pop())&&void 0!==child.pop)for(i=child.length;i--;)stack.push(child[i]);else"boolean"==typeof child&&(child=null),(simple="function"!=typeof nodeName)&&(null==child?child="":"number"==typeof child?child=String(child):"string"!=typeof child&&(simple=!1)),simple&&lastSimple?children[children.length-1]+=child:children===EMPTY_CHILDREN?children=[child]:children.push(child),lastSimple=simple;var p=new function(){};return p.nodeName=nodeName,p.children=children,p.attributes=null==attributes?void 0:attributes,p.key=null==attributes?void 0:attributes.key,void 0!==options.vnode&&options.vnode(p),p}function extend(obj,props){for(var i in props)obj[i]=props[i];return obj}var defer="function"==typeof Promise?Promise.resolve().then.bind(Promise.resolve()):setTimeout;var IS_NON_DIMENSIONAL=/acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i,items=[];function enqueueRender(component){!component._dirty&&(component._dirty=!0)&&1==items.push(component)&&(options.debounceRendering||defer)(rerender)}function rerender(){var p,list=items;for(items=[];p=list.pop();)p._dirty&&renderComponent(p)}function isNamedNode(node,nodeName){return node.normalizedNodeName===nodeName||node.nodeName.toLowerCase()===nodeName.toLowerCase()}function getNodeProps(vnode){var props=extend({},vnode.attributes);props.children=vnode.children;var defaultProps=vnode.nodeName.defaultProps;if(void 0!==defaultProps)for(var i in defaultProps)void 0===props[i]&&(props[i]=defaultProps[i]);return props}function removeNode(node){var parentNode=node.parentNode;parentNode&&parentNode.removeChild(node)}function setAccessor(node,name,old,value,isSvg){if("className"===name&&(name="class"),"key"===name);else if("ref"===name)old&&old(null),value&&value(node);else if("class"!==name||isSvg)if("style"===name){if(value&&"string"!=typeof value&&"string"!=typeof old||(node.style.cssText=value||""),value&&"object"==typeof value){if("string"!=typeof old)for(var i in old)i in value||(node.style[i]="");for(var i in value)node.style[i]="number"==typeof value[i]&&!1===IS_NON_DIMENSIONAL.test(i)?value[i]+"px":value[i]}}else if("dangerouslySetInnerHTML"===name)value&&(node.innerHTML=value.__html||"");else if("o"==name[0]&&"n"==name[1]){var useCapture=name!==(name=name.replace(/Capture$/,""));name=name.toLowerCase().substring(2),value?old||node.addEventListener(name,eventProxy,useCapture):node.removeEventListener(name,eventProxy,useCapture),(node._listeners||(node._listeners={}))[name]=value}else if("list"!==name&&"type"!==name&&!isSvg&&name in node){try{node[name]=null==value?"":value}catch(e){}null!=value&&!1!==value||"spellcheck"==name||node.removeAttribute(name)}else{var ns=isSvg&&name!==(name=name.replace(/^xlink:?/,""));null==value||!1===value?ns?node.removeAttributeNS("http://www.w3.org/1999/xlink",name.toLowerCase()):node.removeAttribute(name):"function"!=typeof value&&(ns?node.setAttributeNS("http://www.w3.org/1999/xlink",name.toLowerCase(),value):node.setAttribute(name,value))}else node.className=value||""}function eventProxy(e){return this._listeners[e.type](options.event&&options.event(e)||e)}var mounts=[],diffLevel=0,isSvgMode=!1,hydrating=!1;function flushMounts(){for(var c;c=mounts.pop();)options.afterMount&&options.afterMount(c),c.componentDidMount&&c.componentDidMount()}function diff(dom,vnode,context,mountAll,parent,componentRoot){diffLevel++||(isSvgMode=null!=parent&&void 0!==parent.ownerSVGElement,hydrating=null!=dom&&!("__preactattr_"in dom));var ret=idiff(dom,vnode,context,mountAll,componentRoot);return parent&&ret.parentNode!==parent&&parent.appendChild(ret),--diffLevel||(hydrating=!1,componentRoot||flushMounts()),ret}function idiff(dom,vnode,context,mountAll,componentRoot){var out=dom,prevSvgMode=isSvgMode;if(null!=vnode&&"boolean"!=typeof vnode||(vnode=""),"string"==typeof vnode||"number"==typeof vnode)return dom&&void 0!==dom.splitText&&dom.parentNode&&(!dom._component||componentRoot)?dom.nodeValue!=vnode&&(dom.nodeValue=vnode):(out=document.createTextNode(vnode),dom&&(dom.parentNode&&dom.parentNode.replaceChild(out,dom),recollectNodeTree(dom,!0))),out.__preactattr_=!0,out;var nodeName,node,vnodeName=vnode.nodeName;if("function"==typeof vnodeName)return function(dom,vnode,context,mountAll){var c=dom&&dom._component,originalComponent=c,oldDom=dom,isDirectOwner=c&&dom._componentConstructor===vnode.nodeName,isOwner=isDirectOwner,props=getNodeProps(vnode);for(;c&&!isOwner&&(c=c._parentComponent);)isOwner=c.constructor===vnode.nodeName;c&&isOwner&&(!mountAll||c._component)?(setComponentProps(c,props,3,context,mountAll),dom=c.base):(originalComponent&&!isDirectOwner&&(unmountComponent(originalComponent),dom=oldDom=null),c=createComponent(vnode.nodeName,props,context),dom&&!c.nextBase&&(c.nextBase=dom,oldDom=null),setComponentProps(c,props,1,context,mountAll),dom=c.base,oldDom&&dom!==oldDom&&(oldDom._component=null,recollectNodeTree(oldDom,!1)));return dom}(dom,vnode,context,mountAll);if(isSvgMode="svg"===vnodeName||"foreignObject"!==vnodeName&&isSvgMode,vnodeName=String(vnodeName),(!dom||!isNamedNode(dom,vnodeName))&&(nodeName=vnodeName,(node=isSvgMode?document.createElementNS("http://www.w3.org/2000/svg",nodeName):document.createElement(nodeName)).normalizedNodeName=nodeName,out=node,dom)){for(;dom.firstChild;)out.appendChild(dom.firstChild);dom.parentNode&&dom.parentNode.replaceChild(out,dom),recollectNodeTree(dom,!0)}var fc=out.firstChild,props=out.__preactattr_,vchildren=vnode.children;if(null==props){props=out.__preactattr_={};for(var a=out.attributes,i=a.length;i--;)props[a[i].name]=a[i].value}return!hydrating&&vchildren&&1===vchildren.length&&"string"==typeof vchildren[0]&&null!=fc&&void 0!==fc.splitText&&null==fc.nextSibling?fc.nodeValue!=vchildren[0]&&(fc.nodeValue=vchildren[0]):(vchildren&&vchildren.length||null!=fc)&&function(dom,vchildren,context,mountAll,isHydrating){var j,c,f,vchild,child,originalChildren=dom.childNodes,children=[],keyed={},keyedLen=0,min=0,len=originalChildren.length,childrenLen=0,vlen=vchildren?vchildren.length:0;if(0!==len)for(var i=0;i<len;i++){var _child=originalChildren[i],props=_child.__preactattr_,key=vlen&&props?_child._component?_child._component.__key:props.key:null;null!=key?(keyedLen++,keyed[key]=_child):(props||(void 0!==_child.splitText?!isHydrating||_child.nodeValue.trim():isHydrating))&&(children[childrenLen++]=_child)}if(0!==vlen)for(var i=0;i<vlen;i++){vchild=vchildren[i],child=null;var key=vchild.key;if(null!=key)keyedLen&&void 0!==keyed[key]&&(child=keyed[key],keyed[key]=void 0,keyedLen--);else if(min<childrenLen)for(j=min;j<childrenLen;j++)if(void 0!==children[j]&&(node=c=children[j],hydrating=isHydrating,"string"==typeof(vnode=vchild)||"number"==typeof vnode?void 0!==node.splitText:"string"==typeof vnode.nodeName?!node._componentConstructor&&isNamedNode(node,vnode.nodeName):hydrating||node._componentConstructor===vnode.nodeName)){child=c,children[j]=void 0,j===childrenLen-1&&childrenLen--,j===min&&min++;break}child=idiff(child,vchild,context,mountAll),f=originalChildren[i],child&&child!==dom&&child!==f&&(null==f?dom.appendChild(child):child===f.nextSibling?removeNode(f):dom.insertBefore(child,f))}var node,vnode,hydrating;if(keyedLen)for(var i in keyed)void 0!==keyed[i]&&recollectNodeTree(keyed[i],!1);for(;min<=childrenLen;)void 0!==(child=children[childrenLen--])&&recollectNodeTree(child,!1)}(out,vchildren,context,mountAll,hydrating||null!=props.dangerouslySetInnerHTML),function(dom,attrs,old){var name;for(name in old)attrs&&null!=attrs[name]||null==old[name]||setAccessor(dom,name,old[name],old[name]=void 0,isSvgMode);for(name in attrs)"children"===name||"innerHTML"===name||name in old&&attrs[name]===("value"===name||"checked"===name?dom[name]:old[name])||setAccessor(dom,name,old[name],old[name]=attrs[name],isSvgMode)}(out,vnode.attributes,props),isSvgMode=prevSvgMode,out}function recollectNodeTree(node,unmountOnly){var component=node._component;component?unmountComponent(component):(null!=node.__preactattr_&&node.__preactattr_.ref&&node.__preactattr_.ref(null),!1!==unmountOnly&&null!=node.__preactattr_||removeNode(node),removeChildren(node))}function removeChildren(node){for(node=node.lastChild;node;){var next=node.previousSibling;recollectNodeTree(node,!0),node=next}}var components={};function createComponent(Ctor,props,context){var inst,list=components[Ctor.name];if(Ctor.prototype&&Ctor.prototype.render?(inst=new Ctor(props,context),Component.call(inst,props,context)):((inst=new Component(props,context)).constructor=Ctor,inst.render=doRender),list)for(var i=list.length;i--;)if(list[i].constructor===Ctor){inst.nextBase=list[i].nextBase,list.splice(i,1);break}return inst}function doRender(props,state,context){return this.constructor(props,context)}function setComponentProps(component,props,renderMode,context,mountAll){component._disable||(component._disable=!0,component.__ref=props.ref,component.__key=props.key,delete props.ref,delete props.key,void 0===component.constructor.getDerivedStateFromProps&&(!component.base||mountAll?component.componentWillMount&&component.componentWillMount():component.componentWillReceiveProps&&component.componentWillReceiveProps(props,context)),context&&context!==component.context&&(component.prevContext||(component.prevContext=component.context),component.context=context),component.prevProps||(component.prevProps=component.props),component.props=props,component._disable=!1,0!==renderMode&&(1!==renderMode&&!1===options.syncComponentUpdates&&component.base?enqueueRender(component):renderComponent(component,1,mountAll)),component.__ref&&component.__ref(component))}function renderComponent(component,renderMode,mountAll,isChild){if(!component._disable){var rendered,inst,cbase,props=component.props,state=component.state,context=component.context,previousProps=component.prevProps||props,previousState=component.prevState||state,previousContext=component.prevContext||context,isUpdate=component.base,nextBase=component.nextBase,initialBase=isUpdate||nextBase,initialChildComponent=component._component,skip=!1,snapshot=previousContext;if(component.constructor.getDerivedStateFromProps&&(previousState=extend({},previousState),component.state=extend(state,component.constructor.getDerivedStateFromProps(props,state))),isUpdate&&(component.props=previousProps,component.state=previousState,component.context=previousContext,2!==renderMode&&component.shouldComponentUpdate&&!1===component.shouldComponentUpdate(props,state,context)?skip=!0:component.componentWillUpdate&&component.componentWillUpdate(props,state,context),component.props=props,component.state=state,component.context=context),component.prevProps=component.prevState=component.prevContext=component.nextBase=null,component._dirty=!1,!skip){rendered=component.render(props,state,context),component.getChildContext&&(context=extend(extend({},context),component.getChildContext())),isUpdate&&component.getSnapshotBeforeUpdate&&(snapshot=component.getSnapshotBeforeUpdate(previousProps,previousState));var toUnmount,base,childComponent=rendered&&rendered.nodeName;if("function"==typeof childComponent){var childProps=getNodeProps(rendered);(inst=initialChildComponent)&&inst.constructor===childComponent&&childProps.key==inst.__key?setComponentProps(inst,childProps,1,context,!1):(toUnmount=inst,component._component=inst=createComponent(childComponent,childProps,context),inst.nextBase=inst.nextBase||nextBase,inst._parentComponent=component,setComponentProps(inst,childProps,0,context,!1),renderComponent(inst,1,mountAll,!0)),base=inst.base}else cbase=initialBase,(toUnmount=initialChildComponent)&&(cbase=component._component=null),(initialBase||1===renderMode)&&(cbase&&(cbase._component=null),base=diff(cbase,rendered,context,mountAll||!isUpdate,initialBase&&initialBase.parentNode,!0));if(initialBase&&base!==initialBase&&inst!==initialChildComponent){var baseParent=initialBase.parentNode;baseParent&&base!==baseParent&&(baseParent.replaceChild(base,initialBase),toUnmount||(initialBase._component=null,recollectNodeTree(initialBase,!1)))}if(toUnmount&&unmountComponent(toUnmount),component.base=base,base&&!isChild){for(var componentRef=component,t=component;t=t._parentComponent;)(componentRef=t).base=base;base._component=componentRef,base._componentConstructor=componentRef.constructor}}for(!isUpdate||mountAll?mounts.unshift(component):skip||(component.componentDidUpdate&&component.componentDidUpdate(previousProps,previousState,snapshot),options.afterUpdate&&options.afterUpdate(component));component._renderCallbacks.length;)component._renderCallbacks.pop().call(component);diffLevel||isChild||flushMounts()}}function unmountComponent(component){options.beforeUnmount&&options.beforeUnmount(component);var base=component.base;component._disable=!0,component.componentWillUnmount&&component.componentWillUnmount(),component.base=null;var inner=component._component;inner?unmountComponent(inner):base&&(base.__preactattr_&&base.__preactattr_.ref&&base.__preactattr_.ref(null),component.nextBase=base,removeNode(base),function(component){var name=component.constructor.name;(components[name]||(components[name]=[])).push(component)}(component),removeChildren(base)),component.__ref&&component.__ref(null)}function Component(props,context){this._dirty=!0,this.context=context,this.props=props,this.state=this.state||{},this._renderCallbacks=[]}extend(Component.prototype,{setState:function(state,callback){var s=this.state;this.prevState||(this.prevState=extend({},s)),extend(s,"function"==typeof state?state(s,this.props):state),callback&&this._renderCallbacks.push(callback),enqueueRender(this)},forceUpdate:function(callback){callback&&this._renderCallbacks.push(callback),renderComponent(this,2)},render:function(){}})}();

after:

!function(){var VNode=function(){},options={},stack=[],EMPTY_CHILDREN=[];function h(nodeName,attributes){var lastSimple,child,simple,i,children=EMPTY_CHILDREN;for(i=arguments.length;i-- >2;)stack.push(arguments[i]);for(attributes&&null!=attributes.children&&(stack.length||stack.push(attributes.children),delete attributes.children);stack.length;)if((child=stack.pop())&&void 0!==child.pop)for(i=child.length;i--;)stack.push(child[i]);else"boolean"==typeof child&&(child=null),(simple="function"!=typeof nodeName)&&(null==child?child="":"number"==typeof child?child=String(child):"string"!=typeof child&&(simple=!1)),simple&&lastSimple?children[children.length-1]+=child:children===EMPTY_CHILDREN?children=[child]:children.push(child),lastSimple=simple;var p=new VNode;return p.nodeName=nodeName,p.children=children,p.attributes=null==attributes?void 0:attributes,p.key=null==attributes?void 0:attributes.key,void 0!==options.vnode&&options.vnode(p),p}function extend(obj,props){for(var i in props)obj[i]=props[i];return obj}var defer="function"==typeof Promise?Promise.resolve().then.bind(Promise.resolve()):setTimeout;var IS_NON_DIMENSIONAL=/acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i,items=[];function enqueueRender(component){!component._dirty&&(component._dirty=!0)&&1==items.push(component)&&(options.debounceRendering||defer)(rerender)}function rerender(){var p,list=items;for(items=[];p=list.pop();)p._dirty&&renderComponent(p)}function isNamedNode(node,nodeName){return node.normalizedNodeName===nodeName||node.nodeName.toLowerCase()===nodeName.toLowerCase()}function getNodeProps(vnode){var props=extend({},vnode.attributes);props.children=vnode.children;var defaultProps=vnode.nodeName.defaultProps;if(void 0!==defaultProps)for(var i in defaultProps)void 0===props[i]&&(props[i]=defaultProps[i]);return props}function removeNode(node){var parentNode=node.parentNode;parentNode&&parentNode.removeChild(node)}function setAccessor(node,name,old,value,isSvg){if("className"===name&&(name="class"),"key"===name);else if("ref"===name)old&&old(null),value&&value(node);else if("class"!==name||isSvg)if("style"===name){if(value&&"string"!=typeof value&&"string"!=typeof old||(node.style.cssText=value||""),value&&"object"==typeof value){if("string"!=typeof old)for(var i in old)i in value||(node.style[i]="");for(var i in value)node.style[i]="number"==typeof value[i]&&!1===IS_NON_DIMENSIONAL.test(i)?value[i]+"px":value[i]}}else if("dangerouslySetInnerHTML"===name)value&&(node.innerHTML=value.__html||"");else if("o"==name[0]&&"n"==name[1]){var useCapture=name!==(name=name.replace(/Capture$/,""));name=name.toLowerCase().substring(2),value?old||node.addEventListener(name,eventProxy,useCapture):node.removeEventListener(name,eventProxy,useCapture),(node._listeners||(node._listeners={}))[name]=value}else if("list"!==name&&"type"!==name&&!isSvg&&name in node){try{node[name]=null==value?"":value}catch(e){}null!=value&&!1!==value||"spellcheck"==name||node.removeAttribute(name)}else{var ns=isSvg&&name!==(name=name.replace(/^xlink:?/,""));null==value||!1===value?ns?node.removeAttributeNS("http://www.w3.org/1999/xlink",name.toLowerCase()):node.removeAttribute(name):"function"!=typeof value&&(ns?node.setAttributeNS("http://www.w3.org/1999/xlink",name.toLowerCase(),value):node.setAttribute(name,value))}else node.className=value||""}function eventProxy(e){return this._listeners[e.type](options.event&&options.event(e)||e)}var mounts=[],diffLevel=0,isSvgMode=!1,hydrating=!1;function flushMounts(){for(var c;c=mounts.pop();)options.afterMount&&options.afterMount(c),c.componentDidMount&&c.componentDidMount()}function diff(dom,vnode,context,mountAll,parent,componentRoot){diffLevel++||(isSvgMode=null!=parent&&void 0!==parent.ownerSVGElement,hydrating=null!=dom&&!("__preactattr_"in dom));var ret=idiff(dom,vnode,context,mountAll,componentRoot);return parent&&ret.parentNode!==parent&&parent.appendChild(ret),--diffLevel||(hydrating=!1,componentRoot||flushMounts()),ret}function idiff(dom,vnode,context,mountAll,componentRoot){var out=dom,prevSvgMode=isSvgMode;if(null!=vnode&&"boolean"!=typeof vnode||(vnode=""),"string"==typeof vnode||"number"==typeof vnode)return dom&&void 0!==dom.splitText&&dom.parentNode&&(!dom._component||componentRoot)?dom.nodeValue!=vnode&&(dom.nodeValue=vnode):(out=document.createTextNode(vnode),dom&&(dom.parentNode&&dom.parentNode.replaceChild(out,dom),recollectNodeTree(dom,!0))),out.__preactattr_=!0,out;var nodeName,node,vnodeName=vnode.nodeName;if("function"==typeof vnodeName)return function(dom,vnode,context,mountAll){var c=dom&&dom._component,originalComponent=c,oldDom=dom,isDirectOwner=c&&dom._componentConstructor===vnode.nodeName,isOwner=isDirectOwner,props=getNodeProps(vnode);for(;c&&!isOwner&&(c=c._parentComponent);)isOwner=c.constructor===vnode.nodeName;c&&isOwner&&(!mountAll||c._component)?(setComponentProps(c,props,3,context,mountAll),dom=c.base):(originalComponent&&!isDirectOwner&&(unmountComponent(originalComponent),dom=oldDom=null),c=createComponent(vnode.nodeName,props,context),dom&&!c.nextBase&&(c.nextBase=dom,oldDom=null),setComponentProps(c,props,1,context,mountAll),dom=c.base,oldDom&&dom!==oldDom&&(oldDom._component=null,recollectNodeTree(oldDom,!1)));return dom}(dom,vnode,context,mountAll);if(isSvgMode="svg"===vnodeName||"foreignObject"!==vnodeName&&isSvgMode,vnodeName=String(vnodeName),(!dom||!isNamedNode(dom,vnodeName))&&(nodeName=vnodeName,(node=isSvgMode?document.createElementNS("http://www.w3.org/2000/svg",nodeName):document.createElement(nodeName)).normalizedNodeName=nodeName,out=node,dom)){for(;dom.firstChild;)out.appendChild(dom.firstChild);dom.parentNode&&dom.parentNode.replaceChild(out,dom),recollectNodeTree(dom,!0)}var fc=out.firstChild,props=out.__preactattr_,vchildren=vnode.children;if(null==props){props=out.__preactattr_={};for(var a=out.attributes,i=a.length;i--;)props[a[i].name]=a[i].value}return!hydrating&&vchildren&&1===vchildren.length&&"string"==typeof vchildren[0]&&null!=fc&&void 0!==fc.splitText&&null==fc.nextSibling?fc.nodeValue!=vchildren[0]&&(fc.nodeValue=vchildren[0]):(vchildren&&vchildren.length||null!=fc)&&function(dom,vchildren,context,mountAll,isHydrating){var j,c,f,vchild,child,originalChildren=dom.childNodes,children=[],keyed={},keyedLen=0,min=0,len=originalChildren.length,childrenLen=0,vlen=vchildren?vchildren.length:0;if(0!==len)for(var i=0;i<len;i++){var _child=originalChildren[i],props=_child.__preactattr_,key=vlen&&props?_child._component?_child._component.__key:props.key:null;null!=key?(keyedLen++,keyed[key]=_child):(props||(void 0!==_child.splitText?!isHydrating||_child.nodeValue.trim():isHydrating))&&(children[childrenLen++]=_child)}if(0!==vlen)for(var i=0;i<vlen;i++){vchild=vchildren[i],child=null;var key=vchild.key;if(null!=key)keyedLen&&void 0!==keyed[key]&&(child=keyed[key],keyed[key]=void 0,keyedLen--);else if(min<childrenLen)for(j=min;j<childrenLen;j++)if(void 0!==children[j]&&(node=c=children[j],hydrating=isHydrating,"string"==typeof(vnode=vchild)||"number"==typeof vnode?void 0!==node.splitText:"string"==typeof vnode.nodeName?!node._componentConstructor&&isNamedNode(node,vnode.nodeName):hydrating||node._componentConstructor===vnode.nodeName)){child=c,children[j]=void 0,j===childrenLen-1&&childrenLen--,j===min&&min++;break}child=idiff(child,vchild,context,mountAll),f=originalChildren[i],child&&child!==dom&&child!==f&&(null==f?dom.appendChild(child):child===f.nextSibling?removeNode(f):dom.insertBefore(child,f))}var node,vnode,hydrating;if(keyedLen)for(var i in keyed)void 0!==keyed[i]&&recollectNodeTree(keyed[i],!1);for(;min<=childrenLen;)void 0!==(child=children[childrenLen--])&&recollectNodeTree(child,!1)}(out,vchildren,context,mountAll,hydrating||null!=props.dangerouslySetInnerHTML),function(dom,attrs,old){var name;for(name in old)attrs&&null!=attrs[name]||null==old[name]||setAccessor(dom,name,old[name],old[name]=void 0,isSvgMode);for(name in attrs)"children"===name||"innerHTML"===name||name in old&&attrs[name]===("value"===name||"checked"===name?dom[name]:old[name])||setAccessor(dom,name,old[name],old[name]=attrs[name],isSvgMode)}(out,vnode.attributes,props),isSvgMode=prevSvgMode,out}function recollectNodeTree(node,unmountOnly){var component=node._component;component?unmountComponent(component):(null!=node.__preactattr_&&node.__preactattr_.ref&&node.__preactattr_.ref(null),!1!==unmountOnly&&null!=node.__preactattr_||removeNode(node),removeChildren(node))}function removeChildren(node){for(node=node.lastChild;node;){var next=node.previousSibling;recollectNodeTree(node,!0),node=next}}var components={};function createComponent(Ctor,props,context){var inst,list=components[Ctor.name];if(Ctor.prototype&&Ctor.prototype.render?(inst=new Ctor(props,context),Component.call(inst,props,context)):((inst=new Component(props,context)).constructor=Ctor,inst.render=doRender),list)for(var i=list.length;i--;)if(list[i].constructor===Ctor){inst.nextBase=list[i].nextBase,list.splice(i,1);break}return inst}function doRender(props,state,context){return this.constructor(props,context)}function setComponentProps(component,props,renderMode,context,mountAll){component._disable||(component._disable=!0,component.__ref=props.ref,component.__key=props.key,delete props.ref,delete props.key,void 0===component.constructor.getDerivedStateFromProps&&(!component.base||mountAll?component.componentWillMount&&component.componentWillMount():component.componentWillReceiveProps&&component.componentWillReceiveProps(props,context)),context&&context!==component.context&&(component.prevContext||(component.prevContext=component.context),component.context=context),component.prevProps||(component.prevProps=component.props),component.props=props,component._disable=!1,0!==renderMode&&(1!==renderMode&&!1===options.syncComponentUpdates&&component.base?enqueueRender(component):renderComponent(component,1,mountAll)),component.__ref&&component.__ref(component))}function renderComponent(component,renderMode,mountAll,isChild){if(!component._disable){var rendered,inst,cbase,props=component.props,state=component.state,context=component.context,previousProps=component.prevProps||props,previousState=component.prevState||state,previousContext=component.prevContext||context,isUpdate=component.base,nextBase=component.nextBase,initialBase=isUpdate||nextBase,initialChildComponent=component._component,skip=!1,snapshot=previousContext;if(component.constructor.getDerivedStateFromProps&&(previousState=extend({},previousState),component.state=extend(state,component.constructor.getDerivedStateFromProps(props,state))),isUpdate&&(component.props=previousProps,component.state=previousState,component.context=previousContext,2!==renderMode&&component.shouldComponentUpdate&&!1===component.shouldComponentUpdate(props,state,context)?skip=!0:component.componentWillUpdate&&component.componentWillUpdate(props,state,context),component.props=props,component.state=state,component.context=context),component.prevProps=component.prevState=component.prevContext=component.nextBase=null,component._dirty=!1,!skip){rendered=component.render(props,state,context),component.getChildContext&&(context=extend(extend({},context),component.getChildContext())),isUpdate&&component.getSnapshotBeforeUpdate&&(snapshot=component.getSnapshotBeforeUpdate(previousProps,previousState));var toUnmount,base,childComponent=rendered&&rendered.nodeName;if("function"==typeof childComponent){var childProps=getNodeProps(rendered);(inst=initialChildComponent)&&inst.constructor===childComponent&&childProps.key==inst.__key?setComponentProps(inst,childProps,1,context,!1):(toUnmount=inst,component._component=inst=createComponent(childComponent,childProps,context),inst.nextBase=inst.nextBase||nextBase,inst._parentComponent=component,setComponentProps(inst,childProps,0,context,!1),renderComponent(inst,1,mountAll,!0)),base=inst.base}else cbase=initialBase,(toUnmount=initialChildComponent)&&(cbase=component._component=null),(initialBase||1===renderMode)&&(cbase&&(cbase._component=null),base=diff(cbase,rendered,context,mountAll||!isUpdate,initialBase&&initialBase.parentNode,!0));if(initialBase&&base!==initialBase&&inst!==initialChildComponent){var baseParent=initialBase.parentNode;baseParent&&base!==baseParent&&(baseParent.replaceChild(base,initialBase),toUnmount||(initialBase._component=null,recollectNodeTree(initialBase,!1)))}if(toUnmount&&unmountComponent(toUnmount),component.base=base,base&&!isChild){for(var componentRef=component,t=component;t=t._parentComponent;)(componentRef=t).base=base;base._component=componentRef,base._componentConstructor=componentRef.constructor}}for(!isUpdate||mountAll?mounts.unshift(component):skip||(component.componentDidUpdate&&component.componentDidUpdate(previousProps,previousState,snapshot),options.afterUpdate&&options.afterUpdate(component));component._renderCallbacks.length;)component._renderCallbacks.pop().call(component);diffLevel||isChild||flushMounts()}}function unmountComponent(component){options.beforeUnmount&&options.beforeUnmount(component);var base=component.base;component._disable=!0,component.componentWillUnmount&&component.componentWillUnmount(),component.base=null;var inner=component._component;inner?unmountComponent(inner):base&&(base.__preactattr_&&base.__preactattr_.ref&&base.__preactattr_.ref(null),component.nextBase=base,removeNode(base),function(component){var name=component.constructor.name;(components[name]||(components[name]=[])).push(component)}(component),removeChildren(base)),component.__ref&&component.__ref(null)}function Component(props,context){this._dirty=!0,this.context=context,this.props=props,this.state=this.state||{},this._renderCallbacks=[]}extend(Component.prototype,{setState:function(state,callback){var s=this.state;this.prevState||(this.prevState=extend({},s)),extend(s,"function"==typeof state?state(s,this.props):state),callback&&this._renderCallbacks.push(callback),enqueueRender(this)},forceUpdate:function(callback){callback&&this._renderCallbacks.push(callback),renderComponent(this,2)},render:function(){}})}();

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 3bb2d2d on wardpeet:bug/vnode-uglify into 8be9c5f on developit:master.

@developit
Copy link
Member

Nice find! Any idea if this increases the size?

@wardpeet
Copy link
Member Author

wardpeet commented Jun 25, 2018

used these commands to measure
npx bundlesize -f dist/preact.min.js -c false
npx bundlesize -f dist/preact.min.js

without fix: (current master)
dist/preact.min.js: 8.25KB
dist/preact.min.js: 3.42KB (gzip)

with fix:
dist/preact.min.js: 8.25KB
dist/preact.min.js: 3.41KB (gzip)

looks like it's a few bytes smaller. No idea why though 😄

i'll do a proper diff later today to see what uglify does with the output to see why it's 1kb less gzipped probably because of the name reuse VNode

@wardpeet
Copy link
Member Author

I added a fiddle just to make sure that preact-compat still works
https://jsfiddle.net/do1xvwe8/

@wardpeet
Copy link
Member Author

I did a diff but actually not much changed, just some var names, maybe that's why..

@developit
Copy link
Member

Amazing work.

@developit developit merged commit f5472e8 into preactjs:master Jun 26, 2018
@developit developit mentioned this pull request Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VNode being inlined by uglifyjs 3
4 participants