Skip to content

Commit 2df7234

Browse files
author
David Heinemeier Hansson
committed
ES Module Shims 1.5.9
1 parent f81889a commit 2df7234

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

app/assets/javascripts/es-module-shims.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ES Module Shims 1.5.8 */
1+
/* ES Module Shims 1.5.9 */
22
(function () {
33

44
const hasWindow = typeof window !== 'undefined';
@@ -16,7 +16,7 @@
1616
const importHook = globalHook(shimMode && esmsInitOptions.onimport);
1717
const resolveHook = globalHook(shimMode && esmsInitOptions.resolve);
1818
let fetchHook = esmsInitOptions.fetch ? globalHook(esmsInitOptions.fetch) : fetch;
19-
const metaHook = esmsInitOptions.meta ? globalHook(shimModule && esmsInitOptions.meta) : noop;
19+
const metaHook = esmsInitOptions.meta ? globalHook(shimMode && esmsInitOptions.meta) : noop;
2020

2121
const skip = esmsInitOptions.skip ? new RegExp(esmsInitOptions.skip) : null;
2222

@@ -44,10 +44,6 @@
4444
const cssModulesEnabled = enable.includes('css-modules');
4545
const jsonModulesEnabled = enable.includes('json-modules');
4646

47-
function setShimMode () {
48-
shimMode = true;
49-
}
50-
5147
const edge = !navigator.userAgentData && !!navigator.userAgent.match(/Edge\/\d+\.\d+/);
5248

5349
const baseUrl = hasDocument
@@ -66,6 +62,32 @@
6662

6763
function fromParent (parent) {
6864
return parent ? ` imported from ${parent}` : '';
65+
}
66+
67+
let importMapSrcOrLazy = false;
68+
69+
function setImportMapSrcOrLazy () {
70+
importMapSrcOrLazy = true;
71+
}
72+
73+
// shim mode is determined on initialization, no late shim mode
74+
if (!shimMode) {
75+
if (document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]').length) {
76+
shimMode = true;
77+
}
78+
else {
79+
let seenScript = false;
80+
for (const script of document.querySelectorAll('script[type=module],script[type=importmap]')) {
81+
if (!seenScript) {
82+
if (script.type === 'module' && !script.ep)
83+
seenScript = true;
84+
}
85+
else if (script.type === 'importmap' && seenScript) {
86+
importMapSrcOrLazy = true;
87+
break;
88+
}
89+
}
90+
}
6991
}
7092

7193
const backslashRegEx = /\\/g;
@@ -243,7 +265,7 @@
243265
function dynamicImportScript (url, { errUrl = url } = {}) {
244266
err = undefined;
245267
const src = createBlob(`import*as m from'${url}';self._esmsi=m`);
246-
const s = Object.assign(document.createElement('script'), { type: 'module', src });
268+
const s = Object.assign(document.createElement('script'), { type: 'module', src, ep: true });
247269
s.setAttribute('nonce', nonce);
248270
s.setAttribute('noshim', '');
249271
const p = new Promise((resolve, reject) => {
@@ -402,36 +424,15 @@
402424
}
403425

404426
let importMap = { imports: {}, scopes: {} };
405-
let importMapSrcOrLazy = false;
406427
let baselinePassthrough;
407428

408429
const initPromise = featureDetectionPromise.then(() => {
409-
// shim mode is determined on initialization, no late shim mode
410-
if (!shimMode) {
411-
if (document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]').length) {
412-
setShimMode();
413-
}
414-
else {
415-
let seenScript = false;
416-
for (const script of document.querySelectorAll('script[type=module],script[type=importmap]')) {
417-
if (!seenScript) {
418-
if (script.type === 'module')
419-
seenScript = true;
420-
}
421-
else if (script.type === 'importmap') {
422-
importMapSrcOrLazy = true;
423-
break;
424-
}
425-
}
426-
}
427-
}
428430
baselinePassthrough = esmsInitOptions.polyfillEnable !== true && supportsDynamicImport && supportsImportMeta && supportsImportMaps && (!jsonModulesEnabled || supportsJsonAssertions) && (!cssModulesEnabled || supportsCssAssertions) && !importMapSrcOrLazy && !false;
429431
if (hasDocument) {
430432
if (!supportsImportMaps) {
431433
const supports = HTMLScriptElement.supports || (type => type === 'classic' || type === 'module');
432434
HTMLScriptElement.supports = type => type === 'importmap' || supports(type);
433435
}
434-
435436
if (shimMode || !baselinePassthrough) {
436437
new MutationObserver(mutations => {
437438
for (const mutation of mutations) {
@@ -476,7 +477,7 @@
476477
if (!shimMode)
477478
acceptingImportMaps = false;
478479
await importMapPromise;
479-
if (importHook) await importHook(id, typeof args[1] !== 'string' ? args[1] : {}, parentUrl);
480+
if (importHook) await importHook(url, typeof fetchOpts !== 'string' ? fetchOpts : {}, '');
480481
// early analysis opt-out - no need to even fetch if we have feature support
481482
if (!shimMode && baselinePassthrough) {
482483
// for polyfill case, only dynamic import needs a return value here, and dynamic import will never pass nativelyLoaded
@@ -824,7 +825,7 @@
824825
if (script.src) {
825826
if (!shimMode)
826827
return;
827-
importMapSrcOrLazy = true;
828+
setImportMapSrcOrLazy();
828829
}
829830
if (acceptingImportMaps) {
830831
importMapPromise = importMapPromise

app/assets/javascripts/es-module-shims.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)