[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
This commit is contained in:
Jonas Jenwald 2022-08-19 10:45:40 +02:00
parent 8f74fe6e1b
commit 6e31799948
5 changed files with 18 additions and 81 deletions

View File

@ -24,7 +24,7 @@ Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mo
### Online demo ### Online demo
Please note that the "Modern browsers" version assumes native support for 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. and private `class` fields/methods.
+ Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html + Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html

View File

@ -8,7 +8,7 @@ This is a pre-built version of the PDF.js source code. It is automatically
generated by the build scripts. generated by the build scripts.
For usage with older browsers or environments, without support for modern 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. and private `class` fields/methods; please see the `legacy/` folder.
See https://github.com/mozilla/pdf.js for learning and contributing. See https://github.com/mozilla/pdf.js for learning and contributing.

View File

@ -78,18 +78,22 @@ const builder = require("./external/builder/builder.js");
const CONFIG_FILE = "pdfjs.config"; const CONFIG_FILE = "pdfjs.config";
const config = JSON.parse(fs.readFileSync(CONFIG_FILE).toString()); 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 // Default Autoprefixer config used for generic, components, minified-pre
const AUTOPREFIXER_CONFIG = { const AUTOPREFIXER_CONFIG = {
overrideBrowserslist: [ overrideBrowserslist: ENV_TARGETS,
"last 2 versions",
"Chrome >= 76",
"Firefox ESR",
"Safari >= 13.1",
"> 1%",
"not IE > 0",
"not dead",
],
}; };
// Default Babel targets used for generic, components, minified-pre
const BABEL_TARGETS = ENV_TARGETS.join(", ");
const DEFINES = Object.freeze({ const DEFINES = Object.freeze({
PRODUCTION: true, PRODUCTION: true,
@ -213,16 +217,7 @@ function createWebpackConfig(
} }
const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`); const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`);
const babelPlugins = [ const babelPlugins = ["@babel/plugin-transform-modules-commonjs"];
"@babel/plugin-transform-modules-commonjs",
[
"@babel/plugin-transform-runtime",
{
helpers: false,
regenerator: true,
},
],
];
const plugins = []; const plugins = [];
if (!disableLicenseHeader) { if (!disableLicenseHeader) {
@ -262,6 +257,7 @@ function createWebpackConfig(
options: { options: {
presets: skipBabel ? undefined : ["@babel/preset-env"], presets: skipBabel ? undefined : ["@babel/preset-env"],
plugins: babelPlugins, plugins: babelPlugins,
targets: BABEL_TARGETS,
}, },
}, },
{ {
@ -1493,15 +1489,9 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
presets: skipBabel ? undefined : ["@babel/preset-env"], presets: skipBabel ? undefined : ["@babel/preset-env"],
plugins: [ plugins: [
"@babel/plugin-transform-modules-commonjs", "@babel/plugin-transform-modules-commonjs",
[
"@babel/plugin-transform-runtime",
{
helpers: false,
regenerator: true,
},
],
babelPluginReplaceNonWebpackImports, babelPluginReplaceNonWebpackImports,
], ],
targets: BABEL_TARGETS,
}).code; }).code;
const removeCjsSrc = const removeCjsSrc =
/^(var\s+\w+\s*=\s*(_interopRequireDefault\()?require\(".*?)(?:\/src)(\/[^"]*"\)\)?;)$/gm; /^(var\s+\w+\s*=\s*(_interopRequireDefault\()?require\(".*?)(?:\/src)(\/[^"]*"\)\)?;)$/gm;

52
package-lock.json generated
View File

@ -10,7 +10,6 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.18.10", "@babel/core": "^7.18.10",
"@babel/plugin-transform-modules-commonjs": "^7.18.6", "@babel/plugin-transform-modules-commonjs": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.18.10",
"@babel/preset-env": "^7.18.10", "@babel/preset-env": "^7.18.10",
"@babel/runtime": "^7.18.9", "@babel/runtime": "^7.18.9",
"@javascript-obfuscator/escodegen": "2.3.0", "@javascript-obfuscator/escodegen": "2.3.0",
@ -1502,35 +1501,6 @@
"@babel/core": "^7.0.0-0" "@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": { "node_modules/@babel/plugin-transform-shorthand-properties": {
"version": "7.18.6", "version": "7.18.6",
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.18.6.tgz", "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/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": { "@babel/plugin-transform-shorthand-properties": {
"version": "7.18.6", "version": "7.18.6",
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.18.6.tgz", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.18.6.tgz",

View File

@ -3,7 +3,6 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.18.10", "@babel/core": "^7.18.10",
"@babel/plugin-transform-modules-commonjs": "^7.18.6", "@babel/plugin-transform-modules-commonjs": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.18.10",
"@babel/preset-env": "^7.18.10", "@babel/preset-env": "^7.18.10",
"@babel/runtime": "^7.18.9", "@babel/runtime": "^7.18.9",
"@javascript-obfuscator/escodegen": "2.3.0", "@javascript-obfuscator/escodegen": "2.3.0",