Skip to content

Commit 2c6d864

Browse files
authored
Extract function for creating worker JS files. NFC (#19579)
Use the the same method for all 3 types of worker
1 parent c431770 commit 2c6d864

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

emcc.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,6 +3191,18 @@ def phase_memory_initializer(memfile):
31913191
final_js += '.mem.js'
31923192

31933193

3194+
def create_worker_file(input_file, target_dir, output_file):
3195+
output_file = os.path.join(target_dir, output_file)
3196+
input_file = utils.path_from_root(input_file)
3197+
contents = shared.read_and_preprocess(input_file, expand_macros=True)
3198+
write_file(output_file, contents)
3199+
3200+
# Minify the worker JS files file in optimized builds
3201+
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
3202+
contents = building.acorn_optimizer(output_file, ['minifyWhitespace'], return_output=True)
3203+
write_file(output_file, contents)
3204+
3205+
31943206
@ToolchainProfiler.profile_block('final emitting')
31953207
def phase_final_emitting(options, state, target, wasm_target, memfile):
31963208
global final_js
@@ -3203,36 +3215,15 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
32033215

32043216
target_dir = os.path.dirname(os.path.abspath(target))
32053217
if settings.PTHREADS:
3206-
worker_output = os.path.join(target_dir, settings.PTHREAD_WORKER_FILE)
3207-
contents = shared.read_and_preprocess(utils.path_from_root('src/worker.js'), expand_macros=True)
3208-
write_file(worker_output, contents)
3209-
3210-
# Minify the worker.js file in optimized builds
3211-
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
3212-
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
3213-
write_file(worker_output, minified_worker)
3218+
create_worker_file('src/worker.js', target_dir, settings.PTHREAD_WORKER_FILE)
32143219

32153220
# Deploy the Wasm Worker bootstrap file as an output file (*.ww.js)
32163221
if settings.WASM_WORKERS == 1:
3217-
worker_output = os.path.join(target_dir, settings.WASM_WORKER_FILE)
3218-
contents = shared.read_and_preprocess(shared.path_from_root('src/wasm_worker.js'), expand_macros=True)
3219-
write_file(worker_output, contents)
3220-
3221-
# Minify the wasm_worker.js file in optimized builds
3222-
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
3223-
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
3224-
write_file(worker_output, minified_worker)
3222+
create_worker_file('src/wasm_worker.js', target_dir, settings.WASM_WORKER_FILE)
32253223

32263224
# Deploy the Audio Worklet module bootstrap file (*.aw.js)
32273225
if settings.AUDIO_WORKLET == 1:
3228-
worklet_output = os.path.join(target_dir, settings.AUDIO_WORKLET_FILE)
3229-
contents = shared.read_and_preprocess(shared.path_from_root('src', 'audio_worklet.js'), expand_macros=True)
3230-
utils.write_file(worklet_output, contents)
3231-
3232-
# Minify the audio_worklet.js file in optimized builds
3233-
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
3234-
minified_worker = building.acorn_optimizer(worklet_output, ['minifyWhitespace'], return_output=True)
3235-
utils.write_file(worklet_output, minified_worker)
3226+
create_worker_file('src/audio_worklet.js', target_dir, settings.AUDIO_WORKLET_FILE)
32363227

32373228
if settings.MODULARIZE:
32383229
modularize()

0 commit comments

Comments
 (0)