import-map-overrides
Version:
[](https://www.jsdelivr.com/package/npm/import-map-overrides)
4 lines (3 loc) • 18.1 kB
JavaScript
/* import-map-overrides@6.1.0 */
!function(){"use strict";function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function e(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function r(t,e,r){return(e=function(t){var e=function(t){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function n(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function o(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?n(Object(o),!0).forEach((function(e){r(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function i(){i=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},a="function"==typeof Symbol?Symbol:{},c=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",l=a.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function p(t,e,r,n){var i=e&&e.prototype instanceof g?e:g,a=Object.create(i.prototype),c=new k(n||[]);return o(a,"_invoke",{value:M(t,r,c)}),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=p;var m="suspendedStart",v="suspendedYield",d="executing",h="completed",y={};function g(){}function w(){}function b(){}var O={};s(O,c,(function(){return this}));var x=Object.getPrototypeOf,E=x&&x(x(N([])));E&&E!==r&&n.call(E,c)&&(O=E);var S=b.prototype=g.prototype=Object.create(O);function j(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function I(t,e){function r(o,i,a,c){var u=f(t[o],t,i);if("throw"!==u.type){var l=u.arg,s=l.value;return s&&"object"==typeof s&&n.call(s,"__await")?e.resolve(s.__await).then((function(t){r("next",t,a,c)}),(function(t){r("throw",t,a,c)})):e.resolve(s).then((function(t){l.value=t,a(l)}),(function(t){return r("throw",t,a,c)}))}c(u.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function M(e,r,n){var o=m;return function(i,a){if(o===d)throw Error("Generator is already running");if(o===h){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=P(c,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===m)throw o=h,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var l=f(e,r,n);if("normal"===l.type){if(o=n.done?h:v,l.arg===y)continue;return{value:l.arg,done:n.done}}"throw"===l.type&&(o=h,n.method="throw",n.arg=l.arg)}}}function P(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,P(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=f(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function L(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function A(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function k(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function N(e){if(e||""===e){var r=e[c];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return w.prototype=b,o(S,"constructor",{value:b,configurable:!0}),o(b,"constructor",{value:w,configurable:!0}),w.displayName=s(b,l,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===w||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,s(t,l,"GeneratorFunction")),t.prototype=Object.create(S),t},e.awrap=function(t){return{__await:t}},j(I.prototype),s(I.prototype,u,(function(){return this})),e.AsyncIterator=I,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new I(p(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},j(S),s(S,l,"Generator"),s(S,c,(function(){return this})),s(S,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=N,k.prototype={constructor:k,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(A),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function o(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),l=n.call(a,"finallyLoc");if(u&&l){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!l)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),A(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;A(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:N(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}function a(e,r){return function(t){if(Array.isArray(t))return t}(e)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,c=[],u=!0,l=!1;try{if(i=(r=r.call(t)).next,0===e);else for(;!(u=(n=i.call(r)).done)&&(c.push(n.value),c.length!==e);u=!0);}catch(t){l=!0,o=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(l)throw o}}return c}}(e,r)||function(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(t){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c(t)}var u;function l(t,e){if(Array.isArray(t)){for(var r=0;r<t.length;r++)if(t[r]===e)return!0;return!1}if("string"==typeof t)return t.indexOf(e)>=0;throw Error("Can't call includes on ".concat(c(t)))}var s,p="import-map-override:",f="import-map-overrides-disabled",m="import-map-overrides-external-maps",v="data-is-importmap-override",d="import-map-overrides-domains",h="allowlist:",y="denylist:",g=document.querySelector('meta[name="importmap-type"]'),w=document.querySelector('meta[name="'.concat(d,'"]')),b={},O=null!==(u=null==g?void 0:g.getAttribute("content"))&&void 0!==u?u:"importmap";if(w){var x=w.getAttribute("content");x||console.warn("Invalid ".concat(d," meta element - content required."));var E=function(t){return new RegExp(function(t){if("string"!=typeof t)throw new TypeError("Expected a string");return t.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}(t).replace("\\*",".+")).test(window.location.hostname)};0===x.indexOf(h)?s=!x.slice(10).split(",").some(E):0===x.indexOf(y)?s=x.slice(9).split(",").some(E):console.log("Invalid ".concat(d," meta content attribute - must start with ").concat(h," or ").concat(y))}else s=!1;function S(t,e){return Object.entries(t).reduce((function(t,r){var n=a(r,2),o=n[0],i=n[1];return t[o]=function(t,e){try{return new URL(t,e).href}catch(e){return t}}(i,e),t}),{})}(function(){try{return localStorage.getItem("test"),!0}catch(t){return!1}})()||(console.warn("Disabling import-map-overrides, since local storage is not readable"),s=!0),s||function(){var t,r=!!g&&g.hasAttribute("server-cookie"),n=!!g&&g.hasAttribute("server-only"),c=!!g&&g.hasAttribute("allow-query-param-override");window.importMapOverrides={addOverride:function(t,e){/^\d+$/g.test(e)&&(e=u.getUrlFromPort(t,e));var n=p+t;return localStorage.setItem(n,e),r&&(document.cookie="".concat(n,"=").concat(e)),d(),u.getOverrideMap()},getOverrideMap:function(){for(var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e={imports:{},scopes:{}},r=u.getDisabledOverrides(),n=function(n,o){!t&&r.indexOf(n)>=0||(e.imports[n]=o)},o=0;o<localStorage.length;o++){var i=localStorage.key(o);0===i.indexOf(p)&&n(i.slice(20),localStorage.getItem(i))}if(c){var a=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:window.location.href;t=t.replace(/[\[\]]/g,"\\$&");var r=new RegExp("[?&]"+t+"(=([^&#]*)|&|#|$)").exec(e);return r?r[2]?decodeURIComponent(r[2].replace(/\+/g," ")):"":null}("imo",window.location!=window.parent.location?document.referrer:window.location.href);if(a){var l;try{l=JSON.parse(a)}catch(t){throw Error("Invalid importMap query param - text content must be json")}Object.keys(l.imports).forEach((function(t){n(t,l.imports[t])}))}}return e},removeOverride:function(t){var e=p+t,n=null!==localStorage.getItem(e);return localStorage.removeItem(e),r&&(document.cookie="".concat(e,"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;")),u.enableOverride(t),d(),n},getOverrideScopes:function(){return(t=i().mark((function t(){var e,r,n,a,c,l,s,p,f;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e={},t.next=3,u.getDefaultMap();case 3:for(a in r=t.sent,(n=u.getOverrideMap()).imports)if(c=r.imports[a])for(f in l=new URL(c,window.location.href).href,s=new URL(n.imports[a],window.location.href).href,p=s.slice(0,s.lastIndexOf("/"))+"/",r.scopes||{})l.startsWith(f)&&(e[p]=o(o({},e[p]||{}),r.scopes[f]));return t.abrupt("return",{scopes:e});case 7:case"end":return t.stop()}}),t)})),function(){var r=this,n=arguments;return new Promise((function(o,i){var a=t.apply(r,n);function c(t){e(a,o,i,c,u,"next",t)}function u(t){e(a,o,i,c,u,"throw",t)}c(void 0)}))})();var t},resetOverrides:function(){return Object.keys(u.getOverrideMap(!0).imports).forEach((function(t){u.removeOverride(t)})),localStorage.removeItem(f),localStorage.removeItem(m),d(),u.getOverrideMap()},hasOverrides:function(){return Object.keys(u.getOverrideMap().imports).length>0},getUrlFromPort:function(t,e){var r=t.replace(/@/g,"").replace(/\//g,"-");return"//localhost:".concat(e,"/").concat(r,".js")},enableUI:function(){var t="import-map-overrides-full",e="show-when-local-storage",r=document.querySelector(t);r||((r=document.createElement(t)).setAttribute(e,"true"),document.body.appendChild(r));var n=r.getAttribute(e);n&&(localStorage.setItem(n,!0),r.renderWithPreact())},mergeImportMap:function(t,e){var r={imports:{},scopes:{}};for(var n in t.imports)r.imports[n]=t.imports[n];for(var o in e.imports)r.imports[o]=e.imports[o];for(var i in t.scopes)r.scopes[i]=t.scopes[i];for(var a in e.scopes)r.scopes[a]=e.scopes[a];return r},resetDefaultMap:function(){t=null},getDefaultMap:function(){return t||(t=Array.prototype.reduce.call(document.querySelectorAll('script[type="'.concat(O,'"], script[type="overridable-importmap"]')),(function(t,e){return e.hasAttribute(v)?t:(r=e.src?P(e.src):Promise.resolve(JSON.parse(e.textContent)),Promise.all([t,r]).then((function(t){var e=a(t,2),r=e[0],n=e[1];return u.mergeImportMap(r,n)})));var r}),Promise.resolve({imports:{},scopes:{}})))},getCurrentPageMap:function(){return Promise.all([u.getDefaultMap(),u.getExternalOverrideMap(u.getCurrentPageExternalOverrides())]).then((function(t){var e=a(t,2),r=e[0],n=e[1];return u.mergeImportMap(u.mergeImportMap(r,n),w)}))},getCurrentPageExternalOverrides:function(){var t=[];return document.querySelectorAll("[".concat(v,']:not([id="import-map-overrides"])')).forEach((function(e){t.push(e.src)})),t},getNextPageMap:function(){return Promise.all([u.getDefaultMap(),u.getExternalOverrideMap()]).then((function(t){var e=a(t,2),r=e[0],n=e[1];return u.mergeImportMap(u.mergeImportMap(r,n),u.getOverrideMap())}))},disableOverride:function(t){var e=u.getDisabledOverrides();return!l(e,t)&&(localStorage.setItem(f,JSON.stringify(e.concat(t))),d(),!0)},enableOverride:function(t){var e=u.getDisabledOverrides(),r=e.indexOf(t);return r>=0&&(e.splice(r,1),localStorage.setItem(f,JSON.stringify(e)),d(),!0)},getDisabledOverrides:function(){var t=localStorage.getItem(f);return t?JSON.parse(t):[]},isDisabled:function(t){return l(u.getDisabledOverrides(),t)},getExternalOverrides:function(){var t=localStorage.getItem(m);return t?JSON.parse(t).sort():[]},addExternalOverride:function(t){t=new URL(t,document.baseURI).href;var e=u.getExternalOverrides();return!l(e,t)&&(localStorage.setItem(m,JSON.stringify(e.concat(t))),d(),!0)},removeExternalOverride:function(t){var e=u.getExternalOverrides();return!!l(e,t)&&(localStorage.setItem(m,JSON.stringify(e.filter((function(e){return e!==t})))),d(),!0)},getExternalOverrideMap:function(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:u.getExternalOverrides()).reduce((function(t,e){var r=b[e]||(b[e]=P(e));return Promise.all([t,r]).then((function(t){var e=a(t,2),r=e[0],n=e[1];return u.mergeImportMap(r,n)}))}),Promise.resolve({imports:{},scopes:{}}))},isExternalMapValid:function(t){return(b[t]||(b[t]=P(t))).then((function(){return!l(u.invalidExternalMaps,t)}))},invalidExternalMaps:[]};var u=window.importMapOverrides,s=!0;try{CustomEvent?new CustomEvent("a"):s=!1}catch(t){s=!1}function d(){h("change")}function h(t){setTimeout((function(){var e="import-map-overrides:".concat(t),r=s?new CustomEvent(e):document.createEvent("CustomEvent");s||r.initCustomEvent(e,!0,!0,null),window.dispatchEvent(r)}))}var y,w=u.getOverrideMap(),x=u.getExternalOverrides();if(!n){var E=document.querySelector('script[type="overridable-importmap"]');if(!(y=E)){var j=document.querySelectorAll('script[type="'.concat(O,'"]'));y=j?j[j.length-1]:null}if(E){if(E.src)throw Error('import-map-overrides: external import maps with type="overridable-importmap" are not supported');var I;try{I=JSON.parse(E.textContent)}catch(t){throw Error('Invalid <script type="overridable-importmap"> - text content must be json')}y=M(u.mergeImportMap(I,w),"import-map-overrides",y),L()}else L(),Object.keys(w.imports).length>0&&(y=M(w,"import-map-overrides",y))}function M(t,e,r){if(null==g||!g.hasAttribute("use-injector")){var n=document.createElement("script");return n.type=O,n.id=e,n.setAttribute(v,""),"string"==typeof t?n.src=t:n.textContent=JSON.stringify(t,null,2),r?r.insertAdjacentElement("afterend",n):document.head.appendChild(n),n}}function P(t){return fetch(t).then((function(t){return t.ok?t.json().catch((function(e){return console.warn(Error("External override import map contained invalid json, at url ".concat(t.url,". ").concat(e))),u.invalidExternalMaps.push(t.url),{imports:{},scopes:{}}})):(console.warn(Error("Unable to download external override import map from url ".concat(t.url,". Server responded with status ").concat(t.status))),u.invalidExternalMaps.push(t.url),{imports:{},scopes:{}})}),(function(){return console.warn(Error("Unable to download external import map at url '".concat(t,"'"))),u.invalidExternalMaps.push(new URL(t,document.baseURI).href),{imports:{},scopes:{}}})).then((function(e){return function(t,e){return{imports:S(t.imports||{},e),scopes:Object.keys(t.scopes||{}).reduce((function(r,n){return r[n]=S(t.scopes[n],e),r}),{})}}(e,t)}))}function L(){x.length>0&&x.forEach((function(t,e){y=M(t,"import-map-overrides-external-".concat(e))}))}h("init")}()}();
//# sourceMappingURL=import-map-overrides-api.js.map