Remove SystemJS usage, in development mode, from the worker

Now that https://bugzilla.mozilla.org/show_bug.cgi?id=1247687 has landed in Firefox, we're able to use worker-modules during development :-)

This removes the final piece of SystemJS usage from the PDF.js library, thus allowing a fair bit of clean-up, and we now use *only* native `import`/`export` statements everywhere in development mode.
This commit is contained in:
Jonas Jenwald 2020-11-01 23:06:40 +01:00
parent 797f8d3dca
commit 95bf9fc17f
12 changed files with 3 additions and 347 deletions

View File

@ -28,7 +28,6 @@
"globals": {
"PDFJSDev": false,
"exports": false,
"SystemJS": false,
},
"rules": {

View File

@ -1,152 +0,0 @@
/* Copyright 2017 Mozilla Foundation
*
* 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.
*/
/* eslint-disable no-var */
var babel = require("plugin-babel");
var cacheExpiration = 60 /* min */ * 60 * 1000;
var dbVersion = 1;
var dbName = "babelcache";
var dbCacheTable = "translated";
var dbPromise;
function getDb() {
if (!dbPromise) {
dbPromise = new Promise(function (resolve, reject) {
var request = indexedDB.open(dbName, dbVersion);
request.onupgradeneeded = function () {
var db = request.result;
db.createObjectStore(dbCacheTable, { keyPath: "address" });
};
request.onsuccess = function () {
var db = request.result;
resolve(db);
};
request.onerror = function () {
console.warn("getDb: " + request.error);
reject(request.error);
};
});
}
return dbPromise;
}
function storeCache(address, hashCode, translated, format, sourceMap) {
return getDb().then(function (db) {
var tx = db.transaction(dbCacheTable, "readwrite");
var store = tx.objectStore(dbCacheTable);
store.put({
address,
hashCode,
translated,
expires: Date.now() + cacheExpiration,
format,
sourceMap,
});
return new Promise(function (resolve, reject) {
tx.oncomplete = function () {
resolve();
};
tx.onerror = function () {
resolve();
};
});
});
}
function loadCache(address, hashCode) {
return getDb().then(function (db) {
var tx = db.transaction(dbCacheTable, "readonly");
var store = tx.objectStore(dbCacheTable);
var getAddress = store.get(address);
return new Promise(function (resolve, reject) {
tx.oncomplete = function () {
var found = getAddress.result;
var isValid =
found && found.hashCode === hashCode && Date.now() < found.expires;
resolve(
isValid
? {
translated: found.translated,
format: found.format,
sourceMap: found.sourceMap,
}
: null
);
};
tx.onerror = function () {
resolve(null);
};
});
});
}
var encoder = new TextEncoder("utf-8");
function sha256(str) {
var buffer = encoder.encode(str);
return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
var data = new Int32Array(hash);
return (
data[0].toString(36) +
"-" +
data[1].toString(36) +
"-" +
data[2].toString(36) +
"-" +
data[3].toString(36)
);
});
}
exports.translate = function (load, opt) {
var savedHashCode, babelTranslateError;
return sha256(load.source)
.then(function (hashCode) {
savedHashCode = hashCode;
return loadCache(load.address, hashCode);
})
.then(
function (cache) {
if (cache) {
load.metadata.format = cache.format;
return cache.translated;
}
return babel.translate.call(this, load, opt).then(
function (translated) {
return storeCache(
load.address,
savedHashCode,
translated,
load.metadata.format,
load.metadata.sourceMap
).then(function () {
return translated;
});
},
function (reason) {
throw (babelTranslateError = reason);
}
);
}.bind(this)
)
.catch(
function (reason) {
if (babelTranslateError) {
throw babelTranslateError;
}
return babel.translate.call(this, load, opt);
}.bind(this)
);
};

26
package-lock.json generated
View File

