21
21
22
22
Usage:
23
23
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]
25
25
26
26
--preload ,
27
27
--embed See emcc --help for more details on those options.
@@ -129,6 +129,7 @@ def __init__(self):
129
129
self .use_preload_plugins = False
130
130
self .support_node = True
131
131
self .wasm64 = False
132
+ self .modularize = False
132
133
133
134
134
135
class DataFile :
@@ -158,7 +159,8 @@ def has_hidden_attribute(filepath):
158
159
return False
159
160
160
161
try :
161
- attrs = ctypes .windll .kernel32 .GetFileAttributesW (filepath )
162
+ attrs = ctypes .windll .kernel32 .GetFileAttributesW (
163
+ u'%s' % filepath )
162
164
assert attrs != - 1
163
165
result = bool (attrs & 2 )
164
166
except Exception :
@@ -284,7 +286,7 @@ def generate_object_file(data_files):
284
286
# The name of file
285
287
{ f .c_symbol_name } _name:
286
288
.asciz "{ dstpath } "
287
- .size { f .c_symbol_name } _name, { len (dstpath ) + 1 }
289
+ .size { f .c_symbol_name } _name, { len (dstpath )+ 1 }
288
290
289
291
# The size of the file followed by the content itself
290
292
{ f .c_symbol_name } :
@@ -318,6 +320,8 @@ def generate_object_file(data_files):
318
320
# A list of triples of:
319
321
# (file_name_ptr, file_data_size, file_data_ptr)
320
322
# 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
321
325
.section .rodata.__emscripten_embedded_file_data,"",@
322
326
__emscripten_embedded_file_data:
323
327
.p2align { align }
@@ -362,26 +366,16 @@ def main(): # noqa: C901, PLR0912, PLR0915
362
366
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
363
367
"""
364
368
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.''' )
367
371
return 1
368
372
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 ]
380
374
data_files = []
381
375
plugins = []
382
376
leading = ''
383
377
384
- for arg in args [ 1 :]:
378
+ for arg in sys . argv [ 2 :]:
385
379
if arg == '--preload' :
386
380
leading = 'preload'
387
381
elif arg == '--embed' :
@@ -391,6 +385,9 @@ def main(): # noqa: C901, PLR0912, PLR0915
391
385
elif arg == '--no-force' :
392
386
options .force = False
393
387
leading = ''
388
+ elif arg == '--modularize' :
389
+ options .modularize = True
390
+ leading = ''
394
391
elif arg == '--use-preload-cache' :
395
392
options .use_preload_cache = True
396
393
leading = ''
@@ -604,6 +601,7 @@ def was_seen(name):
604
601
f .write (escape_for_makefile (dependency ))
605
602
f .write (' \\ \n ' )
606
603
604
+
607
605
return 0
608
606
609
607
@@ -621,17 +619,30 @@ def generate_js(data_target, data_files, metadata):
621
619
if options .from_emcc :
622
620
ret = ''
623
621
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 = '''
625
631
var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n ''' % {"EXPORT_NAME" : options .export_name }
626
632
627
- ret += '''
633
+ ret += '''
628
634
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 '''
635
646
636
647
if options .support_node :
637
648
ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n "
@@ -1137,10 +1148,27 @@ def generate_js(data_target, data_files, metadata):
1137
1148
ret += '''
1138
1149
}
1139
1150
loadPackage(%s);\n ''' % json .dumps (metadata )
1151
+
1152
+ if options .modularize and not options .from_emcc :
1153
+ ret += '''
1154
+ });'''
1140
1155
1141
1156
ret += '''
1142
1157
})();\n '''
1143
1158
1159
+ if options .modularize and not options .from_emcc :
1160
+ ret += '''
1161
+ (() => {
1162
+ var real_createModule = createModule;
1163
+
1164
+ createModule = function (moduleArg) {
1165
+ if (new.target) throw new Error("createModule() should not be called with `new createModule()`");
1166
+ return real_createModule(moduleArg);
1167
+ }
1168
+ })();
1169
+
1170
+ export default createModule;'''
1171
+
1144
1172
return ret
1145
1173
1146
1174
0 commit comments