1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 14:48:08 +02:00

Replace the webpack+acorn transform with a Babel plugin

This commit converts the pdfjsdev-loader transform into a Babel plugin,
to skip a AST->string->AST round-trip.

Before this commit, the webpack build process was:
1. Babel parses the code
2. Babel transforms the AST
3. Babel generates the code
4. Acorn parses the code
5. pdfjsdev-loader transforms the AST
6. @javascript-obfuscator/escodegen generates the code
7. Webpack parses the file
8. Webpack concatenates the files

After this commit, it is reduced to:
1. Babel parses the code
2. Babel transforms the AST
3. babel-plugin-pdfjs-preprocessor transforms the AST
4. Babel generates the code
5. Webpack parses the file
6. Webpack concatenates the files

This change improves the build time by ~25% (tested on MacBook Air M2):
- `gulp lib`: 3.4s to 2.6s
- `gulp dist`: 36s to 29s
- `gulp generic`: 5.5s to 4.0s
- `gulp mozcentral`: 4.7s to 3.2s

The new Babel plugin doesn't support the `saveComments` option of
pdfjsdev-loader, and it just always discards comments. Even though
pdfjsdev-loader supported multiple values for that option, it was
effectively ignored due to `acorn` dropping comments by default.
This commit is contained in:
Nicolò Ribaudo 2024-01-22 18:33:31 +01:00
parent f5bb9bc21b
commit f724ae98a1
No known key found for this signature in database
GPG key ID: AAFDA9101C58F338
16 changed files with 287 additions and 479 deletions

View file

@ -14,6 +14,10 @@
*/
/* eslint-env node */
import {
babelPluginPDFJSPreprocessor,
preprocessPDFJSCode,
} from "./external/builder/babel-plugin-pdfjs-preprocessor.mjs";
import { exec, spawn, spawnSync } from "child_process";
import autoprefixer from "autoprefixer";
import babel from "@babel/core";
@ -30,7 +34,6 @@ import postcssDirPseudoClass from "postcss-dir-pseudo-class";
import postcssDiscardComments from "postcss-discard-comments";
import postcssNesting from "postcss-nesting";
import { preprocess } from "./external/builder/builder.mjs";
import { preprocessPDFJSCode } from "./external/builder/preprocessor2.mjs";
import rename from "gulp-rename";
import replace from "gulp-replace";
import rimraf from "rimraf";
@ -209,10 +212,11 @@ function createWebpackConfig(
const isModule = output.library?.type === "module";
const skipBabel = bundleDefines.SKIP_BABEL;
// `core-js`, see https://github.com/zloirock/core-js/issues/514,
// should be excluded from processing.
const babelExcludes = ["node_modules[\\\\\\/]core-js"];
const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`);
const babelExcludeRegExp = [
// `core-js`, see https://github.com/zloirock/core-js/issues/514,
// should be excluded from processing.
/node_modules[\\/]core-js/,
];
const babelPresets = skipBabel
? undefined
@ -227,7 +231,15 @@ function createWebpackConfig(
},
],
];
const babelPlugins = [];
const babelPlugins = [
[
babelPluginPDFJSPreprocessor,
{
rootPath: __dirname,
defines: bundleDefines,
},
],
];
const plugins = [];
if (!disableLicenseHeader) {
@ -335,14 +347,6 @@ function createWebpackConfig(
targets: BABEL_TARGETS,
},
},
{
loader: path.join(__dirname, "external/webpack/pdfjsdev-loader.mjs"),
options: {
rootPath: __dirname,
saveComments: false,
defines: bundleDefines,
},
},
],
},
// Avoid shadowing actual Node.js variables with polyfills, by disabling
@ -463,7 +467,6 @@ function createSandboxExternal(defines) {
const licenseHeader = fs.readFileSync("./src/license_header.js").toString();
const ctx = {
saveComments: false,
defines,
};
return gulp
@ -1572,13 +1575,15 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
}
function preprocessLib(content) {
const skipBabel = bundleDefines.SKIP_BABEL;
content = preprocessPDFJSCode(ctx, content);
content = babel.transform(content, {
sourceType: "module",
presets: skipBabel
? undefined
: [["@babel/preset-env", { loose: false, modules: false }]],
plugins: [babelPluginReplaceNonWebpackImport],
plugins: [
babelPluginReplaceNonWebpackImport,
[babelPluginPDFJSPreprocessor, ctx],
],
targets: BABEL_TARGETS,
}).code;
content = content.replaceAll(
@ -1589,7 +1594,6 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
}
const ctx = {
rootPath: __dirname,
saveComments: false,
defines: bundleDefines,
map: {
"pdfjs-lib": "../pdf.js",