@ -55,8 +55,6 @@
"streamqueue": "^1.1.2",
"stylelint": "^15.6.0",
"stylelint-prettier": "^3.0.0",
"systemjs": "^0.21.6",
"systemjs-plugin-babel": "^0.0.25",
"terser": "^5.17.1",
"through2": "^4.0.2",
"ttest": "^4.0.0",
@ -17482,18 +17480,6 @@
"integrity": "sha1-WPcc7jvVGbWdSyqEO2x95krAR2Q=",
"dev": true
},
"node_modules/systemjs": {
"version": "0.21.6",
"resolved": "https://registry.npmjs.org/systemjs/-/systemjs-0.21.6.tgz",
"integrity": "sha512-R+5S9eV9vcQgWOoS4D87joZ4xkFJHb19ZsyKY07D1+VBDE9bwYcU+KXE0r5XlDA8mFoJGyuWDbfrNoh90JsA8g==",
"dev": true
},
"node_modules/systemjs-plugin-babel": {
"version": "0.0.25",
"resolved": "https://registry.npmjs.org/systemjs-plugin-babel/-/systemjs-plugin-babel-0.0.25.tgz",
"integrity": "sha512-RMKSizWWlw4+IpDB385ugxn7Owd9W+HEtjYDQ6yO1FpsnER/vk6FbXRweUF+mvRi6EHgk8vDdUdtui7ReDwX3w==",
"dev": true
},
"node_modules/table": {
"version": "6.8.1",
"resolved": "https://registry.npmjs.org/table/-/table-6.8.1.tgz",
@ -32651,18 +32637,6 @@
"integrity": "sha1-WPcc7jvVGbWdSyqEO2x95krAR2Q=",
"dev": true
},
"systemjs": {
"version": "0.21.6",
"resolved": "https://registry.npmjs.org/systemjs/-/systemjs-0.21.6.tgz",
"integrity": "sha512-R+5S9eV9vcQgWOoS4D87joZ4xkFJHb19ZsyKY07D1+VBDE9bwYcU+KXE0r5XlDA8mFoJGyuWDbfrNoh90JsA8g==",
"dev": true
},
"systemjs-plugin-babel": {
"version": "0.0.25",
"resolved": "https://registry.npmjs.org/systemjs-plugin-babel/-/systemjs-plugin-babel-0.0.25.tgz",
"integrity": "sha512-RMKSizWWlw4+IpDB385ugxn7Owd9W+HEtjYDQ6yO1FpsnER/vk6FbXRweUF+mvRi6EHgk8vDdUdtui7ReDwX3w==",
"dev": true
},
"table": {
"version": "6.8.1",
"resolved": "https://registry.npmjs.org/table/-/table-6.8.1.tgz",

View File

@ -48,8 +48,6 @@
"streamqueue": "^1.1.2",
"stylelint": "^15.6.0",
"stylelint-prettier": "^3.0.0",
"systemjs": "^0.21.6",
"systemjs-plugin-babel": "^0.0.25",
"terser": "^5.17.1",
"through2": "^4.0.2",
"ttest": "^4.0.0",

View File

@ -1,13 +0,0 @@
{
"parserOptions": {
"ecmaVersion": 2017,
},
"extends": [
"../../.eslintrc"
],
"env": {
"es2017": true,
},
}

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* no-babel-preset */
import { getLookupTableFactory } from "./core_utils.js";

View File

@ -2130,12 +2130,9 @@ class PDFWorker {
);
}
// Some versions of FF can't create a worker on localhost, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
const worker =
typeof PDFJSDev === "undefined" &&
!workerSrc.endsWith("/build/pdf.worker.js") &&
!workerSrc.endsWith("/src/worker_loader.js")
!workerSrc.endsWith("/build/pdf.worker.js")
? new Worker(workerSrc, { type: "module" })
: new Worker(workerSrc);
const messageHandler = new MessageHandler("main", "worker", worker);

View File

@ -1,13 +0,0 @@
{
"parserOptions": {
"ecmaVersion": 2017,
},
"extends": [
"../../.eslintrc"
],
"env": {
"es2017": true,
},
}

View File

