Skip to content

Commit 2514cb5

Browse files
authored
Fix default conditions for moduleResolve
Closes wooormGH-32. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent fa79719 commit 2514cb5

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

lib/resolve.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
10811081
export function moduleResolve(specifier, base, conditions, preserveSymlinks) {
10821082
// Note: The Node code supports `base` as a string (in this internal API) too,
10831083
// we don’t.
1084+
if (conditions === undefined) {
1085+
conditions = getConditionsSet()
1086+
}
10841087
const protocol = base.protocol
10851088
const isData = protocol === 'data:'
10861089
const isRemote = isData || protocol === 'http:' || protocol === 'https:'

test/core.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import process from 'node:process'
88
import {URL, pathToFileURL} from 'node:url'
99
import test from 'node:test'
1010
import semver from 'semver'
11-
import {resolve} from '../index.js'
11+
import {moduleResolve, resolve} from '../index.js'
1212

1313
const windows = process.platform === 'win32'
1414
const nodeBefore18 = semver.lt(process.versions.node, '18.0.0')
@@ -258,9 +258,13 @@ test(
258258
assert.fail()
259259
} catch (error) {
260260
const exception = /** @type {ErrnoException} */ (error)
261-
assert.equal(
262-
exception.code,
263-
'ERR_NETWORK_IMPORT_DISALLOWED',
261+
assert(
262+
[
263+
// Node 22+
264+
'ERR_ASSERTION',
265+
// Node 20-
266+
'ERR_NETWORK_IMPORT_DISALLOWED'
267+
].includes(/** @type {string} */ (exception.code)),
264268
'should not support loading builtins from http'
265269
)
266270
}
@@ -690,3 +694,12 @@ test(
690694
)
691695
}
692696
)
697+
698+
test('moduleResolve(specifier, parent, conditions?, preserveSymlinks?)', function () {
699+
assert.equal(
700+
moduleResolve('package-export-conditions', new URL(import.meta.url)).href,
701+
new URL('node_modules/package-export-conditions/esm.mjs', import.meta.url)
702+
.href,
703+
'should resolve to the ESM entry point'
704+
)
705+
})

test/node_modules/package-export-conditions/cjs.js

Whitespace-only changes.

test/node_modules/package-export-conditions/esm.mjs

Whitespace-only changes.

test/node_modules/package-export-conditions/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)