Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! module: implement "exports" proposal for CommonJS
  • Loading branch information
hybrist committed Jul 23, 2019
commit 8b29deb7ebddb4752287f58eee71d818b0527c8f
19 changes: 13 additions & 6 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@

'use strict';

const { JSON, Object, Reflect } = primordials;
const {
ArrayPrototype,
JSON,
Object,
Reflect,
SafeMap,
StringPrototype,
} = primordials;

const { NativeModule } = require('internal/bootstrap/loaders');
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
Expand Down Expand Up @@ -185,7 +192,7 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
// Check if the directory is a package.json dir.
const packageMainCache = Object.create(null);
// Explicit exports from package.json files
const packageExportsCache = new Map();
const packageExportsCache = new SafeMap();

function readPackageRaw(requestPath) {
const jsonPath = path.resolve(requestPath, 'package.json');
Expand Down Expand Up @@ -335,7 +342,7 @@ function resolveExports(nmPath, request, absoluteRequest) {
// The implementation's behavior is meant to mirror resolution in ESM.
if (experimentalExports && !absoluteRequest) {
const [, name, expansion] =
request.match(EXPORTS_PATTERN) || [];
StringPrototype.match(request, EXPORTS_PATTERN) || [];
if (!name) {
return path.resolve(nmPath, request);
}
Expand All @@ -355,17 +362,17 @@ function resolveExports(nmPath, request, absoluteRequest) {
if (candidateKey[candidateKey.length - 1] !== '/') continue;
if (candidateValue[candidateValue.length - 1] !== '/') continue;
if (candidateKey.length > dirMatch.length &&
mappingKey.startsWith(candidateKey)) {
StringPrototype.startsWith(mappingKey, candidateKey)) {
dirMatch = candidateKey;
}
}

if (dirMatch !== '') {
const dirMapping = pkgExports[dirMatch];
const remainder = mappingKey.slice(dirMatch.length);
const remainder = ArrayPrototype.slice(mappingKey, dirMatch.length);
const expectedPrefix = path.resolve(basePath, dirMapping);
Comment thread
hybrist marked this conversation as resolved.
Outdated
const resolved = path.resolve(expectedPrefix, remainder);
if (resolved.startsWith(expectedPrefix)) {
if (StringPrototype.startsWith(resolved, expectedPrefix)) {
return resolved;
}
}
Expand Down