@ -1,32 +0,0 @@
/* Copyright 2012 Mozilla Foundation
*
* 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.
*/
"use strict";
// Patch importScripts to work around a bug in WebKit and Chrome 48-.
// See https://crbug.com/572225 and https://webkit.org/b/153317.
self.importScripts = (function (importScripts) {
return function () {
setTimeout(function () {}, 0);
return importScripts.apply(this, arguments);
};
})(importScripts);
importScripts("../node_modules/systemjs/dist/system.js");
importScripts("../systemjs.config.js");
SystemJS.import("pdfjs/core/worker.js").then(function () {
// Worker is loaded at this point.
});

View File

@ -1,96 +0,0 @@
/* Copyright 2017 Mozilla Foundation
*
* 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.
*/
/* eslint-disable no-var, unicorn/no-typeof-undefined */
"use strict";
(function () {
var baseLocation;
if (typeof document !== "undefined") {
baseLocation = new URL("./", document.currentScript.src);
} else if (typeof location !== "undefined") {
// Probably worker -- walking subfolders until we will reach root.
baseLocation = location;
while (baseLocation.href.includes("/src/")) {
baseLocation = new URL("..", baseLocation);
}
} else {
throw new Error("Cannot configure SystemJS");
}
var PluginBabelPath = "node_modules/systemjs-plugin-babel/plugin-babel.js";
var SystemJSPluginBabelPath =
"node_modules/systemjs-plugin-babel/systemjs-babel-browser.js";
var PluginBabelCachePath = "external/systemjs/plugin-babel-cached.js";
var isCachingPossible =
typeof indexedDB !== "undefined" &&
typeof TextEncoder !== "undefined" &&
typeof crypto !== "undefined" &&
typeof crypto.subtle !== "undefined";
// When we create a bundle, webpack is run on the source and it will replace
// require with __webpack_require__. When we want to use the real require,
// __non_webpack_require__ has to be used.
// In this target, we don't create a bundle, so we have to replace the
// occurrences of __non_webpack_require__ ourselves.
function babelPluginReplaceNonWebPackRequire(babel) {
return {
visitor: {
Identifier(path, state) {
if (path.node.name === "__non_webpack_require__") {
path.replaceWith(babel.types.identifier("require"));
}
},
},
};
}
SystemJS.config({
packages: {
"": {
defaultExtension: "js",
},
},
paths: {
pdfjs: new URL("src", baseLocation).href,
"pdfjs-web": new URL("web", baseLocation).href,
"pdfjs-test": new URL("test", baseLocation).href,
"pdfjs-lib": new URL("src/pdf", baseLocation).href,
"core-js": new URL("node_modules/core-js", baseLocation).href,
"web-streams-polyfill": new URL(
"node_modules/web-streams-polyfill",
baseLocation
).href,
},
meta: {
"*": {
scriptLoad: false,
esModule: true,
babelOptions: {
env: false,
plugins: [babelPluginReplaceNonWebPackRequire],
},
},
},
map: {
"plugin-babel": new URL(PluginBabelPath, baseLocation).href,
"systemjs-babel-build": new URL(SystemJSPluginBabelPath, baseLocation)
.href,
"plugin-babel-cached": new URL(PluginBabelCachePath, baseLocation).href,
},
transpiler: isCachingPossible ? "plugin-babel-cached" : "plugin-babel",
});
})();

View File

@ -309,12 +309,7 @@ const PDFViewerApplication = {
const { mainContainer, viewerContainer } = this.appConfig,
params = parseQueryString(hash);
if (
typeof PDFJSDev === "undefined" &&
params.get("workermodules") === "true"
) {
AppOptions.set("workerSrc", "../src/pdf.worker.js");
} else if (params.get("disableworker") === "true") {
if (params.get("disableworker") === "true") {
try {
await loadFakeWorker();
} catch (ex) {

View File

@ -292,7 +292,7 @@ const defaultOptions = {
value:
// eslint-disable-next-line no-nested-ternary
typeof PDFJSDev === "undefined"
? "../src/worker_loader.js"
? "../src/pdf.worker.js"
: PDFJSDev.test("MOZCENTRAL")
? "resource://pdf.js/build/pdf.worker.js"
: "../build/pdf.worker.js",