UNPKG

babel-plugin-local-scoped-modules

Version:

babel-plugin-local-scoped-modules ================ This plugin intends to avoid typing relative paths.Use relative paths like local modules instead.

2 lines (1 loc) 3.41 kB
(function(c,l){typeof exports=="object"&&typeof module<"u"?module.exports=l(require("fs"),require("path")):typeof define=="function"&&define.amd?define(["fs","path"],l):(c=typeof globalThis<"u"?globalThis:c||self,c.babelPluginLocalScopedModules=l(c.fs,c.path))})(this,function(c,l){"use strict";class S{constructor(){this.cachedMethodNames=[],this.visitor={}}addMethod(t,i){let r=t;t in this.cachedMethodNames||(this.cachedMethodNames.push(t),this.visitor[t]=(n,s)=>i(n,s,r))}getVisitor(){return this.cachedMethodNames.length===0?{}:this.visitor}}const v={rootPrefix:"~",scopePrefix:"@",scopes:[],calleeNames:["require"]},E="."+l.sep;function y(e){return e=l.normalize(e),c.existsSync(e+".js")||c.existsSync(e+".json")||l.basename(e).indexOf(".")>-1?"file":"dir"}function N(e,t,i={}){e=l.normalize(e),t=l.normalize(t);let r,n,s,o,{isDir:u}=i;const d=y(e),x=u==!0?"dir":y(t);return r=e,d==="file"&&(r=l.dirname(e)),n=t,x==="file"&&(n=l.dirname(t),s=l.basename(t)),o=l.relative(r,n),n.endsWith(l.sep)&&(o+=l.sep),s&&(o=l.join(o,s)),n.indexOf(r)>-1&&(o=E+o),o.endsWith("..")&&(o+=l.sep),o}var T=/[|\\{}()[\]^$+*?.-]/g;const w=e=>{if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(T,"\\$&")},j={resolve:{},eject:{}},D="__THE_ITEM_WAS_EJECTED__",{resolve:J,eject:h}=j,M=e=>{e in h||(h[e]=D)};function P(e){let t=[];return e.forEach((i,r)=>{let{name:n,alias:s,dir:o}=i;t.push({name:n,dir:o}),s&&(Array.isArray(s)?s.forEach(u=>t.push({name:u,dir:o})):t.push({name:s,dir:o}))}),t}function g(e,t,i){const{cwd:r,filename:n}=t,{rootPrefix:s,scopePrefix:o}=i,u=e;if(u in h)return;let d=P(i.scopes);const x={name:s,dir:r};d.unshift(x);let m=k(e);if(!m){M(u);return}let O=V(),$=b(),C=m===s?"":$[m].slice(s.length),I=e.slice(m.length);return(O+l.normalize(C+I)).replace(l.sep+l.sep,l.sep);function V(){return N(n,r)}function k(a){if(!F(a))return;let f;return d.some(H=>{let p=H.name;return a===p||a.startsWith(`${p}/`)&&a.split(p).length===2?(f=p,!0):!1}),f}function F(a){let f=new RegExp(`^${w(o)}[-_0-9A-z/]+`);return a=s||a.startsWith(`${s}/`)&&a.split(s).length===2||f.test(a)&&a.split(o).length===2}function b(){return d.reduce((a,f)=>(a[f.name]=f.dir,a),{})}}const W=e=>{for(;e.type==="BinaryExpression";){let t=e.left;if(t.type==="BinaryExpression"){e=e.left;continue}if(t.type==="StringLiteral"&&e.operator==="+")return t;if(t.type==="TemplateLiteral"&&e.operator==="+")return t;break}},_=e=>(t,i)=>{if(e.type==="BinaryExpression"&&(e=W(e)),e.type==="CallExpression"&&(e=e.callee.object),!!e){if(e.type==="StringLiteral")return A(e,t,i);if(e.type==="TemplateLiteral")return L(e,t,i)}},A=(e,t,i)=>{let r=e.value,n=g(r,t,i);n&&(e.value=n)},L=(e,t,i)=>{e=e.quasis[0];const{raw:r,cooked:n}=e.value;let o=g(r,t,i);o&&(e.value={raw:o,cooked:o})};function q(e,t,i){let r;if(t==="CallExpression"){Array.isArray(i.calleeNames)||(i.calleeNames=[]),i.calleeNames.includes("require")||i.calleeNames.push("require");let n=e.node.callee.name;const{calleeNames:s}=i;if(!s.includes(n)||!e.node.arguments.length)return;r=e.node.arguments[0]}return e.node.source&&(r=e.node.source),r}let R=["CallExpression","ImportDeclaration","ExportNamedDeclaration","ExportAllDeclaration"].map(e=>({name:e,fn:z}));function z(e,t,i){let r={...v,...t.opts},n=q(e,i,r);if(n)return _(n)(t,r)}function B({types:e}){let t=new S;return R.forEach(r=>{let{name:n,fn:s}=r;t.addMethod(n,s)}),{visitor:t.getVisitor()}}return B});