Skip to content

Commit d4d0081

Browse files
Merge pull request #19923 from nicolo-ribaudo/fix-new-url
Remove unused OpenJPEG wasm fallback logic
2 parents 2ed959d + aebe0cb commit d4d0081

14 files changed

+105
-35
lines changed

external/builder/babel-plugin-pdfjs-preprocessor.mjs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ function handlePreprocessorAction(ctx, actionName, args, path) {
4747
}
4848

4949
function babelPluginPDFJSPreprocessor(babel, ctx) {
50+
function removeUnusedFunctions(path) {
51+
let removed;
52+
do {
53+
removed = false;
54+
path.scope.crawl();
55+
for (const name in path.scope.bindings) {
56+
const binding = path.scope.bindings[name];
57+
if (!binding.referenced) {
58+
const { path: bindingPath } = binding;
59+
if (bindingPath.isFunctionDeclaration()) {
60+
bindingPath.remove();
61+
removed = true;
62+
}
63+
}
64+
}
65+
// If we removed some functions, there might be new unused ones
66+
} while (removed);
67+
}
68+
5069
return {
5170
name: "babel-plugin-pdfjs-preprocessor",
5271
manipulateOptions({ parserOpts }) {
@@ -173,36 +192,6 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
173192
path.replaceWith(t.importExpression(source));
174193
}
175194
},
176-
NewExpression(path) {
177-
const { node } = path;
178-
179-
if (
180-
t.isIdentifier(node.callee, { name: "URL" }) &&
181-
node.arguments.length === 2
182-
) {
183-
const [arg1, arg2] = node.arguments;
184-
185-
if (
186-
arg1.type === "StringLiteral" &&
187-
arg1.value.endsWith(".wasm") &&
188-
arg2.type === "MemberExpression"
189-
) {
190-
// This statement is generated by the Emscripten Compiler (emcc),
191-
// however we're manually loading wasm-files and we want to ensure
192-
// that bundlers will leave it alone; this *must* include Webpack.
193-
arg1.leadingComments = [
194-
{
195-
type: "CommentBlock",
196-
value: "webpackIgnore: true",
197-
},
198-
{
199-
type: "CommentBlock",
200-
value: "@vite-ignore",
201-
},
202-
];
203-
}
204-
}
205-
},
206195
"BlockStatement|StaticBlock": {
207196
// Visit node in post-order so that recursive flattening
208197
// of blocks works correctly.
@@ -255,6 +244,8 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
255244
// Function body ends with return without arg -- removing it.
256245
body.pop();
257246
}
247+
248+
removeUnusedFunctions(path);
258249
},
259250
},
260251
ClassMethod: {
@@ -277,6 +268,26 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
277268
}
278269
},
279270
},
271+
Program: {
272+
exit(path) {
273+
if (path.node.sourceType === "module") {
274+
removeUnusedFunctions(path);
275+
}
276+
},
277+
},
278+
MemberExpression(path) {
279+
// The Emscripten Compiler (emcc) generates code that allows the caller
280+
// to provide the Wasm module (thorugh Module.instantiateWasm), with
281+
// a fallback in case .instantiateWasm is not provided.
282+
// We always define instantiateWasm, so we can hard-code the check
283+
// and let our dead code elimination logic remove the unused fallback.
284+
if (
285+
path.parentPath.isIfStatement({ test: path.node }) &&
286+
path.matchesPattern("Module.instantiateWasm")
287+
) {
288+
path.replaceWith(t.booleanLiteral(true));
289+
}
290+
},
280291
},
281292
};
282293
}

external/builder/fixtures_babel/blocks-expected.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ function test() {
88
}
99
"4";
1010
}
11+
test();

external/builder/fixtures_babel/blocks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ function test() {
1717
"4";
1818
}
1919
}
20+
test();

external/builder/fixtures_babel/comments-expected.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ function f1() {
22
"1";
33
"2";
44
}
5+
f1();
56
function f2() {
67
"1";
78
"2";
89
}
10+
f2();
911
function f3() {
1012
if ("1") {
1113
"1";
@@ -15,3 +17,4 @@ function f3() {
1517
"4";
1618
}
1719
}
20+
f3();

external/builder/fixtures_babel/comments.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function f1() {
66
"2";
77
/* tail */
88
}
9+
f1();
910

1011
function f2() {
1112
// head
@@ -14,6 +15,7 @@ function f2() {
1415
"2";
1516
// tail
1617
}
18+
f2();
1719

1820
function f3() {
1921
if ("1") { // begin block
@@ -24,3 +26,4 @@ function f3() {
2426
"4";
2527
}
2628
}
29+
f3();

external/builder/fixtures_babel/deadcode-expected.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
function f1() {}
2+
f1();
23
function f2() {
34
return 1;
45
}
6+
f2();
57
function f3() {
68
var i = 0;
79
throw "test";
810
}
11+
f3();
912
function f4() {
1013
var i = 0;
1114
}
15+
f4();
1216
var obj = {
1317
method1() {},
1418
method2() {}

external/builder/fixtures_babel/deadcode.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ function f1() {
22
return;
33
var i = 0;
44
}
5+
f1();
56

67
function f2() {
78
return 1;
89
var i = 0;
910
}
11+
f2();
1012

1113
function f3() {
1214
var i = 0;
1315
throw "test";
1416
var j = 0;
1517
}
18+
f3();
1619

1720
function f4() {
1821
var i = 0;
@@ -22,6 +25,7 @@ function f4() {
2225
throw "test";
2326
var j = 0;
2427
}
28+
f4();
2529

2630
var obj = {
2731
method1() { return; var i = 0; },

external/builder/fixtures_babel/ifs-expected.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ if ('1') {
1616
function f1() {
1717
"1";
1818
}
19+
f1();

external/builder/fixtures_babel/ifs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ function f1() {
3232
"2";
3333
}
3434
}
35+
f1();

external/builder/fixtures_babel/ignore-expected.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)