mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-16 02:29:50 +00:00
wasm-bindgen-rayon and new OMT plugin (#1007)
* WIP: wasm-bindgen-rayon and new OMT plugin * Bump package-lock * Make OMT work again * Update year * Restore accidental change * Prevent minification of `nextDefineUri` * Delay loading external deps This gives inline `define` calls a chance to define dependencies for earlier `define` calls on the same page. * Add comment
This commit is contained in:
112
lib/omt.ejs
112
lib/omt.ejs
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2020 Google Inc. All Rights Reserved.
|
||||
* 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
|
||||
@@ -13,80 +13,64 @@
|
||||
|
||||
// If the loader is already loaded, just stop.
|
||||
if (!self.<%- amdFunctionName %>) {
|
||||
const singleRequire = async name => {
|
||||
if (name === 'require') return require;
|
||||
let url;
|
||||
if (name.startsWith(location.origin)) {
|
||||
url = name.slice(location.origin.length);
|
||||
} else {
|
||||
url = name.slice(1) + '.js';
|
||||
}
|
||||
if (!url.startsWith('/c/')) {
|
||||
url = '/c' + url;
|
||||
}
|
||||
name = './static' + url;
|
||||
if (registry[name]) return registry[name];
|
||||
let registry = {};
|
||||
|
||||
if (!registry[name]) {
|
||||
const singleRequire = (uri, parentUri) => {
|
||||
let origURI = uri;
|
||||
uri = new URL(uri + ".js", parentUri).href;
|
||||
return registry[uri] || (
|
||||
<% if (useEval) { %>
|
||||
const text = await fetch(url).then(resp => resp.text());
|
||||
eval(text);
|
||||
fetch(uri)
|
||||
.then(resp => resp.text())
|
||||
.then(code => {
|
||||
self.nextDefineUri = uri;
|
||||
eval(code);
|
||||
})
|
||||
<% } else { %>
|
||||
if ("document" in self) {
|
||||
await new Promise(resolve => {
|
||||
new Promise(resolve => {
|
||||
if ("document" in self) {
|
||||
const script = document.createElement("script");
|
||||
script.src = url;
|
||||
document.head.appendChild(script);
|
||||
script.src = uri;
|
||||
script.onload = resolve;
|
||||
});
|
||||
} else {
|
||||
importScripts(url);
|
||||
}
|
||||
document.head.appendChild(script);
|
||||
} else {
|
||||
self.nextDefineUri = uri;
|
||||
importScripts(uri);
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
<% } %>
|
||||
}
|
||||
if (!registry[name]) {
|
||||
throw new Error(`Module ${name} didn’t register its module`);
|
||||
}
|
||||
return registry[name];
|
||||
.then(() => {
|
||||
let promise = registry[uri];
|
||||
if (!promise) {
|
||||
throw new Error(`Module ${uri} didn’t register its module`);
|
||||
}
|
||||
return promise;
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const require = (names, resolve) => {
|
||||
Promise.all(names.map(singleRequire))
|
||||
.then(modules => resolve(modules.length === 1 ? modules[0] : modules));
|
||||
};
|
||||
|
||||
const registry = {
|
||||
require: Promise.resolve(require)
|
||||
};
|
||||
|
||||
self.<%- amdFunctionName %> = (moduleName, depsNames, factory) => {
|
||||
if (registry[moduleName]) {
|
||||
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;
|
||||
}
|
||||
registry[moduleName] = Promise.resolve().then(() => {
|
||||
let exports = {};
|
||||
const module = {
|
||||
uri: location.origin + moduleName.slice(1)
|
||||
};
|
||||
return Promise.all(
|
||||
depsNames.map(depName => {
|
||||
switch(depName) {
|
||||
case "exports":
|
||||
return exports;
|
||||
case "module":
|
||||
return module;
|
||||
default:
|
||||
return singleRequire(depName);
|
||||
}
|
||||
})
|
||||
).then(deps => {
|
||||
const facValue = factory(...deps);
|
||||
if (!exports.default) {
|
||||
exports.default = facValue;
|
||||
}
|
||||
return exports;
|
||||
});
|
||||
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;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user