pdf.js/external/builder/test-fixtures_esprima.mjs
Nicolò Ribaudo f724ae98a1
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.
2024-01-23 16:00:59 +01:00

76 lines
2.0 KiB
JavaScript

import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
import { preprocessPDFJSCode } from "./babel-plugin-pdfjs-preprocessor.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let errors = 0;
const baseDir = path.join(__dirname, "fixtures_esprima");
const files = fs
.readdirSync(baseDir)
.filter(function (name) {
return /-expected\./.test(name);
})
.map(function (name) {
return path.join(baseDir, name);
});
files.forEach(function (expectationFilename) {
const inFilename = expectationFilename.replace("-expected", "");
const expectation = fs
.readFileSync(expectationFilename)
.toString()
.trim()
.replaceAll("__filename", fs.realpathSync(inFilename));
const input = fs.readFileSync(inFilename).toString();
const defines = {
TRUE: true,
FALSE: false,
OBJ: { obj: { i: 1 }, j: 2 },
TEXT: "text",
};
const map = {
"import-alias": "import-name",
};
const ctx = {
defines,
map,
rootPath: __dirname + "/../..",
};
let out;
try {
out = preprocessPDFJSCode(ctx, input);
} catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//");
}
if (out !== expectation) {
errors++;
// Allow regenerating the expected output using
// OVERWRITE=true node ./external/builder/test-fixtures_esprima.mjs
if (process.env.OVERWRITE) {
fs.writeFileSync(expectationFilename, out + "\n");
}
console.log("Assertion failed for " + inFilename);
console.log("--------------------------------------------------");
console.log("EXPECTED:");
console.log(expectation);
console.log("--------------------------------------------------");
console.log("ACTUAL");
console.log(out);
console.log("--------------------------------------------------");
console.log();
}
});
if (errors) {
console.error("Found " + errors + " expectation failures.");
process.exit(1);
} else {
console.log("All tests completed without errors.");
process.exit(0);
}