@@ -2413,16 +2413,16 @@ def generate_html(target, options, js_target, target_basename,
2413
2413
asm_mods = []
2414
2414
2415
2415
if options .proxy_to_worker :
2416
- proxy_worker_filename = shared .Settings .PROXY_TO_WORKER_FILENAME or target_basename
2416
+ proxy_worker_filename = ( shared .Settings .PROXY_TO_WORKER_FILENAME or target_basename ) + '.js'
2417
2417
worker_js = worker_js_script (proxy_worker_filename )
2418
- script .inline = '''
2418
+ script .inline = ('''
2419
+ var filename = '%s';
2419
2420
if ((',' + window.location.search.substr(1) + ',').indexOf(',noProxy,') < 0) {
2420
2421
console.log('running code in a web worker');
2421
- ''' + worker_js + '''
2422
+ ''' % shared . JS . get_subresource_location ( proxy_worker_filename )) + worker_js + '''
2422
2423
} else {
2423
2424
// note: no support for code mods (PRECISE_F32==2)
2424
2425
console.log('running code on the main thread');
2425
- var filename = '%s';
2426
2426
var fileBytes = tryParseAsDataURI(filename);
2427
2427
var script = document.createElement('script');
2428
2428
if (fileBytes) {
@@ -2432,7 +2432,7 @@ def generate_html(target, options, js_target, target_basename,
2432
2432
}
2433
2433
document.body.appendChild(script);
2434
2434
}
2435
- ''' % shared . JS . get_subresource_location ( proxy_worker_filename + '.js' )
2435
+ '''
2436
2436
else :
2437
2437
# Normal code generation path
2438
2438
script .src = base_js_target
@@ -2442,10 +2442,11 @@ def generate_html(target, options, js_target, target_basename,
2442
2442
minified = 'minifyNames' in optimizer .queue_history ,
2443
2443
separate_asm = options .separate_asm )
2444
2444
2445
- if shared .Settings .EMTERPRETIFY_FILE :
2446
- # We need to load the emterpreter file before anything else, it has to be synchronously ready
2447
- script .un_src ()
2448
- script .inline = '''
2445
+ if not shared .Settings .SINGLE_FILE :
2446
+ if shared .Settings .EMTERPRETIFY_FILE :
2447
+ # We need to load the emterpreter file before anything else, it has to be synchronously ready
2448
+ script .un_src ()
2449
+ script .inline = '''
2449
2450
var emterpretURL = '%s';
2450
2451
var emterpretXHR = new XMLHttpRequest();
2451
2452
emterpretXHR.open('GET', emterpretURL, true);
@@ -2464,10 +2465,10 @@ def generate_html(target, options, js_target, target_basename,
2464
2465
emterpretXHR.send(null);
2465
2466
''' % (shared .JS .get_subresource_location (shared .Settings .EMTERPRETIFY_FILE ), script .inline )
2466
2467
2467
- if options .memory_init_file :
2468
- # start to load the memory init file in the HTML, in parallel with the JS
2469
- script .un_src ()
2470
- script .inline = ('''
2468
+ if options .memory_init_file :
2469
+ # start to load the memory init file in the HTML, in parallel with the JS
2470
+ script .un_src ()
2471
+ script .inline = ('''
2471
2472
var memoryInitializer = '%s';
2472
2473
if (typeof Module['locateFile'] === 'function') {
2473
2474
memoryInitializer = Module['locateFile'](memoryInitializer);
@@ -2481,15 +2482,15 @@ def generate_html(target, options, js_target, target_basename,
2481
2482
meminitXHR.send(null);
2482
2483
''' % shared .JS .get_subresource_location (memfile )) + script .inline
2483
2484
2484
- # Download .asm.js if --separate-asm was passed in an asm.js build, or if 'asmjs' is one
2485
- # of the wasm run methods.
2486
- if not options .separate_asm or (shared .Settings .BINARYEN and 'asmjs' not in shared .Settings .BINARYEN_METHOD ):
2487
- assert len (asm_mods ) == 0 , 'no --separate-asm means no client code mods are possible'
2488
- else :
2489
- script .un_src ()
2490
- if len (asm_mods ) == 0 :
2491
- # just load the asm, then load the rest
2492
- script .inline = '''
2485
+ # Download .asm.js if --separate-asm was passed in an asm.js build, or if 'asmjs' is one
2486
+ # of the wasm run methods.
2487
+ if not options .separate_asm or (shared .Settings .BINARYEN and 'asmjs' not in shared .Settings .BINARYEN_METHOD ):
2488
+ assert len (asm_mods ) == 0 , 'no --separate-asm means no client code mods are possible'
2489
+ else :
2490
+ script .un_src ()
2491
+ if len (asm_mods ) == 0 :
2492
+ # just load the asm, then load the rest
2493
+ script .inline = '''
2493
2494
var filename = '%s';
2494
2495
var fileBytes = tryParseAsDataURI(filename);
2495
2496
var script = document.createElement('script');
@@ -2505,9 +2506,9 @@ def generate_html(target, options, js_target, target_basename,
2505
2506
};
2506
2507
document.body.appendChild(script);
2507
2508
''' % (shared .JS .get_subresource_location (asm_target ), script .inline )
2508
- else :
2509
- # may need to modify the asm code, load it as text, modify, and load asynchronously
2510
- script .inline = '''
2509
+ else :
2510
+ # may need to modify the asm code, load it as text, modify, and load asynchronously
2511
+ script .inline = '''
2511
2512
var codeURL = '%s';
2512
2513
var codeXHR = new XMLHttpRequest();
2513
2514
codeXHR.open('GET', codeURL, true);
@@ -2538,10 +2539,10 @@ def generate_html(target, options, js_target, target_basename,
2538
2539
codeXHR.send(null);
2539
2540
''' % (shared .JS .get_subresource_location (asm_target ), '\n ' .join (asm_mods ), script .inline )
2540
2541
2541
- if shared .Settings .BINARYEN and not shared .Settings .BINARYEN_ASYNC_COMPILATION :
2542
- # We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize
2543
- script .un_src ()
2544
- script .inline = '''
2542
+ if shared .Settings .BINARYEN and not shared .Settings .BINARYEN_ASYNC_COMPILATION :
2543
+ # We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize
2544
+ script .un_src ()
2545
+ script .inline = '''
2545
2546
var wasmURL = '%s';
2546
2547
var wasmXHR = new XMLHttpRequest();
2547
2548
wasmXHR.open('GET', wasmURL, true);
@@ -2567,6 +2568,17 @@ def generate_html(target, options, js_target, target_basename,
2567
2568
script .inline = f .read () + script .inline
2568
2569
f .close ()
2569
2570
2571
+ # inline script for SINGLE_FILE output
2572
+ if shared .Settings .SINGLE_FILE :
2573
+ js_contents = script .inline or ''
2574
+ if script .src :
2575
+ js = open (js_target , 'r' )
2576
+ js_contents += js .read ()
2577
+ js .close ()
2578
+ shared .try_delete (js_target )
2579
+ script .src = None
2580
+ script .inline = js_contents
2581
+
2570
2582
html = open (target , 'wb' )
2571
2583
html_contents = shell .replace ('{{{ SCRIPT }}}' , script .replacement ())
2572
2584
html_contents = tools .line_endings .convert_line_endings (html_contents , '\n ' , options .output_eol )
@@ -2575,9 +2587,16 @@ def generate_html(target, options, js_target, target_basename,
2575
2587
2576
2588
2577
2589
def generate_worker_js (target , js_target , target_basename ):
2578
- shutil .move (js_target , unsuffixed (js_target ) + '.worker.js' ) # compiler output goes in .worker.js file
2579
- worker_target_basename = target_basename + '.worker'
2580
- proxy_worker_filename = shared .Settings .PROXY_TO_WORKER_FILENAME or worker_target_basename
2590
+ # compiler output is embedded as base64
2591
+ if shared .Settings .SINGLE_FILE :
2592
+ proxy_worker_filename = shared .JS .get_subresource_location (js_target )
2593
+
2594
+ # compiler output goes in .worker.js file
2595
+ else :
2596
+ shutil .move (js_target , unsuffixed (js_target ) + '.worker.js' )
2597
+ worker_target_basename = target_basename + '.worker'
2598
+ proxy_worker_filename = (shared .Settings .PROXY_TO_WORKER_FILENAME or worker_target_basename ) + '.js'
2599
+
2581
2600
target_contents = worker_js_script (proxy_worker_filename )
2582
2601
open (target , 'w' ).write (target_contents )
2583
2602
0 commit comments