Skip to content

Commit bd68eaf

Browse files
hybristguybedford
andcommitted
fixup! module: implement "exports" proposal for CommonJS
Co-Authored-By: Guy Bedford <guybedford@gmail.com>
1 parent b7258f9 commit bd68eaf

6 files changed

Lines changed: 14 additions & 5 deletions

File tree

β€Žlib/internal/modules/cjs/loader.jsβ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,13 @@ function resolveExports(nmPath, request, absoluteRequest) {
348348

349349
const basePath = path.resolve(nmPath, name);
350350
const pkgExports = readExports(basePath);
351+
const baseURL = `${pathToFileURL(basePath)}/`;
351352

352353
if (pkgExports != null) {
353354
const mappingKey = `.${expansion}`;
354355
const mapping = pkgExports[mappingKey];
355356
if (typeof mapping === 'string') {
356-
return path.resolve(basePath, mapping);
357+
return fileURLToPath(new URL(mapping, baseURL));
357358
}
358359

359360
let dirMatch = '';
@@ -369,10 +370,10 @@ function resolveExports(nmPath, request, absoluteRequest) {
369370
if (dirMatch !== '') {
370371
const dirMapping = pkgExports[dirMatch];
371372
const remainder = StringPrototype.slice(mappingKey, dirMatch.length);
372-
const expectedPrefix = path.resolve(basePath, dirMapping);
373-
const resolved = path.resolve(expectedPrefix, remainder);
373+
const expectedPrefix = new URL(dirMapping, baseURL).href;
374+
const resolved = new URL(remainder, expectedPrefix).href;
374375
if (StringPrototype.startsWith(resolved, expectedPrefix)) {
375-
return resolved;
376+
return fileURLToPath(resolved);
376377
}
377378
}
378379
throw new ERR_PATH_NOT_EXPORTED(basePath, mappingKey);

β€Žtest/es-module/test-esm-exports.mjsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { mustCall } from '../common/index.mjs';
44
import { ok, strictEqual } from 'assert';
55

6-
import { asdf, asdf2 } from '../fixtures/pkgexports.mjs';
6+
import { asdf, asdf2, space } from '../fixtures/pkgexports.mjs';
77
import {
88
loadMissing,
99
loadFromNumber,
@@ -12,6 +12,7 @@ import {
1212

1313
strictEqual(asdf, 'asdf');
1414
strictEqual(asdf2, 'asdf');
15+
strictEqual(space, 'encoded path');
1516

1617
loadMissing().catch(mustCall((err) => {
1718
ok(err.message.toString().startsWith('Package exports'));

β€Žtest/fixtures/node_modules/pkgexports/package.jsonβ€Ž

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

β€Žtest/fixtures/node_modules/pkgexports/sp ce.jsβ€Ž

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

β€Žtest/fixtures/pkgexports.mjsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as asdf } from 'pkgexports/asdf';
22
export { default as asdf2 } from 'pkgexports/sub/asdf.js';
3+
export { default as space } from 'pkgexports/space';

β€Žtest/parallel/test-module-package-exports.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ assert.strictEqual(fixtureRequire('baz/index'), 'eye catcher');
1616

1717
assert.strictEqual(fixtureRequire('pkgexports/sub/asdf.js'), 'asdf');
1818

19+
assert.strictEqual(fixtureRequire('pkgexports/space'), 'encoded path');
20+
1921
assert.throws(
2022
() => fixtureRequire('pkgexports/not-a-known-entry'),
2123
(e) => {

0 commit comments

Comments
Β (0)