From 67303b16f1f708fd1c430eccd11719f4e4e8d65b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 17 Jul 2023 15:44:30 +0200 Subject: [PATCH] [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. --- gulpfile.mjs | 10 ++++++++- src/display/node_utils.js | 21 +++++++++++++++++ src/shared/compatibility.js | 45 ------------------------------------- src/shared/util.js | 10 --------- 4 files changed, 30 insertions(+), 56 deletions(-) delete mode 100644 src/shared/compatibility.js diff --git a/gulpfile.mjs b/gulpfile.mjs index 160b1a953..5be216f8a 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -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, }, diff --git a/src/display/node_utils.js b/src/display/node_utils.js index e848ea543..e187c5aec 100644 --- a/src/display/node_utils.js +++ b/src/display/node_utils.js @@ -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"); diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js deleted file mode 100644 index 346024f77..000000000 --- a/src/shared/compatibility.js +++ /dev/null @@ -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"); -})(); diff --git a/src/shared/util.js b/src/shared/util.js index 70184dd8d..bbc692dd0 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -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];