Merge pull request #12527 from Snuffleupagus/worker-rm-require

Remove SystemJS usage from the development viewer and the unit-tests
This commit is contained in:
Tim van der Meij 2020-10-26 23:28:03 +01:00 committed by GitHub
commit b27e4fac9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 6048 additions and 5991 deletions

View File

@ -185,6 +185,19 @@ function createWebpackConfig(defines, output) {
!bundleDefines.TESTING;
var skipBabel = bundleDefines.SKIP_BABEL;
// `core-js` (see https://github.com/zloirock/core-js/issues/514),
// `web-streams-polyfill` (already using a transpiled file), and
// `src/core/{glyphlist,unicode}.js` (Babel is too slow for those when
// source-maps are enabled) should be excluded from processing.
const babelExcludes = [
"node_modules[\\\\\\/]core-js",
"node_modules[\\\\\\/]web-streams-polyfill",
];
if (enableSourceMaps) {
babelExcludes.push("src[\\\\\\/]core[\\\\\\/](glyphlist|unicode)");
}
const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`);
// Required to expose e.g., the `window` object.
output.globalObject = "this";
@ -209,11 +222,7 @@ function createWebpackConfig(defines, output) {
rules: [
{
loader: "babel-loader",
// `core-js` (see https://github.com/zloirock/core-js/issues/514),
// `web-streams-polyfill` (already using a transpiled file), and
// `src/core/{glyphlist,unicode}.js` (Babel is too slow for those)
// should be excluded from processing.
exclude: /(node_modules[\\\/]core-js|node_modules[\\\/]web-streams-polyfill|src[\\\/]core[\\\/](glyphlist|unicode))/,
exclude: babelExcludeRegExp,
options: {
presets: skipBabel ? undefined : ["@babel/preset-env"],
plugins: [

View File

@ -27,6 +27,22 @@ function getLookupTableFactory(initializer) {
};
}
function getArrayLookupTableFactory(initializer) {
let lookup;
return function () {
if (initializer) {
let arr = initializer();
initializer = null;
lookup = Object.create(null);
for (let i = 0, ii = arr.length; i < ii; i += 2) {
lookup[arr[i]] = arr[i + 1];
}
arr = null;
}
return lookup;
};
}
class MissingDataException extends BaseException {
constructor(begin, end) {
super(`Missing data [${begin}, ${end})`);
@ -212,6 +228,7 @@ function escapePDFName(str) {
export {
escapePDFName,
getLookupTableFactory,
getArrayLookupTableFactory,
MissingDataException,
XRefEntryException,
XRefParseException,

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1168,8 +1168,7 @@ class PDFPageProxy {
pageIndex: this._pageIndex,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
annotationStorage:
(annotationStorage && annotationStorage.getAll()) || null,
annotationStorage: annotationStorage?.getAll() || null,
});
}
@ -1746,12 +1745,7 @@ const PDFWorker = (function PDFWorkerClosure() {
return mainWorkerMessageHandler;
}
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
if (typeof SystemJS !== "object") {
// Manually load SystemJS, since it's only necessary for fake workers.
await loadScript("../node_modules/systemjs/dist/system.js");
await loadScript("../systemjs.config.js");
}
const worker = await SystemJS.import("pdfjs/core/worker.js");
const worker = await import("pdfjs/core/worker.js");
return worker.WorkerMessageHandler;
}
if (
@ -2556,8 +2550,7 @@ class WorkerTransport {
return this.messageHandler
.sendWithPromise("SaveDocument", {
numPages: this._numPages,
annotationStorage:
(annotationStorage && annotationStorage.getAll()) || null,
annotationStorage: annotationStorage?.getAll() || null,
filename: this._fullReader ? this._fullReader.filename : null,
})
.finally(() => {

13
src/shared/.eslintrc Normal file
View File

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

View File

@ -14,7 +14,6 @@
*/
/* globals __non_webpack_require__ */
import { setStubs, unsetStubs } from "../../examples/node/domstubs.js";
import { buildGetDocumentParams } from "./test_utils.js";
import { getDocument } from "../../src/display/api.js";
import { isNodeJS } from "../../src/shared/is_node.js";
@ -95,6 +94,9 @@ describe("SVGGraphics", function () {
// This points to the XObject image in xobject-image.pdf.
const xobjectObjId = "img_p0_1";
if (isNodeJS) {
const { setStubs } = __non_webpack_require__(
"../../examples/node/domstubs.js"
);
setStubs(global);
}
try {
@ -102,6 +104,9 @@ describe("SVGGraphics", function () {
svgGfx.paintInlineImageXObject(imgData, elementContainer);
} finally {
if (isNodeJS) {
const { unsetStubs } = __non_webpack_require__(
"../../examples/node/domstubs.js"
);
unsetStubs(global);
}
}
@ -113,10 +118,10 @@ describe("SVGGraphics", function () {
function testFunc() {
__non_webpack_require__("zlib");
}
// Verifies that the script loader replaces __non_webpack_require__ with
// require.
expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
if (isNodeJS) {
// Verifies that the script loader replaces __non_webpack_require__ with
// require.
expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
expect(testFunc).not.toThrow();
} else {
// require not defined, require('zlib') not a module, etc.

View File

@ -40,8 +40,8 @@
"use strict";
function initializePDFJS(callback) {
Promise.all(
async function initializePDFJS(callback) {
const modules = await Promise.all(
[
"pdfjs/display/api.js",
"pdfjs/display/worker_options.js",
@ -83,40 +83,42 @@ function initializePDFJS(callback) {
"pdfjs-test/unit/writer_spec.js",
"pdfjs-test/unit/xml_spec.js",
].map(function (moduleName) {
return SystemJS.import(moduleName);
// eslint-disable-next-line no-unsanitized/method
return import(moduleName);
})
).then(function (modules) {
const displayApi = modules[0];
const { GlobalWorkerOptions } = modules[1];
const { PDFNetworkStream } = modules[2];
const { PDFFetchStream } = modules[3];
const { isNodeJS } = modules[4];
);
const [
{ setPDFNetworkStreamFactory },
{ GlobalWorkerOptions },
{ PDFNetworkStream },
{ PDFFetchStream },
{ isNodeJS },
] = modules;
if (isNodeJS) {
throw new Error(
"The `gulp unittest` command cannot be used in Node.js environments."
);
}
// Set the network stream factory for unit-tests.
if (
typeof Response !== "undefined" &&
"body" in Response.prototype &&
typeof ReadableStream !== "undefined"
) {
displayApi.setPDFNetworkStreamFactory(function (params) {
return new PDFFetchStream(params);
});
} else {
displayApi.setPDFNetworkStreamFactory(function (params) {
return new PDFNetworkStream(params);
});
}
if (isNodeJS) {
throw new Error(
"The `gulp unittest` command cannot be used in Node.js environments."
);
}
// Set the network stream factory for unit-tests.
if (
typeof Response !== "undefined" &&
"body" in Response.prototype &&
typeof ReadableStream !== "undefined"
) {
setPDFNetworkStreamFactory(function (params) {
return new PDFFetchStream(params);
});
} else {
setPDFNetworkStreamFactory(function (params) {
return new PDFNetworkStream(params);
});
}
// Configure the worker.
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";
// Configure the worker.
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";
callback();
});
callback();
}
(function () {
@ -197,26 +199,26 @@ function initializePDFJS(callback) {
// Sets longer timeout.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
// Replace the browser window's `onload`, ensure it's called, and then run
// all of the loaded specs. This includes initializing the `HtmlReporter`
// instance and then executing the loaded Jasmine environment.
const currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
initializePDFJS(function () {
htmlReporter.initialize();
env.execute();
});
};
function extend(destination, source) {
for (const property in source) {
destination[property] = source[property];
}
return destination;
}
function unitTestInit() {
initializePDFJS(function () {
htmlReporter.initialize();
env.execute();
});
}
if (
document.readyState === "interactive" ||
document.readyState === "complete"
) {
unitTestInit();
} else {
document.addEventListener("DOMContentLoaded", unitTestInit, true);
}
})();

View File

@ -5,12 +5,22 @@
<link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="../../node_modules/systemjs/dist/system.js"></script>
<script src="../../systemjs.config.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="testreporter.js"></script>
<script src="jasmine-boot.js"></script>
<script defer src="../../node_modules/es-module-shims/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
"imports": {
"pdfjs/": "../../src/",
"pdfjs-lib": "../../src/pdf.js",
"pdfjs-web/": "../../web/",
"pdfjs-test/": "../"
}
}
</script>
<script src="jasmine-boot.js" type="module-shim"></script>
</head>
<body>
</body>

View File

@ -2026,12 +2026,7 @@ async function loadFakeWorker() {
GlobalWorkerOptions.workerSrc = AppOptions.get("workerSrc");
}
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
if (typeof SystemJS !== "object") {
// Manually load SystemJS, since it's only necessary for fake workers.
await loadScript("../node_modules/systemjs/dist/system.js");
await loadScript("../systemjs.config.js");
}
window.pdfjsWorker = await SystemJS.import("pdfjs/core/worker.js");
window.pdfjsWorker = await import("pdfjs/core/worker.js");
return undefined;
}
return loadScript(PDFWorker.getWorkerSrc());