Skip to content

Commit

Permalink
Update ES Module Shims to 1.7.3 (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh authored Jun 18, 2023
1 parent 56780fd commit 952f000
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 70 deletions.
157 changes: 89 additions & 68 deletions app/assets/javascripts/es-module-shims.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ES Module Shims 1.7.2 */
/* ES Module Shims 1.7.3 */
(function () {

const hasWindow = typeof window !== 'undefined';
Expand Down Expand Up @@ -596,88 +596,109 @@
// edge doesnt execute sibling in order, so we fix this up by ensuring all previous executions are explicit dependencies
let resolvedSource = edge && lastLoad ? `import '${lastLoad}';` : '';

if (!imports.length) {
resolvedSource += source;
}
else {
// once all deps have loaded we can inline the dependency resolution blobs
// and define this blob
let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
function pushStringTo (originalIndex) {
while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
const dynamicImportEnd = dynamicImportEndStack.pop();
resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
lastIndex = dynamicImportEnd;
}
resolvedSource += source.slice(lastIndex, originalIndex);
lastIndex = originalIndex;
// once all deps have loaded we can inline the dependency resolution blobs
// and define this blob
let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
function pushStringTo (originalIndex) {
while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
const dynamicImportEnd = dynamicImportEndStack.pop();
resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
lastIndex = dynamicImportEnd;
}
for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
// dependency source replacements
if (dynamicImportIndex === -1) {
let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
if (cycleShell) {
// circular shell creation
if (!(blobUrl = depLoad.s)) {
blobUrl = depLoad.s = createBlob(`export function u$_(m){${
depLoad.a[1].map(({ s, e }, i) => {
const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
}).join(',')
}}${
depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
}export {${
depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
}}\n//# sourceURL=${depLoad.r}?cycle`);
}
}

pushStringTo(start - 1);
resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
resolvedSource += source.slice(lastIndex, originalIndex);
lastIndex = originalIndex;
}

// circular shell execution
if (!cycleShell && depLoad.s) {
resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
depLoad.s = undefined;
for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
// dependency source replacements
if (dynamicImportIndex === -1) {
let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
if (cycleShell) {
// circular shell creation
if (!(blobUrl = depLoad.s)) {
blobUrl = depLoad.s = createBlob(`export function u$_(m){${
depLoad.a[1].map(({ s, e }, i) => {
const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
}).join(',')
}}${
depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
}export {${
depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
}}\n//# sourceURL=${depLoad.r}?cycle`);
}
lastIndex = statementEnd;
}
// import.meta
else if (dynamicImportIndex === -2) {
load.m = { url: load.r, resolve: metaResolve };
metaHook(load.m, load.u);
pushStringTo(start);
resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
lastIndex = statementEnd;
}
// dynamic import
else {
pushStringTo(statementStart + 6);
resolvedSource += `Shim(`;
dynamicImportEndStack.push(statementEnd - 1);
lastIndex = start;

pushStringTo(start - 1);
resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;

// circular shell execution
if (!cycleShell && depLoad.s) {
resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
depLoad.s = undefined;
}
lastIndex = statementEnd;
}
// import.meta
else if (dynamicImportIndex === -2) {
load.m = { url: load.r, resolve: metaResolve };
metaHook(load.m, load.u);
pushStringTo(start);
resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
lastIndex = statementEnd;
}
// dynamic import
else {
pushStringTo(statementStart + 6);
resolvedSource += `Shim(`;
dynamicImportEndStack.push(statementEnd - 1);
lastIndex = start;
}
}

// support progressive cycle binding updates (try statement avoids tdz errors)
if (load.s)
resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;

function pushSourceURL (commentPrefix, commentStart) {
const urlStart = commentStart + commentPrefix.length;
const commentEnd = source.indexOf('\n', urlStart);
const urlEnd = commentEnd !== -1 ? commentEnd : source.length;
pushStringTo(urlStart);
resolvedSource += new URL(source.slice(urlStart, urlEnd), load.r).href;
lastIndex = urlEnd;
}

let sourceURLCommentStart = source.lastIndexOf(sourceURLCommentPrefix);
let sourceMapURLCommentStart = source.lastIndexOf(sourceMapURLCommentPrefix);

// support progressive cycle binding updates (try statement avoids tdz errors)
if (load.s)
resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
// ignore sourceMap comments before already spliced code
if (sourceURLCommentStart < lastIndex) sourceURLCommentStart = -1;
if (sourceMapURLCommentStart < lastIndex) sourceMapURLCommentStart = -1;

pushStringTo(source.length);
// sourceURL first / only
if (sourceURLCommentStart !== -1 && (sourceMapURLCommentStart === -1 || sourceMapURLCommentStart > sourceURLCommentStart)) {
pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
}
// sourceMappingURL
if (sourceMapURLCommentStart !== -1) {
pushSourceURL(sourceMapURLCommentPrefix, sourceMapURLCommentStart);
// sourceURL last
if (sourceURLCommentStart !== -1 && (sourceURLCommentStart > sourceMapURLCommentStart))
pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
}

pushStringTo(source.length);

let hasSourceURL = false;
resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
if (!hasSourceURL)
resolvedSource += '\n//# sourceURL=' + load.r;
if (sourceURLCommentStart === -1)
resolvedSource += sourceURLCommentPrefix + load.r;

load.b = lastLoad = createBlob(resolvedSource);
load.S = undefined;
}

// ; and // trailer support added for Ruby on Rails 7 source maps compatibility
// https://github.com/guybedford/es-module-shims/issues/228
const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]*)\s*)*$/;
const sourceURLCommentPrefix = '\n//# sourceURL=';
const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';

const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
const jsonContentType = /^(text|application)\/json(;|$)/;
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/es-module-shims.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 952f000

Please sign in to comment.