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.
8484sys .path .insert (0 , __rootdir__ )
8585
8686from tools import shared , utils , js_manipulation
87- from tools .response_file import substitute_response_files
8887
8988
9089DEBUG = os .environ .get ('EMCC_DEBUG' )
@@ -129,6 +128,7 @@ def __init__(self):
129128 self .use_preload_plugins = False
130129 self .support_node = True
131130 self .wasm64 = False
131+ self .modularize = False
132132
133133
134134class DataFile :
@@ -158,7 +158,8 @@ def has_hidden_attribute(filepath):
158158 return False
159159
160160 try :
161- attrs = ctypes .windll .kernel32 .GetFileAttributesW (filepath )
161+ attrs = ctypes .windll .kernel32 .GetFileAttributesW (
162+ u'%s' % filepath )
162163 assert attrs != - 1
163164 result = bool (attrs & 2 )
164165 except Exception :
@@ -284,7 +285,7 @@ def generate_object_file(data_files):
284285 # The name of file
285286 { f .c_symbol_name } _name:
286287 .asciz "{ dstpath } "
287- .size { f .c_symbol_name } _name, { len (dstpath ) + 1 }
288+ .size { f .c_symbol_name } _name, { len (dstpath )+ 1 }
288289
289290 # The size of the file followed by the content itself
290291 { f .c_symbol_name } :
@@ -318,6 +319,8 @@ def generate_object_file(data_files):
318319 # A list of triples of:
319320 # (file_name_ptr, file_data_size, file_data_ptr)
320321 # The list in null terminate with a single 0
322+ .globl __emscripten_embedded_file_data
323+ .export_name __emscripten_embedded_file_data, __emscripten_embedded_file_data
321324 .section .rodata.__emscripten_embedded_file_data,"",@
322325 __emscripten_embedded_file_data:
323326 .p2align { align }
@@ -362,26 +365,16 @@ def main(): # noqa: C901, PLR0912, PLR0915
362365 To revalidate these numbers, run `ruff check --select=C901,PLR091`.
363366 """
364367 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.''' )
368+ 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]
369+ See the source for more details.''' )
367370 return 1
368371
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 ]
372+ data_target = sys .argv [1 ]
380373 data_files = []
381374 plugins = []
382375 leading = ''
383376
384- for arg in args [ 1 :]:
377+ for arg in sys . argv [ 2 :]:
385378 if arg == '--preload' :
386379 leading = 'preload'
387380 elif arg == '--embed' :
@@ -391,6 +384,9 @@ def main(): # noqa: C901, PLR0912, PLR0915
391384 elif arg == '--no-force' :
392385 options .force = False
393386 leading = ''
387+ elif arg == '--modularize' :
388+ options .modularize = True
389+ leading = ''
394390 elif arg == '--use-preload-cache' :
395391 options .use_preload_cache = True
396392 leading = ''
@@ -599,6 +595,7 @@ def was_seen(name):
599595 f .write (escape_for_makefile (dependency ))
600596 f .write (' \\ \n ' )
601597
598+
602599 return 0
603600
604601
@@ -616,17 +613,30 @@ def generate_js(data_target, data_files, metadata):
616613 if options .from_emcc :
617614 ret = ''
618615 else :
619- ret = '''
616+ if options .modularize :
617+ ret = '''
618+ var createModule = (() => {
619+
620+ return (async function(moduleArg = {}) {
621+ var Module = moduleArg;
622+ '''
623+ else :
624+ ret = '''
620625 var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n ''' % {"EXPORT_NAME" : options .export_name }
621626
622- ret += '''
627+ ret += '''
623628 Module['expectedDataFileDownloads'] ??= 0;
624- Module['expectedDataFileDownloads']++;
625- (() => {
626- // Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
627- var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
628- var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
629- if (isPthread || isWasmWorker) return;\n '''
629+ Module['expectedDataFileDownloads']++;'''
630+
631+ if not options .modularize and not options .from_emcc :
632+ ret += '''
633+ (() => {'''
634+
635+ ret += '''
636+ // Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
637+ var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
638+ var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
639+ if (isPthread || isWasmWorker) return;\n '''
630640
631641 if options .support_node :
632642 ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n "
@@ -807,7 +817,13 @@ def generate_js(data_target, data_files, metadata):
807817 return errback();
808818 }'''
809819 code += '''
810- if (typeof indexedDB == 'undefined') {
820+ var indexedDB;
821+ if (typeof window === 'object') {
822+ indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
823+ } else if (typeof location !== 'undefined') {
824+ // worker
825+ indexedDB = self.indexedDB;
826+ } else {
811827 throw 'using IndexedDB to cache data can only be done on a web page or in a web worker';
812828 }
813829 try {
@@ -1147,10 +1163,27 @@ def generate_js(data_target, data_files, metadata):
11471163 ret += '''
11481164 }
11491165 loadPackage(%s);\n ''' % json .dumps (metadata )
1166+
1167+ if options .modularize and not options .from_emcc :
1168+ ret += '''
1169+ });'''
11501170
11511171 ret += '''
11521172 })();\n '''
11531173
1174+ if options .modularize and not options .from_emcc :
1175+ ret += '''
1176+ (() => {
1177+ var real_createModule = createModule;
1178+
1179+ createModule = function (moduleArg) {
1180+ if (new.target) throw new Error("createModule() should not be called with `new createModule()`");
1181+ return real_createModule(moduleArg);
1182+ }
1183+ })();
1184+
1185+ export default createModule;'''
1186+
11541187 return ret
11551188
11561189
0 commit comments