Repository navigation
Expand file tree
/
Copy pathmin.js
More file actions
2 lines (2 loc) · 3.04 KB
/
Copy pathmin.js
File metadata and controls
2 lines (2 loc) · 3.04 KB
1
2
/*! (c) Andrea Giammarchi, ISC License */
function loadAsFile(e,o){let t,n;return["",".js",".json"].some(s=>(t=`${e}${DIR_SEPARATOR}${o}${s}`,0<(n=loadInfo(t)).type.length))?/^javascript|json$/.test(n.type)&&/\.(?:js|json)$/.test(t)?n.path:loadAsDirectory([e,o].join(DIR_SEPARATOR)):""}function loadAsDirectory(e){const o=loadInfo([e,"package.json"].join(DIR_SEPARATOR));if("json"===o.type){const t=JSON.parse(loadContent(o.path));if(t.main)return loadAsFile(e,t.main)}return loadIndex(e)}function loadIndex(e){const o=loadInfo([e,"index.js"].join(DIR_SEPARATOR));if("javascript"===o.type)return o.path;const t=loadInfo([e,"index.json"].join(DIR_SEPARATOR));return"json"===t.type?t.path:""}function resolve(e,o){const t=(isPath.test(o)?loadAsFile:loadAsModule)(e,o);if(!t)throw new Error(`${o} not found`);return t}const DIR_SEPARATOR="/",cache=Object.create(null),modules=Object.create(null),isPath=/^[./\\]/,cjs=e=>{function o(e){const t=o.resolve(e);if(!(t in cache)){if(!t)throw new Error(`${e} not found`);const o=loadContent(t);if(".json"===t.slice(-5))cache[t]=JSON.parse(o);else{const e={exports:{}},n=t.slice(0,t.lastIndexOf(DIR_SEPARATOR));cache[t]=e.exports,Function("exports","module","require","__dirname","__filename",o.toString().replace(/^#!.+[\r\n]+/,"")).call(e.exports,e.exports,e,cjs(n),n,t),cache[t]=e.exports}}return cache[t]}return o.cache=cache,o.resolve=resolve.bind(null,(e||".").replace(/\/+$/,"")),o};let loadContent,loadInfo,loadAsModule;try{new XMLHttpRequest,loadContent=(e=>{const o=new XMLHttpRequest;return o.open("GET",e,!1),o.send(null),o.responseText}),loadInfo=(e=>{const o=new XMLHttpRequest;try{return o.open("HEAD",e,!1),o.send(null),{type:o.status<400?o.getResponseHeader("content-type").replace(/^.+?\/([a-z_-]+).*$/,"$1"):"",path:o.responseURL}}catch(o){return{type:"dir",path:e}}}),loadAsModule=((e,o)=>{const t=[e,o].join(DIR_SEPARATOR);if(t in modules)return modules[t];do{const n=loadInfo([e,"package.json"].join(DIR_SEPARATOR));if("json"===n.type){const{dependencies:e,devDependencies:s}=JSON.parse(loadContent(n.path)),r=e&&e[o]||s&&s[o];if(r)return modules[t]=loadAsDirectory(`https://unpkg.com/${o}@${r.replace(/[~^]/g,"").replace(/\.[x*]/g,".0")}`)}}while(e=e.slice(0,e.lastIndexOf(DIR_SEPARATOR)));return""})}catch(e){const o=e=>e.replace(/(^|\/)\.\//g,"$1").replace(/([^/]+?)\/\.\.\//g,""),t={type:"",path:""};if(loadAsModule=((e,t)=>{const n=o([e,t].join(DIR_SEPARATOR));if(n in modules)return modules[n];do{const o=[e,"node_modules"].join(DIR_SEPARATOR);if("dir"===loadInfo(o).type){const e=loadInfo([o,t].join(DIR_SEPARATOR));if("dir"===e.type)return modules[n]=loadAsDirectory(e.path)}}while(e=e.slice(0,e.lastIndexOf(DIR_SEPARATOR)));return""}),"function"==typeof read&&"object"==typeof os&&os.system)loadContent=(e=>read(e)),loadInfo=(e=>(e=o(e),0===os.system(`[ -f '${e}' ]`)?{type:/\.js$/.test(e)?"javascript":/\.json$/.test(e)?"json":"",path:e}:0===os.system(`[ -d '${e}' ]`)?{type:"dir",path:e}:t));else{if("function"!=typeof readFile)throw new Error("Incompatible Platform");loadContent=(e=>readFile(e)),loadInfo=(e=>(e=o(e),t))}}export default cjs;