From 6e3179994869d5713b3adabdea37966c3767b79b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 19 Aug 2022 10:45:40 +0200 Subject: [PATCH] [api-minor] Add the Babel `targets`-option to avoid transpiling code for unsupported browsers Currently we simply use the Babel `preset-env` in the `legacy`-builds of the PDF.js library. This has the side-effect of transpiling the code for *very old* browsers/environments, including ones that we (since many years) no longer support which unnecessarily bloats the size of the `legacy`-builds. For the CSS files we're only targeting *the supported browsers*, and it's thus possible to extend that to also apply to Babel. One of the most significant changes, with this patch, is that we'll no longer polyfill `async`/`await` in the `legacy`-builds. However, this shouldn't be an issue given the browsers that we currently support in PDF.js; please refer to: - https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#browser_compatibility --- README.md | 2 +- external/dist/README.md | 2 +- gulpfile.js | 42 +++++++++++++-------------------- package-lock.json | 52 ----------------------------------------- package.json | 1 - 5 files changed, 18 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index b042bd95c..3d022156f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mo ### Online demo Please note that the "Modern browsers" version assumes native support for -features such as `async`/`await`, optional chaining, nullish coalescing, +features such as optional chaining, nullish coalescing, and private `class` fields/methods. + Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html diff --git a/external/dist/README.md b/external/dist/README.md index c3348a54b..144be52bf 100644 --- a/external/dist/README.md +++ b/external/dist/README.md @@ -8,7 +8,7 @@ This is a pre-built version of the PDF.js source code. It is automatically generated by the build scripts. For usage with older browsers or environments, without support for modern -features such as `async`/`await`, optional chaining, nullish coalescing, +features such as optional chaining, nullish coalescing, and private `class` fields/methods; please see the `legacy/` folder. See https://github.com/mozilla/pdf.js for learning and contributing. diff --git a/gulpfile.js b/gulpfile.js index 11feaa7de..8d6cd86de 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -78,18 +78,22 @@ const builder = require("./external/builder/builder.js"); const CONFIG_FILE = "pdfjs.config"; const config = JSON.parse(fs.readFileSync(CONFIG_FILE).toString()); +const ENV_TARGETS = [ + "last 2 versions", + "Chrome >= 76", + "Firefox ESR", + "Safari >= 13.1", + "> 1%", + "not IE > 0", + "not dead", +]; + // Default Autoprefixer config used for generic, components, minified-pre const AUTOPREFIXER_CONFIG = { - overrideBrowserslist: [ - "last 2 versions", - "Chrome >= 76", - "Firefox ESR", - "Safari >= 13.1", - "> 1%", - "not IE > 0", - "not dead", - ], + overrideBrowserslist: ENV_TARGETS, }; +// Default Babel targets used for generic, components, minified-pre +const BABEL_TARGETS = ENV_TARGETS.join(", "); const DEFINES = Object.freeze({ PRODUCTION: true, @@ -213,16 +217,7 @@ function createWebpackConfig( } const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`); - const babelPlugins = [ - "@babel/plugin-transform-modules-commonjs", - [ - "@babel/plugin-transform-runtime", - { - helpers: false, - regenerator: true, - }, - ], - ]; + const babelPlugins = ["@babel/plugin-transform-modules-commonjs"]; const plugins = []; if (!disableLicenseHeader) { @@ -262,6 +257,7 @@ function createWebpackConfig( options: { presets: skipBabel ? undefined : ["@babel/preset-env"], plugins: babelPlugins, + targets: BABEL_TARGETS, }, }, { @@ -1493,15 +1489,9 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) { presets: skipBabel ? undefined : ["@babel/preset-env"], plugins: [ "@babel/plugin-transform-modules-commonjs", - [ - "@babel/plugin-transform-runtime", - { - helpers: false, - regenerator: true, - }, - ], babelPluginReplaceNonWebpackImports, ], + targets: BABEL_TARGETS, }).code; const removeCjsSrc = /^(var\s+\w+\s*=\s*(_interopRequireDefault\()?require\(".*?)(?:\/src)(\/[^"]*"\)\)?;)$/gm; diff --git a/package-lock.json b/package-lock.json index 8624ef253..e84767864 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "devDependencies": { "@babel/core": "^7.18.10", "@babel/plugin-transform-modules-commonjs": "^7.18.6", - "@babel/plugin-transform-runtime": "^7.18.10", "@babel/preset-env": "^7.18.10", "@babel/runtime": "^7.18.9", "@javascript-obfuscator/escodegen": "2.3.0", @@ -1502,35 +1501,6 @@ "@babel/core": "^7.0.0-0" } }, - "node_modules/@babel/plugin-transform-runtime": { - "version": "7.18.10", - "resolved": "https://registry.npmjs.org/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.18.10.tgz", - "integrity": "sha512-q5mMeYAdfEbpBAgzl7tBre/la3LeCxmDO1+wMXRdPWbcoMjR3GiXlCLk7JBZVVye0bqTGNMbt0yYVXX1B1jEWQ==", - "dev": true, - "dependencies": { - "@babel/helper-module-imports": "^7.18.6", - "@babel/helper-plugin-utils": "^7.18.9", - "babel-plugin-polyfill-corejs2": "^0.3.2", - "babel-plugin-polyfill-corejs3": "^0.5.3", - "babel-plugin-polyfill-regenerator": "^0.4.0", - "semver": "^6.3.0" - }, - "engines": { - "node": ">=6.9.0" - }, - "peerDependencies": { - "@babel/core": "^7.0.0-0" - } - }, - "node_modules/@babel/plugin-transform-runtime/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", - "dev": true, - "bin": { - "semver": "bin/semver.js" - } - }, "node_modules/@babel/plugin-transform-shorthand-properties": { "version": "7.18.6", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.18.6.tgz", @@ -19877,28 +19847,6 @@ "@babel/helper-plugin-utils": "^7.18.6" } }, - "@babel/plugin-transform-runtime": { - "version": "7.18.10", - "resolved": "https://registry.npmjs.org/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.18.10.tgz", - "integrity": "sha512-q5mMeYAdfEbpBAgzl7tBre/la3LeCxmDO1+wMXRdPWbcoMjR3GiXlCLk7JBZVVye0bqTGNMbt0yYVXX1B1jEWQ==", - "dev": true, - "requires": { - "@babel/helper-module-imports": "^7.18.6", - "@babel/helper-plugin-utils": "^7.18.9", - "babel-plugin-polyfill-corejs2": "^0.3.2", - "babel-plugin-polyfill-corejs3": "^0.5.3", - "babel-plugin-polyfill-regenerator": "^0.4.0", - "semver": "^6.3.0" - }, - "dependencies": { - "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", - "dev": true - } - } - }, "@babel/plugin-transform-shorthand-properties": { "version": "7.18.6", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.18.6.tgz", diff --git a/package.json b/package.json index c63609dd5..773a0d211 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "devDependencies": { "@babel/core": "^7.18.10", "@babel/plugin-transform-modules-commonjs": "^7.18.6", - "@babel/plugin-transform-runtime": "^7.18.10", "@babel/preset-env": "^7.18.10", "@babel/runtime": "^7.18.9", "@javascript-obfuscator/escodegen": "2.3.0",