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 "
@@ -812,7 +823,13 @@ def generate_js(data_target, data_files, metadata):
812
823
return errback();
813
824
}'''
814
825
code += '''
815
- if (typeof indexedDB == 'undefined') {
826
+ var indexedDB;
827
+ if (typeof window === 'object') {
828
+ indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
829
+ } else if (typeof location !== 'undefined') {
830
+ // worker
831
+ indexedDB = self.indexedDB;
832
+ } else {
816
833
throw 'using IndexedDB to cache data can only be done on a web page or in a web worker';
817
834
}
818
835
try {
@@ -1152,10 +1169,27 @@ def generate_js(data_target, data_files, metadata):
1152
1169
ret += '''
1153
1170
}
1154
1171
loadPackage(%s);\n ''' % json .dumps (metadata )
1172
+
1173
+ if options .modularize and not options .from_emcc :
1174
+ ret += '''
1175
+ });'''
1155
1176
1156
1177
ret += '''
1157
1178
})();\n '''
1158
1179
1180
+ if options .modularize and not options .from_emcc :
1181
+ ret += '''
1182
+ (() => {
1183
+ var real_createModule = createModule;
1184
+
1185
+ createModule = function (moduleArg) {
1186
+ if (new.target) throw new Error("createModule() should not be called with `new createModule()`");
1187
+ return real_createModule(moduleArg);
1188
+ }
1189
+ })();
1190
+
1191
+ export default createModule;'''
1192
+
1159
1193
return ret
1160
1194
1161
1195
0 commit comments