Files
squoosh/lib/omt.ejs
Ingvar Stepanyan 10c5082499 Add back a loader customization for full URL (#1016)
Emscripten uses dynamic `import(fullUrlOfTheMainJS)` in generated Workers.

It appears that upstream rollup-plugin-off-main-thread never handled this correctly, but we had a customization in Squoosh loader template that allowed those anyway, which I removed together with most other customizations in #1007, thus breaking Emscripten codecs.

This adds a hotfix back, but we need to fix this properly upstream too (TBD separately).
2021-05-13 15:48:23 +01:00

76 lines
2.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Copyright 2021 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// If the loader is already loaded, just stop.
if (!self.<%- amdFunctionName %>) {
let registry = {};
const singleRequire = (uri, parentUri) => {
uri = uri.startsWith(location.origin) ? uri : new URL(uri + ".js", parentUri).href;
return registry[uri] || (
<% if (useEval) { %>
fetch(uri)
.then(resp => resp.text())
.then(code => {
self.nextDefineUri = uri;
eval(code);
})
<% } else { %>
new Promise(resolve => {
if ("document" in self) {
const script = document.createElement("script");
script.src = uri;
script.onload = resolve;
document.head.appendChild(script);
} else {
self.nextDefineUri = uri;
importScripts(uri);
resolve();
}
})
<% } %>
.then(() => {
let promise = registry[uri];
if (!promise) {
throw new Error(`Module ${uri} didnt register its module`);
}
return promise;
})
);
};
self.<%- amdFunctionName %> = (depsNames, factory) => {
const uri = self.nextDefineUri || ("document" in self ? document.currentScript.src : "") || location.href;
if (registry[uri]) {
// Module is already loading or loaded.
return;
}
let exports = {};
const require = depUri => singleRequire(depUri, uri);
const specialDeps = {
module: { uri },
exports,
require
};
// Note: Promise.resolve() is necessary to delay loading until all the
// `define`s on the current page had a chance to execute first.
// This allows to inline some deps on the main page.
registry[uri] = Promise.resolve().then(() => Promise.all(depsNames.map(
depName => specialDeps[depName] || require(depName)
))).then(deps => {
factory(...deps);
return exports;
});
};
}