[ESM] Convert the external/builder/-folder to use standard modules

This commit is contained in:
Jonas Jenwald 2023-07-08 16:07:54 +02:00
parent bcf14a49c0
commit 5174232326
6 changed files with 35 additions and 41 deletions

View File

@ -1,8 +1,6 @@
"use strict";
const fs = require("fs"),
path = require("path"),
vm = require("vm");
import fs from "fs";
import path from "path";
import vm from "vm";
const AllWhitespaceRegexp = /^\s+$/g;
@ -226,7 +224,6 @@ function preprocess(inFilename, outFilename, defines) {
fs.writeFileSync(outFilename, out.join("\n"));
}
}
exports.preprocess = preprocess;
/**
* Merge two defines arrays. Values in the second param will override values in
@ -242,4 +239,5 @@ function merge(defaults, defines) {
}
return ret;
}
exports.merge = merge;
export { merge, preprocess };

View File

@ -1,10 +1,8 @@
"use strict";
const acorn = require("acorn");
const escodegen = require("@javascript-obfuscator/escodegen");
const vm = require("vm");
const fs = require("fs");
const path = require("path");
import * as acorn from "acorn";
import escodegen from "@javascript-obfuscator/escodegen";
import fs from "fs";
import path from "path";
import vm from "vm";
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
const ROOT_PREFIX = "$ROOT/";
@ -346,4 +344,4 @@ function preprocessPDFJSCode(ctx, code) {
return escodegen.generate(syntax, codegenOptions);
}
exports.preprocessPDFJSCode = preprocessPDFJSCode;
export { preprocessPDFJSCode };

View File

@ -1,8 +1,9 @@
"use strict";
import * as builder from "./builder.mjs";
import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
const builder = require("./builder.js");
const fs = require("fs");
const path = require("path");
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let errors = 0;

View File

@ -1,8 +1,9 @@
"use strict";
import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
import { preprocessPDFJSCode } from "./preprocessor2.mjs";
const p2 = require("./preprocessor2.js");
const fs = require("fs");
const path = require("path");
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let errors = 0;
@ -40,7 +41,7 @@ files.forEach(function (expectationFilename) {
};
let out;
try {
out = p2.preprocessPDFJSCode(ctx, input);
out = preprocessPDFJSCode(ctx, input);
} catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//");
}

View File

@ -13,12 +13,10 @@
* limitations under the License.
*/
"use strict";
import path from "path";
import { preprocessPDFJSCode } from "../builder/preprocessor2.mjs";
const preprocessor2 = require("../builder/preprocessor2.js");
const path = require("path");
module.exports = function (source) {
export default function (source) {
// Options must be specified, ignoring request if not.
if (!this.query || typeof this.query !== "object") {
return source;
@ -34,10 +32,10 @@ module.exports = function (source) {
ctx.sourceFile = sourcePath;
const callback = this.callback;
const sourceAndMap = preprocessor2.preprocessPDFJSCode(ctx, source);
const sourceAndMap = preprocessPDFJSCode(ctx, source);
const map = sourceAndMap.map.toJSON();
// escodegen does not embed source -- setting map's sourcesContent.
map.sourcesContent = [source];
callback(null, sourceAndMap.code, map);
return undefined;
};
}

View File

@ -14,9 +14,9 @@
*/
/* eslint-env node */
import * as builder from "./external/builder/builder.mjs";
import { exec, spawn, spawnSync } from "child_process";
import autoprefixer from "autoprefixer";
import builder from "./external/builder/builder.js";
import { createRequire } from "module";
import crypto from "crypto";
import { fileURLToPath } from "url";
@ -27,6 +27,7 @@ import { mkdirp } from "mkdirp";
import path from "path";
import postcss from "gulp-postcss";
import postcssDirPseudoClass from "postcss-dir-pseudo-class";
import { preprocessPDFJSCode } from "./external/builder/preprocessor2.mjs";
import rename from "gulp-rename";
import replace from "gulp-replace";
import rimraf from "rimraf";
@ -295,7 +296,7 @@ function createWebpackConfig(
},
},
{
loader: path.join(__dirname, "external/webpack/pdfjsdev-loader.js"),
loader: path.join(__dirname, "external/webpack/pdfjsdev-loader.mjs"),
options: {
rootPath: __dirname,
saveComments: false,
@ -428,7 +429,6 @@ function createScriptingBundle(defines, extraOptions = undefined) {
}
function createSandboxExternal(defines) {
const preprocessor2 = require("./external/builder/preprocessor2.js");
const licenseHeader = fs.readFileSync("./src/license_header.js").toString();
const ctx = {
@ -440,7 +440,7 @@ function createSandboxExternal(defines) {
.pipe(rename("pdf.sandbox.external.sys.mjs"))
.pipe(
transform("utf8", content => {
content = preprocessor2.preprocessPDFJSCode(ctx, content);
content = preprocessPDFJSCode(ctx, content);
return `${licenseHeader}\n${content}`;
})
);
@ -1268,7 +1268,6 @@ gulp.task(
);
function preprocessDefaultPreferences(content) {
const preprocessor2 = require("./external/builder/preprocessor2.js");
const licenseHeader = fs.readFileSync("./src/license_header.js").toString();
const MODIFICATION_WARNING =
@ -1278,7 +1277,7 @@ function preprocessDefaultPreferences(content) {
DEFAULT_PREFERENCES: getDefaultPreferences("mozcentral/"),
});
content = preprocessor2.preprocessPDFJSCode(
content = preprocessPDFJSCode(
{
rootPath: __dirname,
defines: bundleDefines,
@ -1545,7 +1544,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
function preprocess(content) {
const skipBabel =
bundleDefines.SKIP_BABEL || /\/\*\s*no-babel-preset\s*\*\//.test(content);
content = preprocessor2.preprocessPDFJSCode(ctx, content);
content = preprocessPDFJSCode(ctx, content);
content = babel.transform(content, {
sourceType: "module",
presets: skipBabel ? undefined : ["@babel/preset-env"],
@ -1575,7 +1574,6 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
const licenseHeaderLibre = fs
.readFileSync("./src/license_header_libre.js")
.toString();
const preprocessor2 = require("./external/builder/preprocessor2.js");
return inputStream
.pipe(transform("utf8", preprocess))
.pipe(gulp.dest(outputDir));
@ -2411,13 +2409,13 @@ gulp.task(
gulp.task("externaltest", function (done) {
console.log();
console.log("### Running test-fixtures.js");
safeSpawnSync("node", ["external/builder/test-fixtures.js"], {
safeSpawnSync("node", ["external/builder/test-fixtures.mjs"], {
stdio: "inherit",
});
console.log();
console.log("### Running test-fixtures_esprima.js");
safeSpawnSync("node", ["external/builder/test-fixtures_esprima.js"], {
safeSpawnSync("node", ["external/builder/test-fixtures_esprima.mjs"], {
stdio: "inherit",
});
done();