1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

[api-major] Output JavaScript modules in the builds (issue 10317)

At this point in time all browsers, and also Node.js, support standard `import`/`export` statements and we can now finally consider outputting modern JavaScript modules in the builds.[1]

In order for this to work we can *only* use proper `import`/`export` statements throughout the main code-base, and (as expected) our Node.js support made this much more complicated since both the official builds and the GitHub Actions-based tests must keep working.[2]
One remaining issue is that the `pdf.scripting.js` file cannot be built as a JavaScript module, since doing so breaks PDF scripting.

Note that my initial goal was to try and split these changes into a couple of commits, however that unfortunately didn't really work since it turned out to be difficult for smaller patches to work correctly and pass (all) tests that way.[3]
This is a classic case of every change requiring a couple of other changes, with each of those changes requiring further changes in turn and the size/scope quickly increasing as a result.

One possible "issue" with these changes is that we'll now only output JavaScript modules in the builds, which could perhaps be a problem with older tools. However it unfortunately seems far too complicated/time-consuming for us to attempt to support both the old and modern module formats, hence the alternative would be to do "nothing" here and just keep our "old" builds.[4]

---
[1] The final blocker was module support in workers in Firefox, which was implemented in Firefox 114; please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility

[2] It's probably possible to further improve/simplify especially the Node.js-specific code, but it does appear to work as-is.

[3] Having partially "broken" patches, that fail tests, as part of the commit history is *really not* a good idea in general.

[4] Outputting JavaScript modules was first requested almost five years ago, see issue 10317, and nowadays there *should* be much better support for JavaScript modules in various tools.
This commit is contained in:
Jonas Jenwald 2023-09-28 13:00:10 +02:00
parent 0a970ee443
commit 927e50f5d4
23 changed files with 227 additions and 241 deletions

View file

@ -34,7 +34,7 @@ const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";
const IMAGE_RESOURCES_PATH = "/web/images/";
const VIEWER_CSS = "../build/components/pdf_viewer.css";
const VIEWER_LOCALE = "en-US";
const WORKER_SRC = "../build/generic/build/pdf.worker.js";
const WORKER_SRC = "../build/generic/build/pdf.worker.mjs";
const RENDER_TASK_ON_CONTINUE_DELAY = 5; // ms
const SVG_NS = "http://www.w3.org/2000/svg";

View file

@ -18,8 +18,8 @@ limitations under the License.
<head>
<title>PDF.js test slave</title>
<meta charset="utf-8">
<script src="../build/generic/build/pdf.js"></script>
<script src="../build/components/pdf_viewer.js"></script>
<script src="../build/generic/build/pdf.mjs" type="module"></script>
<script src="../build/components/pdf_viewer.mjs" type="module"></script>
<link rel="resource" type="application/l10n" href="../build/generic/web/locale/locale.properties">
</head>

View file

@ -56,6 +56,8 @@ import { GlobalImageCache } from "../../src/core/image_utils.js";
import { GlobalWorkerOptions } from "../../src/display/worker_options.js";
import { Metadata } from "../../src/display/metadata.js";
const WORKER_SRC = "../../build/generic/build/pdf.worker.mjs";
describe("api", function () {
const basicApiFileName = "basicapi.pdf";
const basicApiFileLength = 105779; // bytes
@ -914,7 +916,8 @@ describe("api", function () {
}
GlobalWorkerOptions.workerPort = new Worker(
new URL("../../build/generic/build/pdf.worker.js", window.location)
new URL(WORKER_SRC, window.location),
{ type: "module" }
);
const loadingTask1 = getDocument(basicApiGetDocumentParams);
@ -934,7 +937,8 @@ describe("api", function () {
}
GlobalWorkerOptions.workerPort = new Worker(
new URL("../../build/generic/build/pdf.worker.js", window.location)
new URL(WORKER_SRC, window.location),
{ type: "module" }
);
const loadingTask1 = getDocument(basicApiGetDocumentParams);
@ -963,7 +967,8 @@ describe("api", function () {
}
GlobalWorkerOptions.workerPort = new Worker(
new URL("../../build/generic/build/pdf.worker.js", window.location)
new URL(WORKER_SRC, window.location),
{ type: "module" }
);
const loadingTask = getDocument(basicApiGetDocumentParams);

View file

@ -108,7 +108,7 @@ async function initializePDFJS(callback) {
);
}
// Configure the worker.
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.mjs";
callback();
}

View file

@ -49,7 +49,6 @@ import {
getXfaPageViewport,
isDataScheme,
isPdfFile,
loadScript,
noContextMenu,
PDFDateString,
PixelsPerInch,
@ -88,7 +87,6 @@ const expectedAPI = Object.freeze({
InvalidPDFException,
isDataScheme,
isPdfFile,
loadScript,
MissingPDFException,
noContextMenu,
normalizeUnicode,
@ -132,10 +130,9 @@ describe("web_pdfjsLib", function () {
if (isNodeJS) {
pending("loadScript is not supported in Node.js.");
}
await loadScript(
"../../build/generic/build/pdf.js",
/* removeScriptElement = */ true
);
const apiPath = "../../build/generic/build/pdf.mjs";
await import(apiPath);
const webPdfjsLib = await import("../../web/pdfjs.js");
expect(Object.keys(webPdfjsLib).sort()).toEqual(

View file

@ -13,9 +13,7 @@
* limitations under the License.
*/
import { loadScript } from "../../src/display/display_utils.js";
const sandboxBundleSrc = "../../build/generic/build/pdf.sandbox.js";
const sandboxBundleSrc = "../../build/generic/build/pdf.sandbox.mjs";
describe("Scripting", function () {
let sandbox, send_queue, test_id, ref, windowAlert;
@ -53,8 +51,9 @@ describe("Scripting", function () {
const command = "alert";
send_queue.set(command, { command, value });
};
const promise = loadScript(sandboxBundleSrc).then(() => {
return window.pdfjsSandbox.QuickJSSandbox();
// eslint-disable-next-line no-unsanitized/method
const promise = import(sandboxBundleSrc).then(pdfjsSandbox => {
return pdfjsSandbox.QuickJSSandbox();
});
sandbox = {
createSandbox(data) {