UNPKG

1.64 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var RHLPackage="react-hot-loader";function isImportedFromRHL(e,t){var a=e.scope.getBinding(t),r=a&&a.path.node.type;return("ImportSpecifier"===r||"ImportNamespaceSpecifier"===r)&&a.path.parent.source.value===RHLPackage}function getRHLContext(e){for(var t=[],a=e.ast.program.body,r=0;r<a.length;r++){var n=a[r],l=n.source,o=n.specifiers;if("ImportDeclaration"===n.type&&l.value===RHLPackage)for(var i=0;i<o.length;i++){var p=o[i];if("ImportNamespaceSpecifier"===p.type)t.push({kind:"namespace",local:p.local.name});else if("ImportSpecifier"===p.type){var c={kind:"named",local:p.local.name};"hot"===p.imported.name&&t.push(c)}}}return t.length?t:null}function plugin(){return{pre:function(){this.rhlContext=getRHLContext(this.file),this.rhlContext||(this.cancel=!0)},visitor:{CallExpression:function(e){if(!this.cancel)for(var t=0;t<this.rhlContext.length;t++){var a=this.rhlContext[t];if("named"===a.kind){if(e.node.callee.name===a.local&&isImportedFromRHL(e,a.local)&&"CallExpression"===e.parent.type&&e.parent.arguments[0]&&"Identifier"===e.parent.arguments[0].type){e.parentPath.replaceWith(e.parent.arguments[0]);break}}else if("namespace"===a.kind&&e.node.callee.callee&&"MemberExpression"===e.node.callee.callee.type&&"Identifier"===e.node.callee.callee.object.type&&e.node.callee.callee.object.name===a.local&&isImportedFromRHL(e,a.local)&&"Identifier"===e.node.callee.callee.property.type&&"hot"===e.node.callee.callee.property.name&&e.node.arguments[0]&&"Identifier"===e.node.arguments[0].type){e.replaceWith(e.node.arguments[0]);break}}}}}}exports.default=plugin;