Skip to content

Commit 78b1090

Browse files
authored
Minor refactor of modularization code. NFC (#20961)
The only code output difference here is the use of the `||=` operator.
1 parent 7feec72 commit 78b1090

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

tools/link.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,53 +2341,54 @@ def modularize():
23412341
if settings.MINIMAL_RUNTIME and not settings.PTHREADS:
23422342
# Single threaded MINIMAL_RUNTIME programs do not need access to
23432343
# document.currentScript, so a simple export declaration is enough.
2344-
src = '/** @nocollapse */ var %s=%s' % (settings.EXPORT_NAME, src)
2344+
src = '/** @nocollapse */ var %s = %s' % (settings.EXPORT_NAME, src)
23452345
else:
23462346
script_url_node = ''
23472347
# When MODULARIZE this JS may be executed later,
23482348
# after document.currentScript is gone, so we save it.
2349-
# In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
2350-
# so doesn't have access to 'document'. In this case use 'import.meta' instead.
2349+
# In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module
2350+
# webworker running in strict mode, so doesn't have access to 'document'.
2351+
# In this case use 'import.meta' instead.
23512352
if settings.EXPORT_ES6 and settings.USE_ES6_IMPORT_META:
23522353
script_url = 'import.meta.url'
23532354
else:
23542355
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
23552356
if shared.target_environment_may_be('node'):
2356-
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename;"
2357+
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir ||= __filename;"
23572358
src = '''%(node_imports)s
23582359
var %(EXPORT_NAME)s = (() => {
23592360
var _scriptDir = %(script_url)s;
23602361
%(script_url_node)s
23612362
return (%(src)s);
23622363
})();
2363-
%(capture_module_function_for_audio_worklet)s;
23642364
''' % {
23652365
'node_imports': node_es6_imports(),
23662366
'EXPORT_NAME': settings.EXPORT_NAME,
23672367
'script_url': script_url,
23682368
'script_url_node': script_url_node,
23692369
'src': src,
2370-
# Given the async nature of how the Module function and Module object come into existence in AudioWorkletGlobalScope,
2371-
# store the Module function under a different variable name so that AudioWorkletGlobalScope will be able to reference
2372-
# it without aliasing/conflicting with the Module variable name.
2373-
'capture_module_function_for_audio_worklet': 'globalThis.AudioWorkletModule = ' + settings.EXPORT_NAME if settings.AUDIO_WORKLET and settings.MODULARIZE else ''
23742370
}
2375-
2376-
final_js += '.modular.js'
2377-
with open(final_js, 'w', encoding='utf-8') as f:
2378-
f.write(src)
2379-
2380-
# Export using a UMD style export, or ES6 exports if selected
2381-
if settings.EXPORT_ES6:
2382-
f.write('export default %s;' % settings.EXPORT_NAME)
2383-
elif not settings.MINIMAL_RUNTIME:
2384-
f.write('''\
2371+
# Given the async nature of how the Module function and Module object
2372+
# come into existence in AudioWorkletGlobalScope, store the Module
2373+
# function under a different variable name so that AudioWorkletGlobalScope
2374+
# will be able to reference it without aliasing/conflicting with the
2375+
# Module variable name.
2376+
if settings.AUDIO_WORKLET and settings.MODULARIZE:
2377+
src += f'globalThis.AudioWorkletModule = {settings.EXPORT_NAME};'
2378+
2379+
# Export using a UMD style export, or ES6 exports if selected
2380+
if settings.EXPORT_ES6:
2381+
src += 'export default %s;' % settings.EXPORT_NAME
2382+
elif not settings.MINIMAL_RUNTIME:
2383+
src += '''\
23852384
if (typeof exports === 'object' && typeof module === 'object')
23862385
module.exports = %(EXPORT_NAME)s;
23872386
else if (typeof define === 'function' && define['amd'])
23882387
define([], () => %(EXPORT_NAME)s);
2389-
''' % {'EXPORT_NAME': settings.EXPORT_NAME})
2388+
''' % {'EXPORT_NAME': settings.EXPORT_NAME}
23902389

2390+
final_js += '.modular.js'
2391+
write_file(final_js, src)
23912392
shared.get_temp_files().note(final_js)
23922393
save_intermediate('modularized')
23932394

0 commit comments

Comments
 (0)