2024-01-23 02:33:31 +09:00
|
|
|
import { types as t, transformSync } from "@babel/core";
|
2023-07-08 23:07:54 +09:00
|
|
|
import fs from "fs";
|
2024-01-23 02:33:31 +09:00
|
|
|
import { join as joinPaths } from "path";
|
2023-07-08 23:07:54 +09:00
|
|
|
import vm from "vm";
|
2016-05-11 08:05:29 +09:00
|
|
|
|
2021-03-14 01:37:27 +09:00
|
|
|
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
|
|
|
|
const ROOT_PREFIX = "$ROOT/";
|
2016-05-11 08:05:29 +09:00
|
|
|
|
|
|
|
function isPDFJSPreprocessor(obj) {
|
|
|
|
return obj.type === "Identifier" && obj.name === PDFJS_PREPROCESSOR_NAME;
|
|
|
|
}
|
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
function evalWithDefines(code, defines) {
|
2016-05-11 08:05:29 +09:00
|
|
|
if (!code || !code.trim()) {
|
|
|
|
throw new Error("No JavaScript expression given");
|
|
|
|
}
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return vm.runInNewContext(code, defines, { displayErrors: false });
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
function handlePreprocessorAction(ctx, actionName, args, path) {
|
2016-05-11 08:05:29 +09:00
|
|
|
try {
|
2024-01-23 02:33:31 +09:00
|
|
|
const arg = args[0];
|
2016-05-11 08:05:29 +09:00
|
|
|
switch (actionName) {
|
|
|
|
case "test":
|
2024-01-23 02:33:31 +09:00
|
|
|
if (!t.isStringLiteral(arg)) {
|
2016-05-11 08:05:29 +09:00
|
|
|
throw new Error("No code for testing is given");
|
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
return !!evalWithDefines(arg.value, ctx.defines);
|
2016-05-11 08:05:29 +09:00
|
|
|
case "eval":
|
2024-01-23 02:33:31 +09:00
|
|
|
if (!t.isStringLiteral(arg)) {
|
2016-05-11 08:05:29 +09:00
|
|
|
throw new Error("No code for eval is given");
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
const result = evalWithDefines(arg.value, ctx.defines);
|
2016-05-11 08:05:29 +09:00
|
|
|
if (
|
|
|
|
typeof result === "boolean" ||
|
|
|
|
typeof result === "string" ||
|
2024-01-23 02:33:31 +09:00
|
|
|
typeof result === "number" ||
|
|
|
|
typeof result === "object"
|
2016-05-11 08:05:29 +09:00
|
|
|
) {
|
2024-01-23 02:33:31 +09:00
|
|
|
return result;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "json":
|
2024-01-23 02:33:31 +09:00
|
|
|
if (!t.isStringLiteral(arg)) {
|
2016-05-11 08:05:29 +09:00
|
|
|
throw new Error("Path to JSON is not provided");
|
|
|
|
}
|
2021-03-14 01:40:44 +09:00
|
|
|
let jsonPath = arg.value;
|
2024-01-23 02:33:31 +09:00
|
|
|
if (jsonPath.startsWith(ROOT_PREFIX)) {
|
|
|
|
jsonPath = joinPaths(
|
2016-05-11 08:05:29 +09:00
|
|
|
ctx.rootPath,
|
|
|
|
jsonPath.substring(ROOT_PREFIX.length)
|
|
|
|
);
|
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
return JSON.parse(fs.readFileSync(jsonPath, "utf8"));
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
throw new Error("Unsupported action");
|
|
|
|
} catch (e) {
|
2024-01-23 02:33:31 +09:00
|
|
|
throw path.buildCodeFrameError(
|
2016-05-11 08:05:29 +09:00
|
|
|
"Could not process " +
|
|
|
|
PDFJS_PREPROCESSOR_NAME +
|
|
|
|
"." +
|
|
|
|
actionName +
|
|
|
|
": " +
|
|
|
|
e.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
function babelPluginPDFJSPreprocessor(babel, ctx) {
|
|
|
|
return {
|
|
|
|
name: "babel-plugin-pdfjs-preprocessor",
|
|
|
|
manipulateOptions({ parserOpts }) {
|
|
|
|
parserOpts.attachComment = false;
|
|
|
|
},
|
|
|
|
visitor: {
|
|
|
|
"ExportNamedDeclaration|ImportDeclaration": ({ node }) => {
|
|
|
|
if (node.source && ctx.map?.[node.source.value]) {
|
|
|
|
node.source.value = ctx.map[node.source.value];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"IfStatement|ConditionalExpression": {
|
|
|
|
exit(path) {
|
|
|
|
const { node } = path;
|
|
|
|
if (t.isBooleanLiteral(node.test)) {
|
|
|
|
// if (true) stmt1; => stmt1
|
|
|
|
// if (false) stmt1; else stmt2; => stmt2
|
2024-02-09 21:58:24 +09:00
|
|
|
if (node.test.value === true) {
|
|
|
|
path.replaceWith(node.consequent);
|
|
|
|
} else if (node.alternate) {
|
|
|
|
path.replaceWith(node.alternate);
|
|
|
|
} else {
|
|
|
|
path.remove(node);
|
|
|
|
}
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
},
|
|
|
|
},
|
2024-01-29 19:13:48 +09:00
|
|
|
UnaryExpression: {
|
|
|
|
exit(path) {
|
|
|
|
const { node } = path;
|
|
|
|
if (
|
|
|
|
node.operator === "typeof" &&
|
|
|
|
isPDFJSPreprocessor(node.argument)
|
|
|
|
) {
|
|
|
|
// typeof PDFJSDev => 'object'
|
|
|
|
path.replaceWith(t.stringLiteral("object"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node.operator === "!" && t.isBooleanLiteral(node.argument)) {
|
|
|
|
// !true => false, !false => true
|
|
|
|
path.replaceWith(t.booleanLiteral(!node.argument.value));
|
|
|
|
}
|
|
|
|
},
|
2024-01-23 02:33:31 +09:00
|
|
|
},
|
|
|
|
LogicalExpression: {
|
|
|
|
exit(path) {
|
|
|
|
const { node } = path;
|
|
|
|
if (!t.isBooleanLiteral(node.left)) {
|
|
|
|
return;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
|
|
|
|
switch (node.operator) {
|
|
|
|
case "&&":
|
|
|
|
// true && expr => expr
|
|
|
|
// false && expr => false
|
|
|
|
path.replaceWith(
|
|
|
|
node.left.value === true ? node.right : node.left
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "||":
|
|
|
|
// true || expr => true
|
|
|
|
// false || expr => expr
|
|
|
|
path.replaceWith(
|
|
|
|
node.left.value === true ? node.left : node.right
|
|
|
|
);
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
BinaryExpression: {
|
|
|
|
exit(path) {
|
|
|
|
const { node } = path;
|
|
|
|
switch (node.operator) {
|
|
|
|
case "==":
|
|
|
|
case "===":
|
|
|
|
case "!=":
|
|
|
|
case "!==":
|
|
|
|
if (t.isLiteral(node.left) && t.isLiteral(node.right)) {
|
|
|
|
// folding == and != check that can be statically evaluated
|
|
|
|
const { confident, value } = path.evaluate();
|
|
|
|
if (confident) {
|
|
|
|
path.replaceWith(t.booleanLiteral(value));
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CallExpression(path) {
|
|
|
|
const { node } = path;
|
|
|
|
if (
|
|
|
|
t.isMemberExpression(node.callee) &&
|
|
|
|
isPDFJSPreprocessor(node.callee.object) &&
|
|
|
|
t.isIdentifier(node.callee.property) &&
|
|
|
|
!node.callee.computed
|
|
|
|
) {
|
|
|
|
// PDFJSDev.xxxx(arg1, arg2, ...) => transform
|
|
|
|
const action = node.callee.property.name;
|
|
|
|
const result = handlePreprocessorAction(
|
|
|
|
ctx,
|
|
|
|
action,
|
|
|
|
node.arguments,
|
|
|
|
path
|
|
|
|
);
|
|
|
|
path.replaceWith(t.inherits(t.valueToNode(result), path.node));
|
2016-11-12 06:05:17 +09:00
|
|
|
}
|
2024-02-08 20:13:30 +09:00
|
|
|
|
|
|
|
if (t.isIdentifier(node.callee, { name: "__non_webpack_import__" })) {
|
|
|
|
if (node.arguments.length !== 1) {
|
|
|
|
throw new Error("Invalid `__non_webpack_import__` usage.");
|
|
|
|
}
|
|
|
|
// Replace it with a standard `import`-call and
|
|
|
|
// ensure that Webpack will leave it alone.
|
|
|
|
const source = node.arguments[0];
|
|
|
|
source.leadingComments = [
|
|
|
|
{
|
|
|
|
type: "CommentBlock",
|
|
|
|
value: "webpackIgnore: true",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
path.replaceWith(t.importExpression(source));
|
|
|
|
}
|
2024-01-23 02:33:31 +09:00
|
|
|
},
|
|
|
|
BlockStatement: {
|
|
|
|
// Visit node in post-order so that recursive flattening
|
|
|
|
// of blocks works correctly.
|
|
|
|
exit(path) {
|
|
|
|
const { node } = path;
|
2016-05-11 08:05:29 +09:00
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
let subExpressionIndex = 0;
|
|
|
|
while (subExpressionIndex < node.body.length) {
|
|
|
|
switch (node.body[subExpressionIndex].type) {
|
|
|
|
case "EmptyStatement":
|
|
|
|
// Removing empty statements from the blocks.
|
|
|
|
node.body.splice(subExpressionIndex, 1);
|
|
|
|
continue;
|
|
|
|
case "BlockStatement":
|
|
|
|
// Block statements inside a block are flattened
|
|
|
|
// into the parent one.
|
|
|
|
const subChildren = node.body[subExpressionIndex].body;
|
|
|
|
node.body.splice(subExpressionIndex, 1, ...subChildren);
|
|
|
|
subExpressionIndex += Math.max(subChildren.length - 1, 0);
|
|
|
|
continue;
|
|
|
|
case "ReturnStatement":
|
|
|
|
case "ThrowStatement":
|
|
|
|
// Removing dead code after return or throw.
|
|
|
|
node.body.splice(
|
|
|
|
subExpressionIndex + 1,
|
|
|
|
node.body.length - subExpressionIndex - 1
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
subExpressionIndex++;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Function: {
|
|
|
|
exit(path) {
|
|
|
|
if (!t.isBlockStatement(path.node.body)) {
|
|
|
|
// Arrow function with expression body
|
|
|
|
return;
|
|
|
|
}
|
2016-05-11 08:05:29 +09:00
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
const { body } = path.node.body;
|
|
|
|
if (
|
|
|
|
body.length > 0 &&
|
|
|
|
t.isReturnStatement(body.at(-1), { argument: null })
|
|
|
|
) {
|
|
|
|
// Function body ends with return without arg -- removing it.
|
|
|
|
body.pop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-10-06 20:34:45 +09:00
|
|
|
},
|
2016-05-11 08:05:29 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-23 02:33:31 +09:00
|
|
|
function preprocessPDFJSCode(ctx, content) {
|
|
|
|
return transformSync(content, {
|
|
|
|
configFile: false,
|
|
|
|
plugins: [[babelPluginPDFJSPreprocessor, ctx]],
|
|
|
|
}).code;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { babelPluginPDFJSPreprocessor, preprocessPDFJSCode };
|