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] [--export-es6] [-- help]
25
25
26
26
--preload ,
27
27
--embed See emcc --help for more details on those options.
41
41
42
42
--export-name=EXPORT_NAME Use custom export name (default is `Module`)
43
43
44
+ --export-es6 Wrap generated code inside ES6 exported function
45
+
44
46
--no-force Don't create output if no valid input file is specified.
45
47
46
48
--use-preload-cache Stores package in IndexedDB so that subsequent loads don't need to do XHR. Checks package version.
@@ -129,6 +131,7 @@ def __init__(self):
129
131
self .use_preload_plugins = False
130
132
self .support_node = True
131
133
self .wasm64 = False
134
+ self .export_es6 = False
132
135
133
136
134
137
class DataFile :
@@ -362,7 +365,7 @@ def main(): # noqa: C901, PLR0912, PLR0915
362
365
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
363
366
"""
364
367
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]
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] [--export-es6] [-- help]
366
369
Try 'file_packager --help' for more details.''' )
367
370
return 1
368
371
@@ -391,6 +394,9 @@ def main(): # noqa: C901, PLR0912, PLR0915
391
394
elif arg == '--no-force' :
392
395
options .force = False
393
396
leading = ''
397
+ elif arg == '--export-es6' :
398
+ options .export_es6 = True
399
+ leading = ''
394
400
elif arg == '--use-preload-cache' :
395
401
options .use_preload_cache = True
396
402
leading = ''
@@ -485,6 +491,11 @@ def main(): # noqa: C901, PLR0912, PLR0915
485
491
diagnostics .error ('TARGET should not be the same value of --js-output' )
486
492
return 1
487
493
494
+ if options .from_emcc and options .export_es6 :
495
+ diagnostics .error ('Can\' t use --export-es6 option together with --from-emcc since the code should be embedded '
496
+ 'within emcc\' s code' )
497
+ return 1
498
+
488
499
walked .append (__file__ )
489
500
for file_ in data_files :
490
501
if not should_ignore (file_ .srcpath ):
@@ -621,20 +632,38 @@ def generate_js(data_target, data_files, metadata):
621
632
if options .from_emcc :
622
633
ret = ''
623
634
else :
624
- ret = '''
635
+ if options .export_es6 :
636
+ ret = 'export default async function loadDataFile(Module) {\n '
637
+ else :
638
+ ret = '''
625
639
var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n ''' % {"EXPORT_NAME" : options .export_name }
626
640
627
641
ret += '''
628
642
Module['expectedDataFileDownloads'] ??= 0;
629
- Module['expectedDataFileDownloads']++;
630
- (() => {
643
+ Module['expectedDataFileDownloads']++;'''
644
+
645
+ if not options .export_es6 :
646
+ ret += '''
647
+ (() => {'''
648
+
649
+ ret += '''
631
650
// Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
632
651
var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
633
652
var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
634
653
if (isPthread || isWasmWorker) return;\n '''
635
654
636
655
if options .support_node :
637
656
ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n "
657
+
658
+ if options .support_node and options .export_es6 :
659
+ ret += '''if (isNode) {
660
+ const { createRequire } = await import('module');
661
+ /** @suppress{duplicate} */
662
+ var require = createRequire(import.meta.url);
663
+ }\n '''
664
+
665
+ if options .export_es6 :
666
+ ret += 'return new Promise((loadDataResolve, loadDataReject) => {\n '
638
667
ret += ' async function loadPackage(metadata) {\n '
639
668
640
669
code = '''
@@ -684,6 +713,8 @@ def generate_js(data_target, data_files, metadata):
684
713
create_data = '''// canOwn this data in the filesystem, it is a slide into the heap that will never change
685
714
Module['FS_createDataFile'](this.name, null, byteArray, true, true, true);
686
715
Module['removeRunDependency'](`fp ${that.name}`);'''
716
+ ready_promise = '''
717
+ loadDataResolve();'''
687
718
688
719
if not options .lz4 :
689
720
# Data requests - for getting a block of data out of the big archive - have
@@ -710,14 +741,14 @@ def generate_js(data_target, data_files, metadata):
710
741
finish: function(byteArray) {
711
742
var that = this;
712
743
%s
713
- this.requests[this.name] = null;
744
+ this.requests[this.name] = null;%s
714
745
}
715
746
};
716
747
717
748
var files = metadata['files'];
718
749
for (var i = 0; i < files.length; ++i) {
719
750
new DataRequest(files[i]['start'], files[i]['end'], files[i]['audio'] || 0).open('GET', files[i]['filename']);
720
- }\n ''' % (create_preloaded if options .use_preload_plugins else create_data )
751
+ }\n ''' % (create_preloaded if options .use_preload_plugins else create_data , ready_promise if options . export_es6 else '' )
721
752
722
753
if options .has_embedded and not options .obj_output :
723
754
diagnostics .warn ('--obj-output is recommended when using --embed. This outputs an object file for linking directly into your application is more efficient than JS encoding' )
@@ -958,6 +989,9 @@ def generate_js(data_target, data_files, metadata):
958
989
return contents.buffer;
959
990
}''' .strip ()
960
991
992
+ reject_promise = '''
993
+ loadDataReject();'''
994
+
961
995
ret += '''
962
996
async function fetchRemotePackage(packageName, packageSize) {
963
997
%(node_support_code)s
@@ -1128,7 +1162,14 @@ def generate_js(data_target, data_files, metadata):
1128
1162
}
1129
1163
loadPackage(%s);\n ''' % json .dumps (metadata )
1130
1164
1131
- ret += '''
1165
+ if options .export_es6 :
1166
+ ret += '''
1167
+ });
1168
+ }
1169
+ // END the loadDataFile function
1170
+ '''
1171
+ else :
1172
+ ret += '''
1132
1173
})();\n '''
1133
1174
1134
1175
return ret
0 commit comments