UNPKG

3.56 kBJavaScriptView Raw
1makeInstaller=function(n){"use strict";function e(n,e){return o(n)&&(p(O,n,e),u(I)&&I(M)),M}function t(n){this.id=n,this.children=[],this.childrenById={}}function r(n,e){return B.call(n,e)&&n[e]}function o(n){return"object"==typeof n&&null!==n}function u(n){return"function"==typeof n}function i(n){return"string"==typeof n}function s(n){return new Error("Cannot find module '"+n+"'")}function c(n){function e(e){var t=w(n,e);if(t)return f(t,n.module);var r=s(e);if(u(A))return A(e,n.module.id,r);throw r}return u(k)&&(e=k(e,n.module)),e.extensions=v(n).slice(0),e.resolve=function(e){var t=w(n,e);if(t)return t.module.id;var r=s(e);if(A&&u(A.resolve))return A.resolve(e,n.module.id,r);throw r},e}function l(n,e){var r=this;r.parent=e=e||null,r.module=new t(n),E[n]=r,r.contents=null,r.deps={}}function f(n,e){var t=n.module;if(!B.call(t,"exports")){var r=n.contents;if(!r){if(n.stub)return n.stub;throw s(t.id)}if(e){t.parent=e;var o=e.children;Array.isArray(o)&&o.push(t)}u(t.useNode)&&t.useNode()||r(t.require=t.require||c(n),t.exports=n.stub||{},t,n.module.id,n.parent.module.id),t.loaded=!0}return u(t.runModuleSetters)&&t.runModuleSetters(),t.exports}function d(n){return n&&o(n.contents)}function a(n){return n&&null===n.contents}function p(n,e,t){Array.isArray(e)?(e.forEach(function(t){i(t)?n.deps[t]=n.module.id:u(t)?e=t:o(t)&&(n.stub=n.stub||{},h(t,function(e,t){n.stub[t]=e}))}),u(e)||(e=null)):u(e)||i(e)||o(e)||(e=null),e&&(n.contents=n.contents||(o(e)?{}:e),o(e)&&d(n)&&h(e,function(e,o){if(".."===o)u=n.parent;else{var u=r(n.contents,o);u||(u=n.contents[o]=new l(n.module.id.replace(/\/*$/,"/")+o,n),u.options=t)}p(u,e,t)}))}function h(n,e,t){Object.keys(n).forEach(function(t){e.call(this,n[t],t)},t)}function v(n){return n.options&&n.options.extensions||j}function m(n,e,t){for(;n&&!d(n);)n=n.parent;if(!n||!e||"."===e)return n;if(".."===e)return n.parent;var o=r(n.contents,e);if(t&&(!o||d(o)))for(var u=0;u<t.length;++u){var i=r(n.contents,e+t[u]);if(i&&!d(i))return i}return o}function y(n,e,t){var r=e.split("/");return r.every(function(e,o){return n=o<r.length-1?m(n,e):m(n,e,t)}),n}function b(n,e){var t=e&&e.module;n&&t&&(n.childrenById[t.id]=t)}function w(n,e,t,r){var t=t||n.module,o=v(n);for(n="/"===e.charAt(0)?y(O,e,o):"."===e.charAt(0)?y(n,e,o):x(n,e,o);d(n);){if(r=r||[],r.indexOf(n)<0){r.push(n);var u,s=m(n,"package.json"),c=s&&f(s,t);if(c&&q.some(function(n){return i(u=c[n])})&&(b(t,s),n=y(n,u,o)||w(n,u,t,r)))continue}n=m(n,"index.js")}return n&&i(n.contents)&&(n=w(n,n.contents,t,r)),b(t,n),n}function x(n,e,t){if(u(g)&&(e=g(e,n.module.id)),i(e)){for(var r;n&&!r;n=n.parent)r=d(n)&&y(n,"node_modules/"+e,t);return r}}n=n||{};var j=n.extensions||[".js",".json"],I=n.onInstall,k=n.wrapRequire,g=n.override,A=n.fallback,q=n.mainFields||(n.browser?["browser","main"]:["main"]),B={}.hasOwnProperty,E={},O=new l("/",new l("/..")),M=c(O);e.fetch=function(){throw new Error("fetch not implemented")},t.prototype.resolve=function(n){return this.require.resolve(n)};var P,N;return t.prototype.prefetch=function(n){function t(n){var e=r(E,n.id);a(e)&&!e.pending&&(e.pending=!0,o=o||{},o[n.id]={module:e.module,deps:Object.keys(e.deps),options:e.options,stub:e.stub},h(e.deps,function(n,t){w(e,t)}),h(n.childrenById,t))}{var o,u=this;r(E,u.id)}P=P||Promise.resolve(),N=N||P;var i=N;return N=P.then(function(){var r=u.resolve(n);return h(u.childrenById,t),Promise.resolve(o&&e.fetch(o)).then(function(n){function t(){return n&&e(n),r}return i.then(t,t)})})},e.Module=t,e},"object"==typeof exports&&(exports.makeInstaller=makeInstaller);
\No newline at end of file