Skip to content

Simplify MODULARIZE code to always use async factory function #24727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions site/source/docs/tools_reference/settings_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1863,14 +1863,9 @@ MODULARIZE
By default we emit all code in a straightforward way into the output
.js file. That means that if you load that in a script tag in a web
page, it will use the global scope. With ``MODULARIZE`` set, we instead emit
the code wrapped in a function that returns a promise. The promise is
resolved with the module instance when it is safe to run the compiled code,
similar to the ``onRuntimeInitialized`` callback. You do not need to use the
``onRuntimeInitialized`` callback when using ``MODULARIZE``.

(If WASM_ASYNC_COMPILATION is off, that is, if compilation is
*synchronous*, then it would not make sense to return a Promise, and instead
the Module object itself is returned, which is ready to be used.)
the code wrapped in an async function. This functions returns a promise that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the code wrapped in an async function. This functions returns a promise that
the code wrapped in an async function. This function returns a promise that

resolves to a module instance when once it is safe to run the compiled code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolves to a module instance when once it is safe to run the compiled code
resolves to a module instance when it is safe to run the compiled code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could also be "once", but not "when once" together, I think)

(similar to the ``onRuntimeInitialized`` callback).

The default name of the function is ``Module``, but can be changed using the
``EXPORT_NAME`` option. We recommend renaming it to a more typical name for a
Expand Down
4 changes: 2 additions & 2 deletions src/modularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var {{{ EXPORT_NAME }}} = (() => {
// after document.currentScript is gone, so we save it.
// In EXPORT_ES6 mode we can just use 'import.meta.url'.
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
return {{{ asyncIf(WASM_ASYNC_COMPILATION || (EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE)) }}}function(moduleArg = {}) {
return async function(moduleArg = {}) {
var moduleRtn;

"<<< INNER_JS_CODE >>>"
Expand All @@ -30,7 +30,7 @@ var {{{ EXPORT_NAME }}} = (() => {
#else
// When targetting node and ES6 we use `await import ..` in the generated code
// so the outer function needs to be marked as async.
{{{ asyncIf(WASM_ASYNC_COMPILATION || (EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE)) }}}function {{{ EXPORT_NAME }}}(moduleArg = {}) {
async function {{{ EXPORT_NAME }}}(moduleArg = {}) {
var moduleRtn;

"<<< INNER_JS_CODE >>>"
Expand Down
11 changes: 3 additions & 8 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,14 +1257,9 @@ var DETERMINISTIC = false;
// By default we emit all code in a straightforward way into the output
// .js file. That means that if you load that in a script tag in a web
// page, it will use the global scope. With ``MODULARIZE`` set, we instead emit
// the code wrapped in a function that returns a promise. The promise is
// resolved with the module instance when it is safe to run the compiled code,
// similar to the ``onRuntimeInitialized`` callback. You do not need to use the
// ``onRuntimeInitialized`` callback when using ``MODULARIZE``.
//
// (If WASM_ASYNC_COMPILATION is off, that is, if compilation is
// *synchronous*, then it would not make sense to return a Promise, and instead
// the Module object itself is returned, which is ready to be used.)
// the code wrapped in an async function. This functions returns a promise that
// resolves to a module instance when once it is safe to run the compiled code
// (similar to the ``onRuntimeInitialized`` callback).
//
// The default name of the function is ``Module``, but can be changed using the
// ``EXPORT_NAME`` option. We recommend renaming it to a more typical name for a
Expand Down