@@ -47,6 +47,25 @@ function handlePreprocessorAction(ctx, actionName, args, path) {
4747}
4848
4949function 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}
0 commit comments