UNPKG

2.12 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var RHLPackage="react-hot-loader",RHLRootPackage="react-hot-loader/root",RHLPackages=[RHLPackage,RHLRootPackage];function isImportedFromPackages(e,t,a){var r=e.scope.getBinding(t),o=r&&r.path.node.type;if("ImportSpecifier"===o||"ImportNamespaceSpecifier"===o){var n=r.path.parent;return a.includes(n.source.value)}return!1}function isImportedFromRHL(e,t){return isImportedFromPackages(e,t,[RHLPackage])}function isImportedFromRHLRoot(e,t){return isImportedFromPackages(e,t,[RHLRootPackage])}function getRHLContext(e){for(var t=[],a=e.ast.program.body,r=0;r<a.length;r++){var o=a[r],n=o.source,l=o.specifiers;if("ImportDeclaration"===o.type&&RHLPackages.includes(n.value))for(var i=0;i<l.length;i++){var c=l[i];if("ImportNamespaceSpecifier"===c.type)t.push({kind:"namespace",local:c.local.name});else if("ImportSpecifier"===c.type){var p={kind:"named",local:c.local.name};"hot"===c.imported.name&&t.push(p)}}}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}if(e.node.callee.name===a.local&&isImportedFromRHLRoot(e,a.local)&&"CallExpression"===e.type&&e.node.arguments[0]&&"Identifier"===e.node.arguments[0].type){e.replaceWith(e.node.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;