Skip to content

Commit

Permalink
module: support Wasm without file extension within module scope
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Sep 7, 2023
1 parent 500ea04 commit d78a9da
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/internal/modules/esm/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

const {
RegExpPrototypeExec,
Uint8Array,
} = primordials;
const { getOptionValue } = require('internal/options');

const { closeSync, openSync, readSync } = require('fs');

const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');

const extensionFormatMap = {
Expand Down Expand Up @@ -35,7 +38,27 @@ function mimeToFormat(mime) {
return null;
}

function guessExtensionlessModule(url) {
if (!experimentalWasmModules)
return 'module';

const magic = new Uint8Array(4);
let fd;
try {
fd = openSync(url);
readSync(fd, magic);
if (magic[0] === 0x00 && magic[1] === 0x61 && magic[2] === 0x73 && magic[3] === 0x6d) {
return 'wasm';
}
} finally {
if (fd) closeSync(fd);
}

return 'module';
}

module.exports = {
extensionFormatMap,
guessExtensionlessModule,
mimeToFormat,
};
10 changes: 9 additions & 1 deletion lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
const { getOptionValue } = require('internal/options');
const {
extensionFormatMap,
guessExtensionlessModule,
mimeToFormat,
} = require('internal/modules/esm/formats');

Expand Down Expand Up @@ -73,10 +74,17 @@ function extname(url) {
*/
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
const ext = extname(url);
if (ext === '.js' || ext === '') {
if (ext === '.js') {
return getPackageType(url) === 'module' ? 'module' : 'commonjs';
}

if (ext === '') {
if (getPackageType(url) === 'module') {
return guessExtensionlessModule(url);
}
return 'commonjs';
}

const format = extensionFormatMap[ext];
if (format) return format;

Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions test/fixtures/es-modules/package-type-module/wasm-dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { strictEqual } from 'assert';

export function jsFn () {
state = 'WASM JS Function Executed';
return 42;
}

export let state = 'JS Function Executed';

export function jsInitFn () {
strictEqual(state, 'JS Function Executed');
state = 'WASM Start Executed';
}
25 changes: 25 additions & 0 deletions test/parallel/test-esm-wasm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --experimental-wasm-modules
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { strictEqual } from 'assert';
import { spawn } from 'child_process';

{
const entry = path('/es-modules/package-type-module/noext-wasm');

// Run a module that does not have extension.
// This is to ensure that "type": "module" applies to extensionless files.

const child = spawn(process.execPath, ['--experimental-wasm-modules', entry]);

let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
stdout += data;
});
child.on('close', mustCall((code, signal) => {
strictEqual(code, 0);
strictEqual(signal, null);
strictEqual(stdout, '');
}));
}

0 comments on commit d78a9da

Please sign in to comment.