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

View File

@ -1,10 +1,8 @@
"use strict"; import * as acorn from "acorn";
import escodegen from "@javascript-obfuscator/escodegen";
const acorn = require("acorn"); import fs from "fs";
const escodegen = require("@javascript-obfuscator/escodegen"); import path from "path";
const vm = require("vm"); import vm from "vm";
const fs = require("fs");
const path = require("path");
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev"; const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
const ROOT_PREFIX = "$ROOT/"; const ROOT_PREFIX = "$ROOT/";
@ -346,4 +344,4 @@ function preprocessPDFJSCode(ctx, code) {
return escodegen.generate(syntax, codegenOptions); 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 __dirname = path.dirname(fileURLToPath(import.meta.url));
const fs = require("fs");
const path = require("path");
let errors = 0; 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 __dirname = path.dirname(fileURLToPath(import.meta.url));
const fs = require("fs");
const path = require("path");
let errors = 0; let errors = 0;
@ -40,7 +41,7 @@ files.forEach(function (expectationFilename) {
}; };
let out; let out;
try { try {
out = p2.preprocessPDFJSCode(ctx, input); out = preprocessPDFJSCode(ctx, input);
} catch (e) { } catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//"); out = ("Error: " + e.message).replaceAll(/^/gm, "//");
} }

View File

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

View File

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