Skip to content

esm: top-level Wasm without package type #57610

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ If a file is found, its path will be passed to the

* The program was started with a command-line flag that forces the entry
point to be loaded with ECMAScript module loader, such as `--import`.
* The file has an `.mjs` extension.
* The file has an `.mjs` or `.wasm` (with `--experimental-wasm-modules`)
extension.
* The file does not have a `.cjs` extension, and the nearest parent
`package.json` file contains a top-level [`"type"`][] field with a value of
`"module"`.
Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function shouldUseESMLoader(mainPath) {

// Determine the module format of the entry point.
if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) { return true; }
if (mainPath && StringPrototypeEndsWith(mainPath, '.wasm')) { return true; }
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) { return false; }

if (getOptionValue('--experimental-strip-types')) {
Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-esm-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
match(stderr, /WebAssembly/);
});

it('should support top-level execution', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-wasm-modules',
fixtures.path('es-modules/top-level-wasm.wasm'),
]);

strictEqual(stderr, '');
strictEqual(stdout, '[Object: null prototype] { prop: \'hello world\' }\n');
strictEqual(code, 0);
});

it('should support static source phase imports', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
Expand Down
Binary file added test/fixtures/es-modules/top-level-wasm.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions test/fixtures/es-modules/wasm-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function call1 (func, thisObj, arg0) {
return func.call(thisObj, arg0);
}

export function call2 (func, thisObj, arg0, arg1) {
return func.call(thisObj, arg0, arg1);
}

export function call3 (func, thisObj, arg0, arg1, arg2) {
return func.call(thisObj, arg0, arg1, arg2);
}
3 changes: 3 additions & 0 deletions test/fixtures/es-modules/wasm-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const { get: getProperty, set: setProperty } = Reflect;
export const { create } = Object;
export const global = globalThis;
6 changes: 6 additions & 0 deletions test/fixtures/es-modules/wasm-string-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const console = 'console';
const hello_world = 'hello world';
const log = 'log';
const prop = 'prop';

export { console, hello_world as 'hello world', log, prop }
Loading