2121
2222Usage:
2323
24- file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help ]
24+ file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node]
2525
2626 --preload ,
2727 --embed See emcc --help for more details on those options.
@@ -129,6 +129,7 @@ def __init__(self):
129129 self .use_preload_plugins = False
130130 self .support_node = True
131131 self .wasm64 = False
132+ self .modularize = False
132133
133134
134135class DataFile :
@@ -158,7 +159,8 @@ def has_hidden_attribute(filepath):
158159 return False
159160
160161 try :
161- attrs = ctypes .windll .kernel32 .GetFileAttributesW (filepath )
162+ attrs = ctypes .windll .kernel32 .GetFileAttributesW (
163+ u'%s' % filepath )
162164 assert attrs != - 1
163165 result = bool (attrs & 2 )
164166 except Exception :
@@ -284,7 +286,7 @@ def generate_object_file(data_files):
284286 # The name of file
285287 { f .c_symbol_name } _name:
286288 .asciz "{ dstpath } "
287- .size { f .c_symbol_name } _name, { len (dstpath ) + 1 }
289+ .size { f .c_symbol_name } _name, { len (dstpath )+ 1 }
288290
289291 # The size of the file followed by the content itself
290292 { f .c_symbol_name } :
@@ -318,6 +320,8 @@ def generate_object_file(data_files):
318320 # A list of triples of:
319321 # (file_name_ptr, file_data_size, file_data_ptr)
320322 # The list in null terminate with a single 0
323+ .globl __emscripten_embedded_file_data
324+ .export_name __emscripten_embedded_file_data, __emscripten_embedded_file_data
321325 .section .rodata.__emscripten_embedded_file_data,"",@
322326 __emscripten_embedded_file_data:
323327 .p2align { align }
@@ -362,26 +366,16 @@ def main(): # noqa: C901, PLR0912, PLR0915
362366 To revalidate these numbers, run `ruff check --select=C901,PLR091`.
363367 """
364368 if len (sys .argv ) == 1 :
365- err ('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help ]
366- Try 'file_packager --help' for more details.''' )
369+ err ('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node]
370+ See the source for more details.''' )
367371 return 1
368372
369- # read response files very early on
370- try :
371- args = substitute_response_files (sys .argv [1 :])
372- except OSError as e :
373- shared .exit_with_error (e )
374-
375- if '--help' in args :
376- print (__doc__ .strip ())
377- return 0
378-
379- data_target = args [0 ]
373+ data_target = sys .argv [1 ]
380374 data_files = []
381375 plugins = []
382376 leading = ''
383377
384- for arg in args [ 1 :]:
378+ for arg in sys . argv [ 2 :]:
385379 if arg == '--preload' :
386380 leading = 'preload'
387381 elif arg == '--embed' :
@@ -391,6 +385,9 @@ def main(): # noqa: C901, PLR0912, PLR0915
391385 elif arg == '--no-force' :
392386 options .force = False
393387 leading = ''
388+ elif arg == '--modularize' :
389+ options .modularize = True
390+ leading = ''
394391 elif arg == '--use-preload-cache' :
395392 options .use_preload_cache = True
396393 leading = ''
@@ -604,6 +601,7 @@ def was_seen(name):
604601 f .write (escape_for_makefile (dependency ))
605602 f .write (' \\ \n ' )
606603
604+
607605 return 0
608606
609607
@@ -621,17 +619,30 @@ def generate_js(data_target, data_files, metadata):
621619 if options .from_emcc :
622620 ret = ''
623621 else :
624- ret = '''
622+ if options .modularize :
623+ ret = '''
624+ var createModule = (() => {
625+
626+ return (async function(moduleArg = {}) {
627+ var Module = moduleArg;
628+ '''
629+ else :
630+ ret = '''
625631 var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n ''' % {"EXPORT_NAME" : options .export_name }
626632
627- ret += '''
633+ ret += '''
628634 Module['expectedDataFileDownloads'] ??= 0;
629- Module['expectedDataFileDownloads']++;
630- (() => {
631- // Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
632- var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
633- var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
634- if (isPthread || isWasmWorker) return;\n '''
635+ Module['expectedDataFileDownloads']++;'''
636+
637+ if not options .modularize and not options .from_emcc :
638+ ret += '''
639+ (() => {'''
640+
641+ ret += '''
642+ // Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
643+ var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
644+ var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
645+ if (isPthread || isWasmWorker) return;\n '''
635646
636647 if options .support_node :
637648 ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n "
@@ -1143,10 +1154,27 @@ def generate_js(data_target, data_files, metadata):
11431154 ret += '''
11441155 }
11451156 loadPackage(%s);\n ''' % json .dumps (metadata )
1157+
1158+ if options .modularize and not options .from_emcc :
1159+ ret += '''
1160+ });'''
11461161
11471162 ret += '''
11481163 })();\n '''
11491164
1165+ if options .modularize and not options .from_emcc :
1166+ ret += '''
1167+ (() => {
1168+ var real_createModule = createModule;
1169+
1170+ createModule = function (moduleArg) {
1171+ if (new.target) throw new Error("createModule() should not be called with `new createModule()`");
1172+ return real_createModule(moduleArg);
1173+ }
1174+ })();
1175+
1176+ export default createModule;'''
1177+
11501178 return ret
11511179
11521180
0 commit comments