first commit
Some checks failed
Types tests / Test (lts/*) (push) Has been cancelled
Lint / Lint (lts/*) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / Test (20) (push) Has been cancelled
CI / Test (22) (push) Has been cancelled
CI / Test (24) (push) Has been cancelled
Some checks failed
Types tests / Test (lts/*) (push) Has been cancelled
Lint / Lint (lts/*) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / Test (20) (push) Has been cancelled
CI / Test (22) (push) Has been cancelled
CI / Test (24) (push) Has been cancelled
This commit is contained in:
302
external/builder/babel-plugin-pdfjs-preprocessor.mjs
vendored
Normal file
302
external/builder/babel-plugin-pdfjs-preprocessor.mjs
vendored
Normal file
@@ -0,0 +1,302 @@
|
||||
import { types as t, transformSync } from "@babel/core";
|
||||
import vm from "vm";
|
||||
|
||||
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
|
||||
|
||||
function isPDFJSPreprocessor(obj) {
|
||||
return obj.type === "Identifier" && obj.name === PDFJS_PREPROCESSOR_NAME;
|
||||
}
|
||||
|
||||
function evalWithDefines(code, defines) {
|
||||
if (!code?.trim()) {
|
||||
throw new Error("No JavaScript expression given");
|
||||
}
|
||||
return vm.runInNewContext(code, defines, { displayErrors: false });
|
||||
}
|
||||
|
||||
function handlePreprocessorAction(ctx, actionName, args, path) {
|
||||
try {
|
||||
const arg = args[0];
|
||||
switch (actionName) {
|
||||
case "test":
|
||||
if (!t.isStringLiteral(arg)) {
|
||||
throw new Error("No code for testing is given");
|
||||
}
|
||||
return !!evalWithDefines(arg.value, ctx.defines);
|
||||
case "eval":
|
||||
if (!t.isStringLiteral(arg)) {
|
||||
throw new Error("No code for eval is given");
|
||||
}
|
||||
const result = evalWithDefines(arg.value, ctx.defines);
|
||||
if (
|
||||
typeof result === "boolean" ||
|
||||
typeof result === "string" ||
|
||||
typeof result === "number" ||
|
||||
typeof result === "object"
|
||||
) {
|
||||
return result;
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw new Error("Unsupported action");
|
||||
} catch (e) {
|
||||
throw path.buildCodeFrameError(
|
||||
`Could not process ${PDFJS_PREPROCESSOR_NAME}.${actionName}: ${e.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function babelPluginPDFJSPreprocessor(babel, ctx) {
|
||||
function removeUnusedFunctions(path) {
|
||||
let removed;
|
||||
do {
|
||||
removed = false;
|
||||
path.scope.crawl();
|
||||
for (const name in path.scope.bindings) {
|
||||
const binding = path.scope.bindings[name];
|
||||
if (!binding.referenced) {
|
||||
const { path: bindingPath } = binding;
|
||||
if (bindingPath.isFunctionDeclaration()) {
|
||||
bindingPath.remove();
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we removed some functions, there might be new unused ones
|
||||
} while (removed);
|
||||
}
|
||||
|
||||
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
|
||||
if (node.test.value === true) {
|
||||
path.replaceWith(node.consequent);
|
||||
} else if (node.alternate) {
|
||||
path.replaceWith(node.alternate);
|
||||
} else {
|
||||
path.remove(node);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
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));
|
||||
}
|
||||
},
|
||||
},
|
||||
LogicalExpression: {
|
||||
exit(path) {
|
||||
const { node } = path;
|
||||
if (!t.isBooleanLiteral(node.left)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
},
|
||||
},
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
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));
|
||||
}
|
||||
|
||||
if (t.isIdentifier(node.callee, { name: "__raw_import__" })) {
|
||||
if (node.arguments.length !== 1) {
|
||||
throw new Error("Invalid `__raw_import__` usage.");
|
||||
}
|
||||
// Replace it with a standard `import`-call and attempt to ensure that
|
||||
// various bundlers will leave it alone; this *must* include Webpack.
|
||||
const source = node.arguments[0];
|
||||
source.leadingComments = [
|
||||
{
|
||||
type: "CommentBlock",
|
||||
value: "webpackIgnore: true",
|
||||
},
|
||||
{
|
||||
type: "CommentBlock",
|
||||
value: "@vite-ignore",
|
||||
},
|
||||
];
|
||||
path.replaceWith(t.importExpression(source));
|
||||
}
|
||||
},
|
||||
"BlockStatement|StaticBlock": {
|
||||
// Visit node in post-order so that recursive flattening
|
||||
// of blocks works correctly.
|
||||
exit(path) {
|
||||
const { node } = path;
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
if (node.type === "StaticBlock" && node.body.length === 0) {
|
||||
path.remove();
|
||||
}
|
||||
},
|
||||
},
|
||||
Function: {
|
||||
exit(path) {
|
||||
if (!t.isBlockStatement(path.node.body)) {
|
||||
// Arrow function with expression body
|
||||
return;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
removeUnusedFunctions(path);
|
||||
},
|
||||
},
|
||||
ClassMethod: {
|
||||
exit(path) {
|
||||
const {
|
||||
node,
|
||||
parentPath: { parent: classNode },
|
||||
} = path;
|
||||
if (
|
||||
// Remove empty constructors. We only do this for
|
||||
// base classes, as the default constructor of derived
|
||||
// classes is not empty (and an empty constructor
|
||||
// must throw at runtime when constructed).
|
||||
node.kind === "constructor" &&
|
||||
node.body.body.length === 0 &&
|
||||
node.params.every(p => p.type === "Identifier") &&
|
||||
!classNode.superClass
|
||||
) {
|
||||
path.remove();
|
||||
}
|
||||
},
|
||||
},
|
||||
Program: {
|
||||
exit(path) {
|
||||
if (path.node.sourceType === "module") {
|
||||
removeUnusedFunctions(path);
|
||||
}
|
||||
},
|
||||
},
|
||||
MemberExpression(path) {
|
||||
// The Emscripten Compiler (emcc) generates code that allows the caller
|
||||
// to provide the Wasm module (thorugh Module.instantiateWasm), with
|
||||
// a fallback in case .instantiateWasm is not provided.
|
||||
// We always define instantiateWasm, so we can hard-code the check
|
||||
// and let our dead code elimination logic remove the unused fallback.
|
||||
if (
|
||||
path.parentPath.isIfStatement({ test: path.node }) &&
|
||||
path.matchesPattern("Module.instantiateWasm")
|
||||
) {
|
||||
path.replaceWith(t.booleanLiteral(true));
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function preprocessPDFJSCode(ctx, content) {
|
||||
return transformSync(content, {
|
||||
configFile: false,
|
||||
plugins: [[babelPluginPDFJSPreprocessor, ctx]],
|
||||
}).code;
|
||||
}
|
||||
|
||||
export { babelPluginPDFJSPreprocessor, preprocessPDFJSCode };
|
||||
232
external/builder/builder.mjs
vendored
Normal file
232
external/builder/builder.mjs
vendored
Normal file
@@ -0,0 +1,232 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import vm from "vm";
|
||||
|
||||
const AllWhitespaceRegexp = /^\s+$/g;
|
||||
|
||||
/**
|
||||
* A simple preprocessor that is based on the Firefox preprocessor
|
||||
* (https://dxr.mozilla.org/mozilla-central/source/build/docs/preprocessor.rst).
|
||||
* The main difference is that this supports a subset of the commands and it
|
||||
* supports preprocessor commands in HTML-style comments.
|
||||
*
|
||||
* Currently supported commands:
|
||||
* - if
|
||||
* - elif
|
||||
* - else
|
||||
* - endif
|
||||
* - include
|
||||
* - expand
|
||||
* - error
|
||||
*
|
||||
* Every #if must be closed with an #endif. Nested conditions are supported.
|
||||
*
|
||||
* Within an #if or #else block, one level of comment tokens is stripped. This
|
||||
* allows us to write code that can run even without preprocessing. For example:
|
||||
*
|
||||
* //#if SOME_RARE_CONDITION
|
||||
* // // Decrement by one
|
||||
* // --i;
|
||||
* //#else
|
||||
* // // Increment by one.
|
||||
* ++i;
|
||||
* //#endif
|
||||
*/
|
||||
function preprocess(inFilename, outFilename, defines) {
|
||||
let lineNumber = 0;
|
||||
function loc() {
|
||||
return fs.realpathSync(inFilename) + ":" + lineNumber;
|
||||
}
|
||||
|
||||
function expandCssImports(content, baseUrl) {
|
||||
return content.replaceAll(
|
||||
/^\s*@import\s+url\(([^)]+)\);\s*$/gm,
|
||||
function (all, url) {
|
||||
if (defines.GECKOVIEW) {
|
||||
switch (url) {
|
||||
case "annotation_editor_layer_builder.css":
|
||||
return "";
|
||||
}
|
||||
}
|
||||
const file = path.join(path.dirname(baseUrl), url);
|
||||
const imported = fs.readFileSync(file, "utf8").toString();
|
||||
return expandCssImports(imported, file);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// TODO make this really read line by line.
|
||||
let content = fs.readFileSync(inFilename, "utf8").toString();
|
||||
// Handle CSS-imports first, when necessary.
|
||||
if (/\.css$/i.test(inFilename)) {
|
||||
content = expandCssImports(content, inFilename);
|
||||
}
|
||||
const lines = content.split("\n"),
|
||||
totalLines = lines.length;
|
||||
const out = [];
|
||||
let i = 0;
|
||||
function readLine() {
|
||||
if (i < totalLines) {
|
||||
return lines[i++];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const writeLine =
|
||||
typeof outFilename === "function"
|
||||
? outFilename
|
||||
: function (line) {
|
||||
if (!line || AllWhitespaceRegexp.test(line)) {
|
||||
const prevLine = out.at(-1);
|
||||
if (!prevLine || AllWhitespaceRegexp.test(prevLine)) {
|
||||
return; // Avoid adding consecutive blank lines.
|
||||
}
|
||||
}
|
||||
out.push(line);
|
||||
};
|
||||
function evaluateCondition(code) {
|
||||
if (!code?.trim()) {
|
||||
throw new Error("No JavaScript expression given at " + loc());
|
||||
}
|
||||
try {
|
||||
return vm.runInNewContext(code, defines, { displayErrors: false });
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
'Could not evaluate "' +
|
||||
code +
|
||||
'" at ' +
|
||||
loc() +
|
||||
"\n" +
|
||||
e.name +
|
||||
": " +
|
||||
e.message
|
||||
);
|
||||
}
|
||||
}
|
||||
function include(file) {
|
||||
const realPath = fs.realpathSync(inFilename);
|
||||
const dir = path.dirname(realPath);
|
||||
try {
|
||||
let fullpath;
|
||||
if (file.indexOf("$ROOT/") === 0) {
|
||||
fullpath = path.join(
|
||||
__dirname,
|
||||
"../..",
|
||||
file.substring("$ROOT/".length)
|
||||
);
|
||||
} else {
|
||||
fullpath = path.join(dir, file);
|
||||
}
|
||||
preprocess(fullpath, writeLine, defines);
|
||||
} catch (e) {
|
||||
if (e.code === "ENOENT") {
|
||||
throw new Error('Failed to include "' + file + '" at ' + loc());
|
||||
}
|
||||
throw e; // Some other error
|
||||
}
|
||||
}
|
||||
function expand(line) {
|
||||
line = line.replaceAll(/__[\w]+__/g, function (variable) {
|
||||
variable = variable.substring(2, variable.length - 2);
|
||||
if (variable in defines) {
|
||||
return defines[variable];
|
||||
}
|
||||
return "";
|
||||
});
|
||||
writeLine(line);
|
||||
}
|
||||
|
||||
// not inside if or else (process lines)
|
||||
const STATE_NONE = 0;
|
||||
// inside if, condition false (ignore until #else or #endif)
|
||||
const STATE_IF_FALSE = 1;
|
||||
// inside else, #if was false, so #else is true (process lines until #endif)
|
||||
const STATE_ELSE_TRUE = 2;
|
||||
// inside if, condition true (process lines until #else or #endif)
|
||||
const STATE_IF_TRUE = 3;
|
||||
// inside else or elif, #if/#elif was true, so following #else or #elif is
|
||||
// false (ignore lines until #endif)
|
||||
const STATE_ELSE_FALSE = 4;
|
||||
|
||||
let line;
|
||||
let state = STATE_NONE;
|
||||
const stack = [];
|
||||
const control =
|
||||
/^(?:\/\/|\s*\/\*|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:\*\/|-->)?$)?/;
|
||||
|
||||
while ((line = readLine()) !== null) {
|
||||
++lineNumber;
|
||||
const m = control.exec(line);
|
||||
if (m) {
|
||||
switch (m[1]) {
|
||||
case "if":
|
||||
stack.push(state);
|
||||
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
||||
break;
|
||||
case "elif":
|
||||
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
||||
state = STATE_ELSE_FALSE;
|
||||
} else if (state === STATE_IF_FALSE) {
|
||||
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
||||
} else if (state === STATE_ELSE_TRUE) {
|
||||
throw new Error("Found #elif after #else at " + loc());
|
||||
} else {
|
||||
throw new Error("Found #elif without matching #if at " + loc());
|
||||
}
|
||||
break;
|
||||
case "else":
|
||||
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
||||
state = STATE_ELSE_FALSE;
|
||||
} else if (state === STATE_IF_FALSE) {
|
||||
state = STATE_ELSE_TRUE;
|
||||
} else {
|
||||
throw new Error("Found #else without matching #if at " + loc());
|
||||
}
|
||||
break;
|
||||
case "endif":
|
||||
if (state === STATE_NONE) {
|
||||
throw new Error("Found #endif without #if at " + loc());
|
||||
}
|
||||
state = stack.pop();
|
||||
break;
|
||||
case "expand":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
expand(m[2]);
|
||||
}
|
||||
break;
|
||||
case "include":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
include(m[2]);
|
||||
}
|
||||
break;
|
||||
case "error":
|
||||
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
||||
throw new Error("Found #error " + m[2] + " at " + loc());
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else if (state === STATE_NONE) {
|
||||
writeLine(line);
|
||||
} else if (
|
||||
(state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
|
||||
!stack.includes(STATE_IF_FALSE) &&
|
||||
!stack.includes(STATE_ELSE_FALSE)
|
||||
) {
|
||||
writeLine(
|
||||
line
|
||||
.replaceAll(/^\/\/|^<!--/g, " ")
|
||||
.replaceAll(/(^\s*)\/\*/g, "$1 ")
|
||||
.replaceAll(/\*\/$|-->$/g, "")
|
||||
);
|
||||
}
|
||||
}
|
||||
if (state !== STATE_NONE || stack.length !== 0) {
|
||||
throw new Error(
|
||||
"Missing #endif in preprocessor for " + fs.realpathSync(inFilename)
|
||||
);
|
||||
}
|
||||
if (typeof outFilename !== "function") {
|
||||
fs.writeFileSync(outFilename, out.join("\n"));
|
||||
}
|
||||
}
|
||||
|
||||
export { preprocess };
|
||||
4
external/builder/fixtures/confusing-comment-expected.js
vendored
Normal file
4
external/builder/fixtures/confusing-comment-expected.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
var i = 0;
|
||||
while(i-->0) {
|
||||
}
|
||||
6
external/builder/fixtures/confusing-comment.js
vendored
Normal file
6
external/builder/fixtures/confusing-comment.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
var i = 0;
|
||||
while(i-->0) {
|
||||
}
|
||||
//#endif
|
||||
5
external/builder/fixtures/css-comment-expected.css
vendored
Normal file
5
external/builder/fixtures/css-comment-expected.css
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/* Comment here... */
|
||||
div {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
7
external/builder/fixtures/css-comment.css
vendored
Normal file
7
external/builder/fixtures/css-comment.css
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/* Comment here... */
|
||||
/*#if TRUE*/
|
||||
/*div {*/
|
||||
/* margin: 0;*/
|
||||
/*padding: 0;*/
|
||||
/*}*/
|
||||
/*#endif*/
|
||||
1
external/builder/fixtures/elif-expected.js
vendored
Normal file
1
external/builder/fixtures/elif-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Found #elif without matching #if at __filename:2
|
||||
4
external/builder/fixtures/elif.js
vendored
Normal file
4
external/builder/fixtures/elif.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
//#elif TRUE
|
||||
var a;
|
||||
//#endif
|
||||
1
external/builder/fixtures/else-expected.js
vendored
Normal file
1
external/builder/fixtures/else-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Found #else without matching #if at __filename:2
|
||||
3
external/builder/fixtures/else.js
vendored
Normal file
3
external/builder/fixtures/else.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
//#else
|
||||
//#endif
|
||||
1
external/builder/fixtures/error-expected.js
vendored
Normal file
1
external/builder/fixtures/error-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Found #error "Some Error" at __filename:3
|
||||
2
external/builder/fixtures/error-false-expected.js
vendored
Normal file
2
external/builder/fixtures/error-false-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var a;
|
||||
5
external/builder/fixtures/error-false.js
vendored
Normal file
5
external/builder/fixtures/error-false.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
//#if FALSE
|
||||
//#error "Some Error"
|
||||
//#endif
|
||||
var a;
|
||||
5
external/builder/fixtures/error.js
vendored
Normal file
5
external/builder/fixtures/error.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
//#error "Some Error"
|
||||
//#endif
|
||||
var b;
|
||||
1
external/builder/fixtures/expand-expected.html
vendored
Normal file
1
external/builder/fixtures/expand-expected.html
vendored
Normal file
@@ -0,0 +1 @@
|
||||
prefixtruesuffix
|
||||
1
external/builder/fixtures/expand.html
vendored
Normal file
1
external/builder/fixtures/expand.html
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<!--#expand prefix__TRUE__suffix-->
|
||||
1
external/builder/fixtures/if-empty-expected.js
vendored
Normal file
1
external/builder/fixtures/if-empty-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: No JavaScript expression given at __filename:2
|
||||
6
external/builder/fixtures/if-empty.js
vendored
Normal file
6
external/builder/fixtures/if-empty.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if
|
||||
var a;
|
||||
//#else
|
||||
var b;
|
||||
//#endif
|
||||
2
external/builder/fixtures/if-false-elif-false-else-expected.js
vendored
Normal file
2
external/builder/fixtures/if-false-elif-false-else-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var c;
|
||||
8
external/builder/fixtures/if-false-elif-false-else.js
vendored
Normal file
8
external/builder/fixtures/if-false-elif-false-else.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
//#if FALSE
|
||||
var a;
|
||||
//#elif FALSE
|
||||
var b;
|
||||
//#else
|
||||
var c;
|
||||
//#endif
|
||||
2
external/builder/fixtures/if-false-elif-true-else-expected.js
vendored
Normal file
2
external/builder/fixtures/if-false-elif-true-else-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var b;
|
||||
8
external/builder/fixtures/if-false-elif-true-else.js
vendored
Normal file
8
external/builder/fixtures/if-false-elif-true-else.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
//#if FALSE
|
||||
var a;
|
||||
//#elif TRUE
|
||||
var b;
|
||||
//#else
|
||||
var c;
|
||||
//#endif
|
||||
2
external/builder/fixtures/if-false-else-expected.js
vendored
Normal file
2
external/builder/fixtures/if-false-else-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var b;
|
||||
6
external/builder/fixtures/if-false-else.js
vendored
Normal file
6
external/builder/fixtures/if-false-else.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if FALSE
|
||||
var a;
|
||||
//#else
|
||||
var b;
|
||||
//#endif
|
||||
3
external/builder/fixtures/if-nested-expected.css
vendored
Normal file
3
external/builder/fixtures/if-nested-expected.css
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
div {
|
||||
margin: 0;
|
||||
}
|
||||
6
external/builder/fixtures/if-nested-expected.js
vendored
Normal file
6
external/builder/fixtures/if-nested-expected.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
var a;
|
||||
|
||||
var b;
|
||||
|
||||
var d;
|
||||
14
external/builder/fixtures/if-nested.css
vendored
Normal file
14
external/builder/fixtures/if-nested.css
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/*#if TRUE*/
|
||||
div {
|
||||
margin: 0;
|
||||
/*#if FALSE*/
|
||||
padding: 0;
|
||||
/*#endif*/
|
||||
}
|
||||
/*#endif*/
|
||||
|
||||
/*#if FALSE*/
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
/*#endif*/
|
||||
19
external/builder/fixtures/if-nested.js
vendored
Normal file
19
external/builder/fixtures/if-nested.js
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
var a;
|
||||
|
||||
//#if TRUE
|
||||
var b;
|
||||
//#else
|
||||
var c;
|
||||
//#endif
|
||||
|
||||
var d;
|
||||
//#else
|
||||
var e;
|
||||
//#if TRUE
|
||||
var f;
|
||||
//#endif
|
||||
|
||||
var g;
|
||||
//#endif
|
||||
2
external/builder/fixtures/if-true-elif-false-else-expected.js
vendored
Normal file
2
external/builder/fixtures/if-true-elif-false-else-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var a;
|
||||
8
external/builder/fixtures/if-true-elif-false-else.js
vendored
Normal file
8
external/builder/fixtures/if-true-elif-false-else.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
var a;
|
||||
//#elif FALSE
|
||||
var b;
|
||||
//#else
|
||||
var c;
|
||||
//#endif
|
||||
2
external/builder/fixtures/if-true-else-expected.js
vendored
Normal file
2
external/builder/fixtures/if-true-else-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
var a;
|
||||
6
external/builder/fixtures/if-true-else.js
vendored
Normal file
6
external/builder/fixtures/if-true-else.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
var a;
|
||||
//#else
|
||||
var b;
|
||||
//#endif
|
||||
1
external/builder/fixtures/if-unclosed-expected.js
vendored
Normal file
1
external/builder/fixtures/if-unclosed-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Missing #endif in preprocessor for __filename
|
||||
3
external/builder/fixtures/if-unclosed.js
vendored
Normal file
3
external/builder/fixtures/if-unclosed.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
var a;
|
||||
5
external/builder/fixtures/include-expected.html
vendored
Normal file
5
external/builder/fixtures/include-expected.html
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script>
|
||||
'use strict';
|
||||
var a;
|
||||
|
||||
</script>
|
||||
1
external/builder/fixtures/include-non-existent-expected.html
vendored
Normal file
1
external/builder/fixtures/include-non-existent-expected.html
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Failed to include "some file that does not exist" at __filename:2
|
||||
2
external/builder/fixtures/include-non-existent.html
vendored
Normal file
2
external/builder/fixtures/include-non-existent.html
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
<!-- Non-existent file -->
|
||||
<!--#include some file that does not exist-->
|
||||
5
external/builder/fixtures/include.html
vendored
Normal file
5
external/builder/fixtures/include.html
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script>
|
||||
<!--#if TRUE-->
|
||||
<!--#include if-true-else.js-->
|
||||
<!--#endif-->
|
||||
</script>
|
||||
4
external/builder/fixtures/js-comment-expected.js
vendored
Normal file
4
external/builder/fixtures/js-comment-expected.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
//var a;
|
||||
var b;
|
||||
var c;
|
||||
6
external/builder/fixtures/js-comment.js
vendored
Normal file
6
external/builder/fixtures/js-comment.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if TRUE
|
||||
////var a;
|
||||
//var b;
|
||||
var c;
|
||||
//#endif
|
||||
2
external/builder/fixtures/undefined-define-expected.js
vendored
Normal file
2
external/builder/fixtures/undefined-define-expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
//Error: Could not evaluate "notdefined" at __filename:2
|
||||
//ReferenceError: notdefined is not defined
|
||||
6
external/builder/fixtures/undefined-define.js
vendored
Normal file
6
external/builder/fixtures/undefined-define.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
//#if notdefined
|
||||
var a;
|
||||
//#else
|
||||
var b;
|
||||
//#endif
|
||||
1
external/builder/fixtures/unsupported-ifdef-expected.js
vendored
Normal file
1
external/builder/fixtures/unsupported-ifdef-expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
//Error: Found #endif without #if at __filename:4
|
||||
5
external/builder/fixtures/unsupported-ifdef.js
vendored
Normal file
5
external/builder/fixtures/unsupported-ifdef.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
//#ifdef TRUE
|
||||
//ifdef should not be recognized
|
||||
//#endif
|
||||
var a;
|
||||
11
external/builder/fixtures_babel/blocks-expected.js
vendored
Normal file
11
external/builder/fixtures_babel/blocks-expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
function test() {
|
||||
"test";
|
||||
"1";
|
||||
"2";
|
||||
"3";
|
||||
if ("test") {
|
||||
"5";
|
||||
}
|
||||
"4";
|
||||
}
|
||||
test();
|
||||
20
external/builder/fixtures_babel/blocks.js
vendored
Normal file
20
external/builder/fixtures_babel/blocks.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
function test() {
|
||||
{;}
|
||||
;
|
||||
"test";
|
||||
{
|
||||
"1";
|
||||
if (true) {
|
||||
"2";
|
||||
}
|
||||
;
|
||||
{
|
||||
"3";
|
||||
if ("test") {
|
||||
"5";
|
||||
}
|
||||
}
|
||||
"4";
|
||||
}
|
||||
}
|
||||
test();
|
||||
20
external/builder/fixtures_babel/comments-expected.js
vendored
Normal file
20
external/builder/fixtures_babel/comments-expected.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
function f1() {
|
||||
"1";
|
||||
"2";
|
||||
}
|
||||
f1();
|
||||
function f2() {
|
||||
"1";
|
||||
"2";
|
||||
}
|
||||
f2();
|
||||
function f3() {
|
||||
if ("1") {
|
||||
"1";
|
||||
}
|
||||
"2";
|
||||
if ("3") {
|
||||
"4";
|
||||
}
|
||||
}
|
||||
f3();
|
||||
29
external/builder/fixtures_babel/comments.js
vendored
Normal file
29
external/builder/fixtures_babel/comments.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
/* globals f0 */
|
||||
function f1() {
|
||||
/* head */
|
||||
"1";
|
||||
/* mid */
|
||||
"2";
|
||||
/* tail */
|
||||
}
|
||||
f1();
|
||||
|
||||
function f2() {
|
||||
// head
|
||||
"1";
|
||||
// mid
|
||||
"2";
|
||||
// tail
|
||||
}
|
||||
f2();
|
||||
|
||||
function f3() {
|
||||
if ("1") { // begin block
|
||||
"1";
|
||||
}
|
||||
"2"; // trailing
|
||||
if (/* s */"3"/*e*/) {
|
||||
"4";
|
||||
}
|
||||
}
|
||||
f3();
|
||||
15
external/builder/fixtures_babel/constants-expected.js
vendored
Normal file
15
external/builder/fixtures_babel/constants-expected.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
var a = true;
|
||||
var b = false;
|
||||
var c = '1';
|
||||
var d = false;
|
||||
var e = true;
|
||||
var f = '0';
|
||||
var g = '1';
|
||||
var h = '0';
|
||||
var i = true;
|
||||
var j = false;
|
||||
var k = false;
|
||||
var l = true;
|
||||
var m = false;
|
||||
var n = false;
|
||||
var o = true;
|
||||
15
external/builder/fixtures_babel/constants.js
vendored
Normal file
15
external/builder/fixtures_babel/constants.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
var a = true;
|
||||
var b = false;
|
||||
var c = true && '1';
|
||||
var d = false && '0';
|
||||
var e = true || '1';
|
||||
var f = false || '0';
|
||||
var g = true ? '1' : '0';
|
||||
var h = false ? '1' : '0';
|
||||
var i = 'test' === 'test';
|
||||
var j = 'test' !== 'test';
|
||||
var k = 'test' === 'test2';
|
||||
var l = 'test' !== 'test2';
|
||||
var m = '1' === true;
|
||||
var n = !true;
|
||||
var o = !false;
|
||||
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal file
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
class A {
|
||||
constructor() {
|
||||
console.log("Hi!");
|
||||
}
|
||||
}
|
||||
class B {
|
||||
constructor(x = console.log("Hi!")) {}
|
||||
}
|
||||
class C {
|
||||
constructor({
|
||||
x
|
||||
}) {}
|
||||
}
|
||||
class D {}
|
||||
class E extends A {
|
||||
constructor() {}
|
||||
}
|
||||
class F {
|
||||
constructor() {
|
||||
var a = 0;
|
||||
}
|
||||
}
|
||||
class G {}
|
||||
37
external/builder/fixtures_babel/constructors.js
vendored
Normal file
37
external/builder/fixtures_babel/constructors.js
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
class A {
|
||||
constructor() {
|
||||
console.log("Hi!");
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
constructor(x = console.log("Hi!")) {}
|
||||
}
|
||||
|
||||
class C {
|
||||
constructor({ x }) {}
|
||||
}
|
||||
|
||||
class D {
|
||||
constructor(x, y, z) {}
|
||||
}
|
||||
|
||||
class E extends A {
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
class F {
|
||||
constructor() {
|
||||
if (PDFJSDev.test('TRUE')) {
|
||||
var a = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class G {
|
||||
constructor() {
|
||||
if (PDFJSDev.test('FALSE')) {
|
||||
var a = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
external/builder/fixtures_babel/deadcode-expected.js
vendored
Normal file
25
external/builder/fixtures_babel/deadcode-expected.js
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
function f1() {}
|
||||
f1();
|
||||
function f2() {
|
||||
return 1;
|
||||
}
|
||||
f2();
|
||||
function f3() {
|
||||
var i = 0;
|
||||
throw "test";
|
||||
}
|
||||
f3();
|
||||
function f4() {
|
||||
var i = 0;
|
||||
}
|
||||
f4();
|
||||
var obj = {
|
||||
method1() {},
|
||||
method2() {}
|
||||
};
|
||||
class C {
|
||||
method1() {}
|
||||
method2() {}
|
||||
}
|
||||
var arrow1 = () => {};
|
||||
var arrow2 = () => {};
|
||||
41
external/builder/fixtures_babel/deadcode.js
vendored
Normal file
41
external/builder/fixtures_babel/deadcode.js
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
function f1() {
|
||||
return;
|
||||
var i = 0;
|
||||
}
|
||||
f1();
|
||||
|
||||
function f2() {
|
||||
return 1;
|
||||
var i = 0;
|
||||
}
|
||||
f2();
|
||||
|
||||
function f3() {
|
||||
var i = 0;
|
||||
throw "test";
|
||||
var j = 0;
|
||||
}
|
||||
f3();
|
||||
|
||||
function f4() {
|
||||
var i = 0;
|
||||
if (true) {
|
||||
return;
|
||||
}
|
||||
throw "test";
|
||||
var j = 0;
|
||||
}
|
||||
f4();
|
||||
|
||||
var obj = {
|
||||
method1() { return; var i = 0; },
|
||||
method2() { return; },
|
||||
};
|
||||
|
||||
class C {
|
||||
method1() { return; var i = 0; }
|
||||
method2() { return; }
|
||||
}
|
||||
|
||||
var arrow1 = () => { return; var i = 0; };
|
||||
var arrow2 = () => { return; };
|
||||
18
external/builder/fixtures_babel/evals-expected.js
vendored
Normal file
18
external/builder/fixtures_babel/evals-expected.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
var a = false;
|
||||
var b = true;
|
||||
var c = true;
|
||||
var d = false;
|
||||
var e = true;
|
||||
var f = "text";
|
||||
var g = {
|
||||
obj: {
|
||||
i: 1
|
||||
},
|
||||
j: 2
|
||||
};
|
||||
var i = '0';
|
||||
var j = {
|
||||
i: 1
|
||||
};
|
||||
var k = false;
|
||||
var l = true;
|
||||
11
external/builder/fixtures_babel/evals.js
vendored
Normal file
11
external/builder/fixtures_babel/evals.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var a = typeof PDFJSDev === 'undefined';
|
||||
var b = typeof PDFJSDev !== 'undefined';
|
||||
var c = PDFJSDev.test('TRUE');
|
||||
var d = PDFJSDev.test('FALSE');
|
||||
var e = PDFJSDev.eval('TRUE');
|
||||
var f = PDFJSDev.eval('TEXT');
|
||||
var g = PDFJSDev.eval('OBJ');
|
||||
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
|
||||
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
|
||||
var k = !PDFJSDev.test('TRUE');
|
||||
var l = !PDFJSDev.test('FALSE');
|
||||
19
external/builder/fixtures_babel/ifs-expected.js
vendored
Normal file
19
external/builder/fixtures_babel/ifs-expected.js
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
if ('test') {
|
||||
"1";
|
||||
}
|
||||
{
|
||||
"1";
|
||||
}
|
||||
{
|
||||
"1";
|
||||
}
|
||||
{
|
||||
"2";
|
||||
}
|
||||
if ('1') {
|
||||
"1";
|
||||
}
|
||||
function f1() {
|
||||
"1";
|
||||
}
|
||||
f1();
|
||||
35
external/builder/fixtures_babel/ifs.js
vendored
Normal file
35
external/builder/fixtures_babel/ifs.js
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
if ('test') {
|
||||
"1";
|
||||
}
|
||||
if (true) {
|
||||
"1";
|
||||
}
|
||||
if (true) {
|
||||
"1";
|
||||
} else {
|
||||
"2";
|
||||
}
|
||||
if (false) {
|
||||
"1";
|
||||
}
|
||||
if (false) {
|
||||
"1";
|
||||
} else {
|
||||
"2";
|
||||
}
|
||||
if (true && false) {
|
||||
"1";
|
||||
}
|
||||
if (true && false || '1') {
|
||||
"1";
|
||||
}
|
||||
|
||||
function f1() {
|
||||
if (true) {
|
||||
"1";
|
||||
}
|
||||
if (false) {
|
||||
"2";
|
||||
}
|
||||
}
|
||||
f1();
|
||||
7
external/builder/fixtures_babel/importalias-expected.js
vendored
Normal file
7
external/builder/fixtures_babel/importalias-expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Test } from "import-name";
|
||||
import { Test2 } from './non-alias';
|
||||
export { Test3 } from "import-name";
|
||||
await import(
|
||||
/*webpackIgnore: true*/
|
||||
/*@vite-ignore*/
|
||||
"./non-alias");
|
||||
4
external/builder/fixtures_babel/importalias.js
vendored
Normal file
4
external/builder/fixtures_babel/importalias.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import { Test } from 'import-alias';
|
||||
import { Test2 } from './non-alias';
|
||||
export { Test3 } from 'import-alias';
|
||||
await __raw_import__("./non-alias");
|
||||
8
external/builder/fixtures_babel/staticblock-expected.js
vendored
Normal file
8
external/builder/fixtures_babel/staticblock-expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
static {
|
||||
foo();
|
||||
}
|
||||
static {
|
||||
var a = 0;
|
||||
}
|
||||
}
|
||||
20
external/builder/fixtures_babel/staticblock.js
vendored
Normal file
20
external/builder/fixtures_babel/staticblock.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
class A {
|
||||
static {}
|
||||
static {
|
||||
{ foo() }
|
||||
}
|
||||
static {
|
||||
{;}
|
||||
}
|
||||
static {
|
||||
if (PDFJSDev.test('TRUE')) {
|
||||
var a = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
if (PDFJSDev.test('FALSE')) {
|
||||
var a = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
5
external/builder/fixtures_babel/unused-function-expected.js
vendored
Normal file
5
external/builder/fixtures_babel/unused-function-expected.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
function usedByUsed() {}
|
||||
function used() {
|
||||
usedByUsed();
|
||||
}
|
||||
used();
|
||||
14
external/builder/fixtures_babel/unused-function.js
vendored
Normal file
14
external/builder/fixtures_babel/unused-function.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
function usedByUsed() {}
|
||||
function usedByUnused() {}
|
||||
function usedByRemovedCode() {}
|
||||
|
||||
function used() {
|
||||
usedByUsed();
|
||||
return;
|
||||
usedByRemovedCode();
|
||||
}
|
||||
function unused() {
|
||||
usedByUnused();
|
||||
}
|
||||
|
||||
used();
|
||||
68
external/builder/test-fixtures.mjs
vendored
Normal file
68
external/builder/test-fixtures.mjs
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
import * as builder from "./builder.mjs";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
const __dirname = import.meta.dirname;
|
||||
|
||||
let errors = 0;
|
||||
|
||||
const baseDir = path.join(__dirname, "fixtures");
|
||||
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 outLines = [];
|
||||
|
||||
const outFilename = function (line) {
|
||||
outLines.push(line);
|
||||
};
|
||||
const defines = {
|
||||
TRUE: true,
|
||||
FALSE: false,
|
||||
};
|
||||
let out;
|
||||
try {
|
||||
builder.preprocess(inFilename, outFilename, defines);
|
||||
out = outLines.join("\n").trim();
|
||||
} 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.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);
|
||||
}
|
||||
74
external/builder/test-fixtures_babel.mjs
vendored
Normal file
74
external/builder/test-fixtures_babel.mjs
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { preprocessPDFJSCode } from "./babel-plugin-pdfjs-preprocessor.mjs";
|
||||
|
||||
const __dirname = import.meta.dirname;
|
||||
|
||||
let errors = 0;
|
||||
|
||||
const baseDir = path.join(__dirname, "fixtures_babel");
|
||||
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_babel.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);
|
||||
}
|
||||
Reference in New Issue
Block a user