|
| 1 | +export const hasWindow = typeof window !== 'undefined'; |
| 2 | +export const hasDocument = typeof document !== 'undefined'; |
| 3 | + |
| 4 | +export const noop = () => {}; |
| 5 | + |
| 6 | +const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined; |
| 7 | + |
| 8 | +export const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {}; |
| 9 | +Object.assign(esmsInitOptions, self.esmsInitOptions || {}); |
| 10 | + |
| 11 | +export let shimMode = hasDocument ? !!esmsInitOptions.shimMode : true; |
| 12 | + |
| 13 | +export const importHook = globalHook(shimMode && esmsInitOptions.onimport); |
| 14 | +export const resolveHook = globalHook(shimMode && esmsInitOptions.resolve); |
| 15 | +export let fetchHook = esmsInitOptions.fetch ? globalHook(esmsInitOptions.fetch) : fetch; |
| 16 | +export const metaHook = esmsInitOptions.meta ? globalHook(shimMode && esmsInitOptions.meta) : noop; |
| 17 | + |
| 18 | +export const mapOverrides = esmsInitOptions.mapOverrides; |
| 19 | + |
| 20 | +export let nonce = esmsInitOptions.nonce; |
| 21 | +if (!nonce && hasDocument) { |
| 22 | + const nonceElement = document.querySelector('script[nonce]'); |
| 23 | + if (nonceElement) |
| 24 | + nonce = nonceElement.nonce || nonceElement.getAttribute('nonce'); |
| 25 | +} |
| 26 | + |
| 27 | +export const onerror = globalHook(esmsInitOptions.onerror || noop); |
| 28 | +export const onpolyfill = esmsInitOptions.onpolyfill ? globalHook(esmsInitOptions.onpolyfill) : () => { |
| 29 | + console.log('%c^^ Module TypeError above is polyfilled and can be ignored ^^', 'font-weight:900;color:#391'); |
| 30 | +}; |
| 31 | + |
| 32 | +export const { revokeBlobURLs, noLoadEventRetriggers, enforceIntegrity } = esmsInitOptions; |
| 33 | + |
| 34 | +function globalHook (name) { |
| 35 | + return typeof name === 'string' ? self[name] : name; |
| 36 | +} |
| 37 | + |
| 38 | +const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : []; |
| 39 | +export const cssModulesEnabled = enable.includes('css-modules'); |
| 40 | +export const jsonModulesEnabled = enable.includes('json-modules'); |
| 41 | + |
| 42 | +export const edge = !navigator.userAgentData && !!navigator.userAgent.match(/Edge\/\d+\.\d+/); |
| 43 | + |
| 44 | +export const baseUrl = hasDocument |
| 45 | + ? document.baseURI |
| 46 | + : `${location.protocol}//${location.host}${location.pathname.includes('/') |
| 47 | + ? location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1) |
| 48 | + : location.pathname}`; |
| 49 | + |
| 50 | +export const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type })); |
| 51 | +export let { skip } = esmsInitOptions; |
| 52 | +if (Array.isArray(skip)) { |
| 53 | + const l = skip.map(s => new URL(s, baseUrl).href); |
| 54 | + skip = s => l.some(i => i[i.length - 1] === '/' && s.startsWith(i) || s === i); |
| 55 | +} |
| 56 | +else if (typeof skip === 'string') { |
| 57 | + const r = new RegExp(skip); |
| 58 | + skip = s => r.test(s); |
| 59 | +} |
| 60 | + |
| 61 | +const eoop = err => setTimeout(() => { throw err }); |
| 62 | + |
| 63 | +export const throwError = err => { (self.reportError || hasWindow && window.safari && console.error || eoop)(err), void onerror(err) }; |
| 64 | + |
| 65 | +export function fromParent (parent) { |
| 66 | + return parent ? ` imported from ${parent}` : ''; |
| 67 | +} |
| 68 | + |
| 69 | +export let importMapSrcOrLazy = false; |
| 70 | + |
| 71 | +export function setImportMapSrcOrLazy () { |
| 72 | + importMapSrcOrLazy = true; |
| 73 | +} |
| 74 | + |
| 75 | +// shim mode is determined on initialization, no late shim mode |
| 76 | +if (!shimMode) { |
| 77 | + if (document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]').length) { |
| 78 | + shimMode = true; |
| 79 | + } |
| 80 | + else { |
| 81 | + let seenScript = false; |
| 82 | + for (const script of document.querySelectorAll('script[type=module],script[type=importmap]')) { |
| 83 | + if (!seenScript) { |
| 84 | + if (script.type === 'module' && !script.ep) |
| 85 | + seenScript = true; |
| 86 | + } |
| 87 | + else if (script.type === 'importmap' && seenScript) { |
| 88 | + importMapSrcOrLazy = true; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments