Skip to content

Commit 95790ca

Browse files
authored
Allow .mjs for js module output (#7876)
* Allow .mjs for js module output Asking for a file of this extension enables EXPORT_ES6 and MODULARIZE. Fixes #7853
1 parent c623c9c commit 95790ca

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

emcc.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979

8080
LIB_PREFIXES = ('', 'lib')
8181

82-
JS_CONTAINING_SUFFIXES = ('js', 'html')
82+
JS_CONTAINING_SUFFIXES = ('js', 'mjs', 'html')
8383
EXECUTABLE_SUFFIXES = JS_CONTAINING_SUFFIXES + ('wasm',)
8484

8585
DEFERRED_RESPONSE_FILES = ('EMTERPRETIFY_BLACKLIST', 'EMTERPRETIFY_WHITELIST', 'EMTERPRETIFY_SYNCLIST')
@@ -917,7 +917,12 @@ def detect_fixed_language_mode(args):
917917
final_ending = ('.' + final_suffix) if len(final_suffix) else ''
918918

919919
# target is now finalized, can finalize other _target s
920-
js_target = unsuffixed(target) + '.js'
920+
if final_suffix == 'mjs':
921+
shared.Settings.EXPORT_ES6 = 1
922+
shared.Settings.MODULARIZE = 1
923+
js_target = target
924+
else:
925+
js_target = unsuffixed(target) + '.js'
921926

922927
asm_target = unsuffixed(js_target) + '.asm.js' # might not be used, but if it is, this is the name
923928
wasm_text_target = asm_target.replace('.asm.js', '.wast') # ditto, might not be used
@@ -1268,7 +1273,11 @@ def check(input_file):
12681273
if shared.Settings.USE_PTHREADS and shared.Settings.WASM and shared.Settings.ALLOW_MEMORY_GROWTH and shared.Settings.WASM_MEM_MAX == -1:
12691274
exit_with_error('If pthreads and memory growth are enabled, WASM_MEM_MAX must be set')
12701275

1271-
# When MODULARIZE option is used, currently declare all module exports individually - TODO: this could be optimized
1276+
if shared.Settings.EXPORT_ES6 and not shared.Settings.MODULARIZE:
1277+
exit_with_error('EXPORT_ES6 requires MODULARIZE to be set')
1278+
1279+
# When MODULARIZE option is used, currently declare all module exports
1280+
# individually - TODO: this could be optimized
12721281
if shared.Settings.MODULARIZE and not shared.Settings.DECLARE_ASM_MODULE_EXPORTS:
12731282
shared.Settings.DECLARE_ASM_MODULE_EXPORTS = 1
12741283
logger.warning('Enabling -s DECLARE_ASM_MODULE_EXPORTS=1, since MODULARIZE currently requires declaring asm.js/wasm module exports in full')

site/build/text/docs/tools_reference/emcc.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +573,24 @@ Options that are modified or new in *emcc* are listed below:
573573
The "target" file name extension defines the output type to be
574574
generated:
575575

576-
* <name> **.js** : JavaScript.
576+
* <name> **.js** : JavaScript (+ separate **<name>.wasm** file
577+
if emitting WebAssembly). (default)
578+
579+
* <name> **.mjs** : ES6 JavaScript module (+ separate
580+
**<name>.wasm** file if emitting WebAssembly).
577581

578582
* <name> **.html** : HTML + separate JavaScript file
579-
(**<name>.js**). Having the separate JavaScript file improves
580-
page load time.
583+
(**<name>.js**; + separate **<name>.wasm** file if emitting
584+
WebAssembly).
585+
586+
* <name> **.bc** : LLVM bitcode.
581587

582-
* <name> **.bc** : LLVM bitcode (default).
588+
* <name> **.o** : LLVM bitcode (same as .bc), unless in
589+
*WASM_OBJECT_FILES* mode, in which case it will contain a
590+
WebAssembly object.
583591

584-
* <name> **.o** : LLVM bitcode (same as .bc).
592+
* <name> **.wasm** : WebAssembly without JavaScript support
593+
code ("standalone wasm").
585594

586595
Note: If "--memory-init-file" is used, a **.mem** file will be
587596
created in addition to the generated **.js** and/or **.html**

site/source/docs/tools_reference/emcc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ Options that are modified or new in *emcc* are listed below:
446446
The ``target`` file name extension defines the output type to be generated:
447447

448448
- <name> **.js** : JavaScript (+ separate **<name>.wasm** file if emitting WebAssembly). (default)
449+
- <name> **.mjs** : ES6 JavaScript module (+ separate **<name>.wasm** file if emitting WebAssembly).
449450
- <name> **.html** : HTML + separate JavaScript file (**<name>.js**; + separate **<name>.wasm** file if emitting WebAssembly).
450451
- <name> **.bc** : LLVM bitcode.
451452
- <name> **.o** : LLVM bitcode (same as .bc), unless in `WASM_OBJECT_FILES` mode, in which case it will contain a WebAssembly object.

tests/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ def test_emcc_generate_config(self):
148148
self.assertContained('LLVM_ROOT', config_contents)
149149
os.remove(config_path)
150150

151+
def test_emcc_output_mjs(self):
152+
run_process([PYTHON, EMCC, '-o', 'hello_world.mjs', path_from_root('tests', 'hello_world.c')])
153+
with open('hello_world.mjs') as f:
154+
output = f.read()
155+
self.assertContained('export default Module;', output)
156+
# TODO(sbc): Test that this is actually runnable. We currently don't have
157+
# any tests for EXPORT_ES6 but once we do this should be enabled.
158+
# self.assertContained('hello, world!', run_js('hello_world.mjs'))
159+
151160
def test_emcc_1(self):
152161
for compiler, suffix in [(EMCC, '.c'), (EMXX, '.cpp')]:
153162
# --version

0 commit comments

Comments
 (0)