emscripten-core / emscripten-core/emscripten

RFE: ability to elide the automatic "export default ..." for ES6 module builds

Open
#18,237 3 comments 0 reactions 0 assignees View on GitHub
JavaScript
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When building an ES6 module, Emscripten automatically adds:

```js
export default myModuleInitFunction;
```

to the resulting JS file. "The problem" with that is that we (the sqlite project) have to, due in part to #18071, overwrite the module init function object with a modified one. We do this via `--extern-post-js` and then want to do:

```js
export default ourModifiedInitFunction;
```

but it's illegal to do "export default" twice in the same module.

Our current workaround is this bit of hackery in our makefile after compiling `sqlite3.mjs`:

```
@if [ esm = $(1) ]; then \
echo "Fragile workaround for an Emscripten annoyance. See emcc.flags.sqlite3.esm."; \
sed -i -e '0,/^export default/{/^export default/d}' $@ || exit $$?; \
if ! grep -q '^export default' $@; then \
echo "Cannot find export default." 1>&2; \
exit 1; \
fi; \
fi
```

i.e. we delete the first "export default" line, leaving ours intact. That's horribly fragile.

Sidebar: the auto-injected "export default" line does not include a newline after it, which causes `--extern-post-js` to be appended starting on that same line, which requires a separate kludge on our side to account for.

What would be really useful is a way to tell emcc to _not_ add the "export default" line automatically. When building an `.mjs` file it automatically does so even if `-sEXPORT_ES6=0` is used. An alternative implementation would be to _not_ add that line when `-sEXPORT_ES6=0`, but whether or not that would have other side effects is a question i cannot answer.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.