Skip to content
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

build,module: add node-esm support #49407

Open
wants to merge 3 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -77,6 +77,7 @@ EXEEXT := $(shell $(PYTHON) -c \
"import sys; print('.exe' if sys.platform == 'win32' else '')")

NODE_EXE = node$(EXEEXT)
NODE_ESM_EXE = node-esm${EXEEXT}
NODE ?= ./$(NODE_EXE)
NODE_G_EXE = node_g$(EXEEXT)
NPM ?= ./deps/npm/bin/npm-cli.js
Expand Down Expand Up @@ -132,6 +133,7 @@ $(NODE_EXE): build_type:=Release
$(NODE_G_EXE): build_type:=Debug
$(NODE_EXE) $(NODE_G_EXE): config.gypi out/Makefile
$(MAKE) -C out BUILDTYPE=${build_type} V=$(V)
ln -fs $(NODE_EXE) out/${build_type}/$(NODE_ESM_EXE)
if [ ! -r $@ ] || [ ! -L $@ ]; then \
ln -fs out/${build_type}/$(NODE_EXE) $@; fi
else
Expand All @@ -147,10 +149,12 @@ else
endif
$(NODE_EXE): config.gypi out/Release/build.ninja
$(NINJA) -C out/Release $(NINJA_ARGS)
ln -fs $(NODE_EXE) out/Release/$(NODE_ESM_EXE)
if [ ! -r $@ ] || [ ! -L $@ ]; then ln -fs out/Release/$(NODE_EXE) $@; fi

$(NODE_G_EXE): config.gypi out/Debug/build.ninja
$(NINJA) -C out/Debug $(NINJA_ARGS)
ln -fs $(NODE_EXE) out/Debug/$(NODE_ESM_EXE)
if [ ! -r $@ ] || [ ! -L $@ ]; then ln -fs out/Debug/$(NODE_EXE) $@; fi
else
$(NODE_EXE) $(NODE_G_EXE):
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/modules/esm/get_format.js
Expand Up @@ -13,6 +13,7 @@ const {
extensionFormatMap,
mimeToFormat,
} = require('internal/modules/esm/formats');
const { isNodeESM } = require('internal/modules/helpers');

const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
Expand Down Expand Up @@ -73,8 +74,13 @@ function extname(url) {
* @returns {string}
*/
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
const defaultESM = isNodeESM();

const ext = extname(url);
if (ext === '.js') {
if (defaultESM) {
return getPackageType(url) === 'commonjs' ? 'commonjs' : 'module';
}
return getPackageType(url) === 'module' ? 'module' : 'commonjs';
}

Expand All @@ -83,6 +89,11 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {

// Explicit undefined return indicates load hook should rerun format check
if (ignoreErrors) { return undefined; }

if (defaultESM) {
return 'module';
}

const filepath = fileURLToPath(url);
let suggestion = '';
if (getPackageType(url) === 'module' && ext === '') {
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/modules/helpers.js
Expand Up @@ -256,11 +256,17 @@ function hasEsmSyntax(code) {
stmt.type === 'ExportAllDeclaration');
}

function isNodeESM() {
const execname = path.basename(process.argv0);
return execname === 'node-esm' || execname === 'node-esm.exe';
}

module.exports = {
addBuiltinLibsToObject,
getCjsConditions,
initializeCjsConditions,
hasEsmSyntax,
isNodeESM,
loadBuiltinModule,
makeRequireFunction,
normalizeReferrerURL,
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/modules/run_main.js
Expand Up @@ -5,6 +5,7 @@ const {
} = primordials;

const { getOptionValue } = require('internal/options');
const { isNodeESM } = require('internal/modules/helpers');
const path = require('path');

function resolveMainPath(main) {
Expand Down Expand Up @@ -42,8 +43,13 @@ function shouldUseESMLoader(mainPath) {
return true;
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs'))
return false;

const pkg = readPackageScope(mainPath);
return pkg && pkg.data.type === 'module';

if (isNodeESM())
return pkg.data?.type !== 'commonjs';

return pkg.data?.type === 'module';
}

function runMainESM(mainPath) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/errors/force_colors.snapshot
Expand Up @@ -8,7 +8,7 @@ Error: Should include grayed stack trace
 at Module._extensions..js (node:internal*modules*cjs*loader:1295:10)
 at Module.load (node:internal*modules*cjs*loader:1091:32)
 at Module._load (node:internal*modules*cjs*loader:938:12)
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:83:12)
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:89:12)
 at node:internal*main*run_main_module:23:47

Node.js *
68 changes: 68 additions & 0 deletions test/parallel/test-node-esm.mjs
@@ -0,0 +1,68 @@
import '../common/index.mjs';
import { strictEqual } from 'node:assert';
import fs from 'node:fs/promises';
import { execFileSync } from 'node:child_process';
import { createRequire } from 'node:module';
const tmpdir = createRequire(import.meta.url)('../common/tmpdir.js');

tmpdir.refresh();

const code = 'process.stdout.write(this === undefined ? "module" : "commonjs");\n';

// Copying would be significantly slower
const copyMethod = async (src, dest) => fs.link(src, dest);

const pathJS = tmpdir.resolve('blep.js');
await fs.writeFile(pathJS, code);
const pathMJS = tmpdir.resolve('blep.mjs');
await fs.writeFile(pathMJS, code);
const pathCJS = tmpdir.resolve('blep.cjs');
await fs.writeFile(pathCJS, code);
const path = tmpdir.resolve('blep');
await fs.writeFile(path, code);
const pathIDK = tmpdir.resolve('blep.idontknowthistype');
await fs.writeFile(pathIDK, code);

{
const execPath = tmpdir.resolve('node');
await copyMethod(process.execPath, execPath);

strictEqual(execFileSync(execPath, [pathJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [pathMJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathCJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [path]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [pathIDK]).toString(), 'commonjs');
}

{
const execPath = tmpdir.resolve('node-esm');
await copyMethod(process.execPath, execPath);

strictEqual(execFileSync(execPath, [pathJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathMJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathCJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [path]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathIDK]).toString(), 'module');
}

{
const execPath = tmpdir.resolve('node.exe');
await copyMethod(process.execPath, execPath);

strictEqual(execFileSync(execPath, [pathJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [pathMJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathCJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [path]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [pathIDK]).toString(), 'commonjs');
}

{
const execPath = tmpdir.resolve('node-esm.exe');
await copyMethod(process.execPath, execPath);

strictEqual(execFileSync(execPath, [pathJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathMJS]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathCJS]).toString(), 'commonjs');
strictEqual(execFileSync(execPath, [path]).toString(), 'module');
strictEqual(execFileSync(execPath, [pathIDK]).toString(), 'module');
}