Skip to content

Commit 2b60379

Browse files
committed
module: add node-esm
1 parent 9848352 commit 2b60379

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ EXEEXT := $(shell $(PYTHON) -c \
7777
"import sys; print('.exe' if sys.platform == 'win32' else '')")
7878

7979
NODE_EXE = node$(EXEEXT)
80+
NODE_ESM_EXE = node-esm${EXEEXT}
8081
NODE ?= ./$(NODE_EXE)
8182
NODE_G_EXE = node_g$(EXEEXT)
8283
NPM ?= ./deps/npm/bin/npm-cli.js
@@ -132,6 +133,7 @@ $(NODE_EXE): build_type:=Release
132133
$(NODE_G_EXE): build_type:=Debug
133134
$(NODE_EXE) $(NODE_G_EXE): config.gypi out/Makefile
134135
$(MAKE) -C out BUILDTYPE=${build_type} V=$(V)
136+
ln -fs $(NODE_EXE) out/${build_type}/$(NODE_ESM_EXE)
135137
if [ ! -r $@ ] || [ ! -L $@ ]; then \
136138
ln -fs out/${build_type}/$(NODE_EXE) $@; fi
137139
else
@@ -147,10 +149,12 @@ else
147149
endif
148150
$(NODE_EXE): config.gypi out/Release/build.ninja
149151
$(NINJA) -C out/Release $(NINJA_ARGS)
152+
ln -fs $(NODE_EXE) out/Release/$(NODE_ESM_EXE)
150153
if [ ! -r $@ ] || [ ! -L $@ ]; then ln -fs out/Release/$(NODE_EXE) $@; fi
151154

152155
$(NODE_G_EXE): config.gypi out/Debug/build.ninja
153156
$(NINJA) -C out/Debug $(NINJA_ARGS)
157+
ln -fs $(NODE_EXE) out/Debug/$(NODE_ESM_EXE)
154158
if [ ! -r $@ ] || [ ! -L $@ ]; then ln -fs out/Debug/$(NODE_EXE) $@; fi
155159
else
156160
$(NODE_EXE) $(NODE_G_EXE):

lib/internal/modules/esm/get_format.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
extensionFormatMap,
1414
mimeToFormat,
1515
} = require('internal/modules/esm/formats');
16+
const { isNodeESM } = require('internal/modules/helpers');
1617

1718
const experimentalNetworkImports =
1819
getOptionValue('--experimental-network-imports');
@@ -73,8 +74,13 @@ function extname(url) {
7374
* @returns {string}
7475
*/
7576
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
77+
const defaultESM = isNodeESM();
78+
7679
const ext = extname(url);
7780
if (ext === '.js') {
81+
if (defaultESM) {
82+
return getPackageType(url) === 'commonjs' ? 'commonjs' : 'module';
83+
}
7884
return getPackageType(url) === 'module' ? 'module' : 'commonjs';
7985
}
8086

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

8490
// Explicit undefined return indicates load hook should rerun format check
8591
if (ignoreErrors) { return undefined; }
92+
93+
if (defaultESM) {
94+
return 'module';
95+
}
96+
8697
const filepath = fileURLToPath(url);
8798
let suggestion = '';
8899
if (getPackageType(url) === 'module' && ext === '') {

lib/internal/modules/helpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,17 @@ function hasEsmSyntax(code) {
256256
stmt.type === 'ExportAllDeclaration');
257257
}
258258

259+
function isNodeESM() {
260+
const execname = path.basename(process.argv0);
261+
return execname === 'node-esm' || execname === 'node-esm.exe';
262+
}
263+
259264
module.exports = {
260265
addBuiltinLibsToObject,
261266
getCjsConditions,
262267
initializeCjsConditions,
263268
hasEsmSyntax,
269+
isNodeESM,
264270
loadBuiltinModule,
265271
makeRequireFunction,
266272
normalizeReferrerURL,

lib/internal/modules/run_main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
} = primordials;
66

77
const { getOptionValue } = require('internal/options');
8+
const { isNodeESM } = require('internal/modules/helpers');
89
const path = require('path');
910

1011
function resolveMainPath(main) {
@@ -42,8 +43,13 @@ function shouldUseESMLoader(mainPath) {
4243
return true;
4344
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs'))
4445
return false;
46+
4547
const pkg = readPackageScope(mainPath);
46-
return pkg && pkg.data.type === 'module';
48+
49+
if (isNodeESM())
50+
return pkg.data?.type !== 'commonjs';
51+
52+
return pkg.data?.type === 'module';
4753
}
4854

4955
function runMainESM(mainPath) {

0 commit comments

Comments
 (0)