Skip to content

Commit

Permalink
fixup! module: implement "exports" proposal for CommonJS
Browse files Browse the repository at this point in the history
Co-Authored-By: Guy Bedford <[email protected]>
  • Loading branch information
jkrems and guybedford committed Jul 23, 2019
1 parent b7258f9 commit bd68eaf
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,13 @@ function resolveExports(nmPath, request, absoluteRequest) {

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

if (pkgExports != null) {
const mappingKey = `.${expansion}`;
const mapping = pkgExports[mappingKey];
if (typeof mapping === 'string') {
return path.resolve(basePath, mapping);
return fileURLToPath(new URL(mapping, baseURL));
}

let dirMatch = '';
Expand All @@ -369,10 +370,10 @@ function resolveExports(nmPath, request, absoluteRequest) {
if (dirMatch !== '') {
const dirMapping = pkgExports[dirMatch];
const remainder = StringPrototype.slice(mappingKey, dirMatch.length);
const expectedPrefix = path.resolve(basePath, dirMapping);
const resolved = path.resolve(expectedPrefix, remainder);
const expectedPrefix = new URL(dirMapping, baseURL).href;
const resolved = new URL(remainder, expectedPrefix).href;
if (StringPrototype.startsWith(resolved, expectedPrefix)) {
return resolved;
return fileURLToPath(resolved);
}
}
throw new ERR_PATH_NOT_EXPORTED(basePath, mappingKey);
Expand Down
3 changes: 2 additions & 1 deletion test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { mustCall } from '../common/index.mjs';
import { ok, strictEqual } from 'assert';

import { asdf, asdf2 } from '../fixtures/pkgexports.mjs';
import { asdf, asdf2, space } from '../fixtures/pkgexports.mjs';
import {
loadMissing,
loadFromNumber,
Expand All @@ -12,6 +12,7 @@ import {

strictEqual(asdf, 'asdf');
strictEqual(asdf2, 'asdf');
strictEqual(space, 'encoded path');

loadMissing().catch(mustCall((err) => {
ok(err.message.toString().startsWith('Package exports'));
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/node_modules/pkgexports/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/node_modules/pkgexports/sp ce.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/pkgexports.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as asdf } from 'pkgexports/asdf';
export { default as asdf2 } from 'pkgexports/sub/asdf.js';
export { default as space } from 'pkgexports/space';
2 changes: 2 additions & 0 deletions test/parallel/test-module-package-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ assert.strictEqual(fixtureRequire('baz/index'), 'eye catcher');

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

assert.strictEqual(fixtureRequire('pkgexports/space'), 'encoded path');

assert.throws(
() => fixtureRequire('pkgexports/not-a-known-entry'),
(e) => {
Expand Down

0 comments on commit bd68eaf

Please sign in to comment.