@@ -209,6 +209,7 @@ export function minify(
209209 }
210210
211211 const structs = new Set < string > ( )
212+ const externals = new Set < string > ( )
212213 const externalTypes = new Set < string > ( )
213214
214215 // Top-level pass for externals and type definitions
@@ -225,6 +226,35 @@ export function minify(
225226 structs . add ( statement . typeSpecifier . typeSpecifier . name )
226227 if ( isExternal ) externalTypes . add ( statement . typeSpecifier . typeSpecifier . name )
227228 }
229+
230+ if ( isExternal ) {
231+ if ( statement . id ) {
232+ externals . add ( statement . id . name )
233+ } else {
234+ for ( const member of statement . members ) {
235+ if ( member . type !== 'VariableDeclaration' ) continue
236+
237+ for ( const decl of member . declarations ) {
238+ if ( decl . id . type === 'Identifier' ) {
239+ externals . add ( decl . id . name )
240+ } else if ( decl . id . type === 'ArraySpecifier' ) {
241+ externals . add ( ( decl . id as unknown as ArraySpecifier ) . typeSpecifier . name )
242+ }
243+ }
244+ }
245+ }
246+ }
247+ } else if ( statement . type === 'VariableDeclaration' ) {
248+ for ( const decl of statement . declarations ) {
249+ const isExternal = decl . qualifiers . some ( isStorage )
250+ if ( isExternal ) {
251+ if ( decl . id . type === 'Identifier' ) {
252+ externals . add ( decl . id . name )
253+ } else if ( decl . id . type === 'ArraySpecifier' ) {
254+ externals . add ( ( decl . id as unknown as ArraySpecifier ) . typeSpecifier . name )
255+ }
256+ }
257+ }
228258 }
229259 }
230260
@@ -350,15 +380,31 @@ export function minify(
350380 }
351381 } ,
352382 PreprocessorStatement ( node ) {
353- const value = node . value ?. [ 0 ]
354- if ( node . name === 'define' && value ) {
355- const isExternal = false
356- if ( value . type === 'Identifier' ) {
357- mangleName ( value . name , isExternal )
358- } else if ( value . type === 'MemberExpression' ) {
383+ if ( node . name === 'define' && node . value ) {
384+ const [ name , value ] = node . value
385+
386+ let isExternal = false
387+
388+ if ( value ) {
389+ if ( value . type === 'Identifier' ) {
390+ isExternal ||= externals . has ( value . name ) || externalTypes . has ( value . name )
391+ if ( ! isExternal || mangleExternals ) mangleName ( value . name , isExternal )
392+ } else if ( value . type === 'MemberExpression' ) {
393+ // TODO: this needs to be more robust to handle string replacement
394+ } else if ( value . type === 'CallExpression' && value . callee . type === 'Identifier' ) {
395+ isExternal ||= externals . has ( value . callee . name )
396+ if ( ! isExternal || mangleExternals ) mangleName ( value . callee . name , isExternal )
397+ }
398+ }
399+
400+ if ( name . type === 'Identifier' ) {
401+ isExternal ||= externals . has ( name . name ) || externalTypes . has ( name . name )
402+ if ( ! isExternal || mangleExternals ) mangleName ( name . name , isExternal )
403+ } else if ( name . type === 'MemberExpression' ) {
359404 // TODO: this needs to be more robust to handle string replacement
360- } else if ( value . type === 'CallExpression' && value . callee . type === 'Identifier' ) {
361- mangleName ( value . callee . name , isExternal )
405+ } else if ( name . type === 'CallExpression' && name . callee . type === 'Identifier' ) {
406+ isExternal ||= externals . has ( name . callee . name )
407+ if ( ! isExternal || mangleExternals ) mangleName ( name . callee . name , isExternal )
362408 }
363409 }
364410 } ,
0 commit comments