mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-03 04:47:21 +02:00
Compare commits
No commits in common. "d4d0081ac920fa5fa7ba98eae4fe8b90042375be" and "2ed959d75ab04f67b3762655b82aa02a4086d62f" have entirely different histories.
d4d0081ac9
...
2ed959d75a
@ -47,25 +47,6 @@ function handlePreprocessorAction(ctx, actionName, args, path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function babelPluginPDFJSPreprocessor(babel, ctx) {
|
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 {
|
return {
|
||||||
name: "babel-plugin-pdfjs-preprocessor",
|
name: "babel-plugin-pdfjs-preprocessor",
|
||||||
manipulateOptions({ parserOpts }) {
|
manipulateOptions({ parserOpts }) {
|
||||||
@ -192,6 +173,36 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
|
|||||||
path.replaceWith(t.importExpression(source));
|
path.replaceWith(t.importExpression(source));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
NewExpression(path) {
|
||||||
|
const { node } = path;
|
||||||
|
|
||||||
|
if (
|
||||||
|
t.isIdentifier(node.callee, { name: "URL" }) &&
|
||||||
|
node.arguments.length === 2
|
||||||
|
) {
|
||||||
|
const [arg1, arg2] = node.arguments;
|
||||||
|
|
||||||
|
if (
|
||||||
|
arg1.type === "StringLiteral" &&
|
||||||
|
arg1.value.endsWith(".wasm") &&
|
||||||
|
arg2.type === "MemberExpression"
|
||||||
|
) {
|
||||||
|
// This statement is generated by the Emscripten Compiler (emcc),
|
||||||
|
// however we're manually loading wasm-files and we want to ensure
|
||||||
|
// that bundlers will leave it alone; this *must* include Webpack.
|
||||||
|
arg1.leadingComments = [
|
||||||
|
{
|
||||||
|
type: "CommentBlock",
|
||||||
|
value: "webpackIgnore: true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "CommentBlock",
|
||||||
|
value: "@vite-ignore",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"BlockStatement|StaticBlock": {
|
"BlockStatement|StaticBlock": {
|
||||||
// Visit node in post-order so that recursive flattening
|
// Visit node in post-order so that recursive flattening
|
||||||
// of blocks works correctly.
|
// of blocks works correctly.
|
||||||
@ -244,8 +255,6 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
|
|||||||
// Function body ends with return without arg -- removing it.
|
// Function body ends with return without arg -- removing it.
|
||||||
body.pop();
|
body.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
removeUnusedFunctions(path);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ClassMethod: {
|
ClassMethod: {
|
||||||
@ -268,26 +277,6 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
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));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,4 +8,3 @@ function test() {
|
|||||||
}
|
}
|
||||||
"4";
|
"4";
|
||||||
}
|
}
|
||||||
test();
|
|
||||||
|
|||||||
1
external/builder/fixtures_babel/blocks.js
vendored
1
external/builder/fixtures_babel/blocks.js
vendored
@ -17,4 +17,3 @@ function test() {
|
|||||||
"4";
|
"4";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test();
|
|
||||||
|
|||||||
@ -2,12 +2,10 @@ function f1() {
|
|||||||
"1";
|
"1";
|
||||||
"2";
|
"2";
|
||||||
}
|
}
|
||||||
f1();
|
|
||||||
function f2() {
|
function f2() {
|
||||||
"1";
|
"1";
|
||||||
"2";
|
"2";
|
||||||
}
|
}
|
||||||
f2();
|
|
||||||
function f3() {
|
function f3() {
|
||||||
if ("1") {
|
if ("1") {
|
||||||
"1";
|
"1";
|
||||||
@ -17,4 +15,3 @@ function f3() {
|
|||||||
"4";
|
"4";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f3();
|
|
||||||
|
|||||||
3
external/builder/fixtures_babel/comments.js
vendored
3
external/builder/fixtures_babel/comments.js
vendored
@ -6,7 +6,6 @@ function f1() {
|
|||||||
"2";
|
"2";
|
||||||
/* tail */
|
/* tail */
|
||||||
}
|
}
|
||||||
f1();
|
|
||||||
|
|
||||||
function f2() {
|
function f2() {
|
||||||
// head
|
// head
|
||||||
@ -15,7 +14,6 @@ function f2() {
|
|||||||
"2";
|
"2";
|
||||||
// tail
|
// tail
|
||||||
}
|
}
|
||||||
f2();
|
|
||||||
|
|
||||||
function f3() {
|
function f3() {
|
||||||
if ("1") { // begin block
|
if ("1") { // begin block
|
||||||
@ -26,4 +24,3 @@ function f3() {
|
|||||||
"4";
|
"4";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f3();
|
|
||||||
|
|||||||
@ -1,18 +1,14 @@
|
|||||||
function f1() {}
|
function f1() {}
|
||||||
f1();
|
|
||||||
function f2() {
|
function f2() {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
f2();
|
|
||||||
function f3() {
|
function f3() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
throw "test";
|
throw "test";
|
||||||
}
|
}
|
||||||
f3();
|
|
||||||
function f4() {
|
function f4() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
}
|
}
|
||||||
f4();
|
|
||||||
var obj = {
|
var obj = {
|
||||||
method1() {},
|
method1() {},
|
||||||
method2() {}
|
method2() {}
|
||||||
|
|||||||
4
external/builder/fixtures_babel/deadcode.js
vendored
4
external/builder/fixtures_babel/deadcode.js
vendored
@ -2,20 +2,17 @@ function f1() {
|
|||||||
return;
|
return;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
}
|
}
|
||||||
f1();
|
|
||||||
|
|
||||||
function f2() {
|
function f2() {
|
||||||
return 1;
|
return 1;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
}
|
}
|
||||||
f2();
|
|
||||||
|
|
||||||
function f3() {
|
function f3() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
throw "test";
|
throw "test";
|
||||||
var j = 0;
|
var j = 0;
|
||||||
}
|
}
|
||||||
f3();
|
|
||||||
|
|
||||||
function f4() {
|
function f4() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
@ -25,7 +22,6 @@ function f4() {
|
|||||||
throw "test";
|
throw "test";
|
||||||
var j = 0;
|
var j = 0;
|
||||||
}
|
}
|
||||||
f4();
|
|
||||||
|
|
||||||
var obj = {
|
var obj = {
|
||||||
method1() { return; var i = 0; },
|
method1() { return; var i = 0; },
|
||||||
|
|||||||
@ -16,4 +16,3 @@ if ('1') {
|
|||||||
function f1() {
|
function f1() {
|
||||||
"1";
|
"1";
|
||||||
}
|
}
|
||||||
f1();
|
|
||||||
|
|||||||
1
external/builder/fixtures_babel/ifs.js
vendored
1
external/builder/fixtures_babel/ifs.js
vendored
@ -32,4 +32,3 @@ function f1() {
|
|||||||
"2";
|
"2";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f1();
|
|
||||||
|
|||||||
4
external/builder/fixtures_babel/ignore-expected.js
vendored
Normal file
4
external/builder/fixtures_babel/ignore-expected.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
const wasmUrl = new URL(
|
||||||
|
/*webpackIgnore: true*/
|
||||||
|
/*@vite-ignore*/
|
||||||
|
"qwerty.wasm", import.meta.url);
|
||||||
1
external/builder/fixtures_babel/ignore.js
vendored
Normal file
1
external/builder/fixtures_babel/ignore.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
const wasmUrl = new URL("qwerty.wasm" , import.meta.url);
|
||||||
@ -1,5 +0,0 @@
|
|||||||
function usedByUsed() {}
|
|
||||||
function used() {
|
|
||||||
usedByUsed();
|
|
||||||
}
|
|
||||||
used();
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
function usedByUsed() {}
|
|
||||||
function usedByUnused() {}
|
|
||||||
function usedByRemovedCode() {}
|
|
||||||
|
|
||||||
function used() {
|
|
||||||
usedByUsed();
|
|
||||||
return;
|
|
||||||
usedByRemovedCode();
|
|
||||||
}
|
|
||||||
function unused() {
|
|
||||||
usedByUnused();
|
|
||||||
}
|
|
||||||
|
|
||||||
used();
|
|
||||||
27
gulpfile.mjs
27
gulpfile.mjs
@ -339,33 +339,6 @@ function createWebpackConfig(
|
|||||||
new webpack2.BannerPlugin({ banner: licenseHeaderLibre, raw: true })
|
new webpack2.BannerPlugin({ banner: licenseHeaderLibre, raw: true })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
plugins.push({
|
|
||||||
/** @param {import('webpack').Compiler} compiler */
|
|
||||||
apply(compiler) {
|
|
||||||
const errors = [];
|
|
||||||
|
|
||||||
compiler.hooks.afterCompile.tap("VerifyImportMeta", compilation => {
|
|
||||||
for (const asset of compilation.getAssets()) {
|
|
||||||
if (asset.name.endsWith(".mjs")) {
|
|
||||||
const source = asset.source.source();
|
|
||||||
if (
|
|
||||||
typeof source === "string" &&
|
|
||||||
/new URL\([^,)]*,\s*import\.meta\.url/.test(source)
|
|
||||||
) {
|
|
||||||
errors.push(
|
|
||||||
`Output module ${asset.name} uses new URL(..., import.meta.url)`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
compiler.hooks.afterEmit.tap("VerifyImportMeta", compilation => {
|
|
||||||
// Emit the errors after emitting the files, so that it's possible to
|
|
||||||
// look at the contents of the invalid bundle.
|
|
||||||
compilation.errors.push(...errors);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const alias = createWebpackAlias(bundleDefines);
|
const alias = createWebpackAlias(bundleDefines);
|
||||||
const experiments = isModule ? { outputModule: true } : undefined;
|
const experiments = isModule ? { outputModule: true } : undefined;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user