[api-minor] Let Babel handle the necessary core-js polyfills automatically

In the last couple of years we've been quicker to remove support for older browsers/environments, which means that at this point in time we don't bundle that many polyfills. (The polyfills are also generally simpler nowadays, ever since we removed support for e.g. Internet Explorer.)
Rather than having to *manually* handle the polyfills, we can actually let Babel take care of bundling the necessary polyfills for us; please refer to https://babeljs.io/docs/babel-preset-env

The only exception here is the Node.js-specific compatibility-code, which is moved into the `src/display/node_utils.js` file. This ought to be fine since workers are not available/used in Node.js-environments.

*Please note:* For the `legacy`-builds this will increase the size of the *built* files, however that seems like a very small price to pay in order to simplify maintenance of the general PDF.js library.
This commit is contained in:
Jonas Jenwald 2023-07-17 15:44:30 +02:00
parent 9db65ad083
commit 67303b16f1
4 changed files with 30 additions and 56 deletions

View File

@ -211,6 +211,14 @@ function createWebpackConfig(
}
const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`);
const babelPresets = skipBabel
? undefined
: [
[
"@babel/preset-env",
{ corejs: "3.31.1", shippedProposals: true, useBuiltIns: "usage" },
],
];
const babelPlugins = ["@babel/plugin-transform-modules-commonjs"];
const plugins = [];
@ -289,7 +297,7 @@ function createWebpackConfig(
loader: "babel-loader",
exclude: babelExcludeRegExp,
options: {
presets: skipBabel ? undefined : ["@babel/preset-env"],
presets: babelPresets,
plugins: babelPlugins,
targets: BABEL_TARGETS,
},

View File

@ -20,6 +20,7 @@ import {
BaseFilterFactory,
BaseStandardFontDataFactory,
} from "./base_factory.js";
import { isNodeJS } from "../shared/is_node.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
@ -27,6 +28,26 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
(function checkDOMMatrix() {
if (globalThis.DOMMatrix || !isNodeJS) {
return;
}
globalThis.DOMMatrix = __non_webpack_require__("canvas").DOMMatrix;
})();
(function checkPath2D() {
if (globalThis.Path2D || !isNodeJS) {
return;
}
const { CanvasRenderingContext2D } = __non_webpack_require__("canvas");
const { polyfillPath2D } = __non_webpack_require__("path2d-polyfill");
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
polyfillPath2D(globalThis);
})();
}
const fetchData = function (url) {
return new Promise((resolve, reject) => {
const fs = __non_webpack_require__("fs");

View File

@ -1,45 +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.
*/
/* globals __non_webpack_require__ */
import { isNodeJS } from "./is_node.js";
// Support: Node.js
(function checkDOMMatrix() {
if (globalThis.DOMMatrix || !isNodeJS) {
return;
}
globalThis.DOMMatrix = __non_webpack_require__("canvas").DOMMatrix;
})();
// Support: Node.js
(function checkPath2D() {
if (globalThis.Path2D || !isNodeJS) {
return;
}
const { CanvasRenderingContext2D } = __non_webpack_require__("canvas");
const { polyfillPath2D } = __non_webpack_require__("path2d-polyfill");
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
polyfillPath2D(globalThis);
})();
// Support: Chrome<98
(function checkStructuredClone() {
if (globalThis.structuredClone) {
return;
}
require("core-js/web/structured-clone.js");
})();

View File

@ -13,16 +13,6 @@
* limitations under the License.
*/
// Skip compatibility checks for modern builds and if we already ran the module.
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
!globalThis._pdfjsCompatibilityChecked
) {
globalThis._pdfjsCompatibilityChecked = true;
require("./compatibility.js");
}
const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];