Skip to content

Commit

Permalink
module: CJS extension searching for folder exports
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 26, 2020
1 parent f399cfd commit ce967de
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 43 deletions.
38 changes: 22 additions & 16 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,32 @@ LOAD_SELF_REFERENCE(X, START)
3. If the `package.json` has no "exports", return.
4. If the name in `package.json` isn't a prefix of X, throw "not found".
5. Otherwise, load the remainder of X relative to this package as if it
was loaded via `LOAD_NODE_MODULES` with a name in `package.json`.
was loaded via `LOAD_NODE_MODULES` with a name in `package.json`.
LOAD_PACKAGE_EXPORTS(DIR, X)
1. Try to interpret X as a combination of name and subpath where the name
may have a @scope/ prefix and the subpath begins with a slash (`/`).
2. If X matches this pattern and DIR/name/package.json is a file:
a. Parse DIR/name/package.json, and look for "exports" field.
b. If "exports" is null or undefined, return.
c. If "exports" is an object with some keys starting with "." and some keys
not starting with ".", throw "invalid config".
d. If "exports" is a string, or object with no keys starting with ".", treat
it as having that value as its "." object property.
e. If subpath is "." and "exports" does not have a "." entry, return.
f. Find the longest key in "exports" that the subpath starts with.
g. If no such key can be found, throw "not found".
h. let RESOLVED_URL =
PACKAGE_EXPORTS_TARGET_RESOLVE(pathToFileURL(DIR/name), exports[key],
subpath.slice(key.length), ["node", "require"]), as defined in the ESM
resolver.
i. Load fileURLToPath(RESOLVED_URL) as its file extension format. STOP
2. If X does not match this pattern or DIR/name/package.json is not a file,
return.
3. Parse DIR/name/package.json, and look for "exports" field.
4. If "exports" is null or undefined, return.
5. If "exports" is an object with some keys starting with "." and some keys
not starting with ".", throw "invalid config".
6. If "exports" is a string, or object with no keys starting with ".", treat
it as having that value as its "." object property.
7. If subpath is "." and "exports" does not have a "." entry, return.
8. Find the longest key in "exports" that the subpath starts with.
9. If no such key can be found, throw "not found".
10. let RESOLVED =
fileURLToPath(PACKAGE_EXPORTS_TARGET_RESOLVE(pathToFileURL(DIR/name),
exports[key], subpath.slice(key.length), ["node", "require"])), as defined
in the ESM resolver.
11. If key ends with "/":
a. LOAD_AS_FILE(RESOLVED)
b. LOAD_AS_DIRECTORY(RESOLVED)
12. Otherwise
a. If RESOLVED is a file, load it as its file extension format. STOP
13. Throw "not found"
```

## Caching
Expand Down
42 changes: 32 additions & 10 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
ReflectSet,
RegExpPrototypeTest,
SafeMap,
String,
StringPrototypeIndexOf,
Expand Down Expand Up @@ -125,7 +126,7 @@ function enrichCJSError(err) {
after a comment block and/or after a variable definition.
*/
if (err.message.startsWith('Unexpected token \'export\'') ||
(/^\s*import(?=[ {'"*])\s*(?![ (])/).test(lineWithErr)) {
(RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, lineWithErr))) {
// Emit the warning synchronously because we are in the middle of handling
// a SyntaxError that will throw and likely terminate the process before an
// asynchronous warning would be emitted.
Expand Down Expand Up @@ -352,10 +353,11 @@ const realpathCache = new Map();
// absolute realpath.
function tryFile(requestPath, isMain) {
const rc = stat(requestPath);
if (rc !== 0) return;
if (preserveSymlinks && !isMain) {
return rc === 0 && path.resolve(requestPath);
return path.resolve(requestPath);
}
return rc === 0 && toRealPath(requestPath);
return toRealPath(requestPath);
}

function toRealPath(requestPath) {
Expand Down Expand Up @@ -410,7 +412,7 @@ function trySelf(parentPath, request) {
if (fromExports) {
return tryFile(fromExports, false);
}
assert(false);
assert(fromExports !== false);
}

function isConditionalDotExportSugar(exports, basePath) {
Expand Down Expand Up @@ -466,8 +468,24 @@ function applyExports(basePath, expansion) {
if (dirMatch !== '') {
const mapping = pkgExports[dirMatch];
const subpath = StringPrototypeSlice(mappingKey, dirMatch.length);
return resolveExportsTarget(pathToFileURL(basePath + '/'), mapping,
subpath, mappingKey);
const resolved = resolveExportsTarget(pathToFileURL(basePath + '/'),
mapping, subpath, mappingKey);
// Extension searching for folder exports only
const rc = stat(resolved);
if (rc === 0) return resolved;
if (!(RegExpPrototypeTest(trailingSlashRegex, resolved))) {
const exts = ObjectKeys(Module._extensions);
const filename = tryExtensions(resolved, exts, false);
if (filename) return filename;
}
if (rc === 1) {
const exts = ObjectKeys(Module._extensions);
const filename = tryPackage(resolved, exts, false,
basePath + expansion);
if (filename) return filename;
}
// Undefined means not found
return;
}
}

Expand All @@ -488,10 +506,10 @@ function resolveExports(nmPath, request) {

const basePath = path.resolve(nmPath, name);
const fromExports = applyExports(basePath, expansion);
if (!fromExports) {
return false;
if (fromExports) {
return tryFile(fromExports, false);
}
return tryFile(fromExports, false);
return fromExports;
}

function isArrayIndex(p) {
Expand Down Expand Up @@ -582,6 +600,7 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
StringPrototypeSlice(baseUrl.pathname, 0, -1), mappingKey, subpath, target);
}

const trailingSlashRegex = /(?:^|\/)\.?\.$/;
Module._findPath = function(request, paths, isMain) {
const absoluteRequest = path.isAbsolute(request);
if (absoluteRequest) {
Expand All @@ -600,7 +619,7 @@ Module._findPath = function(request, paths, isMain) {
let trailingSlash = request.length > 0 &&
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
if (!trailingSlash) {
trailingSlash = /(?:^|\/)\.?\.$/.test(request);
trailingSlash = RegExpPrototypeTest(trailingSlashRegex, request);
}

// For each path
Expand All @@ -611,6 +630,9 @@ Module._findPath = function(request, paths, isMain) {

if (!absoluteRequest) {
const exportsResolved = resolveExports(curPath, request);
// Undefined means not found, false means no exports
if (exportsResolved === undefined)
break;
if (exportsResolved) {
return exportsResolved;
}
Expand Down
45 changes: 29 additions & 16 deletions test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports-sugar', { default: 'main' }],
]);

if (isRequire) {
validSpecifiers.set('pkgexports/subpath/file', { default: 'file' });
validSpecifiers.set('pkgexports/subpath/dir1', { default: 'main' });
validSpecifiers.set('pkgexports/subpath/dir1/', { default: 'main' });
validSpecifiers.set('pkgexports/subpath/dir2', { default: 'index' });
validSpecifiers.set('pkgexports/subpath/dir2/', { default: 'index' });
}

for (const [validSpecifier, expected] of validSpecifiers) {
if (validSpecifier === null) continue;

Expand Down Expand Up @@ -118,23 +126,28 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
}));
}

// Non-existing file
loadFixture('pkgexports/sub/not-a-file.js').catch(mustCall((err) => {
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
// ESM returns a full file path
assertStartsWith(err.message, isRequire ?
'Cannot find module \'pkgexports/sub/not-a-file.js\'' :
'Cannot find module');
}));
const notFoundExports = new Map([
// Non-existing file
['pkgexports/sub/not-a-file.js', 'pkgexports/sub/not-a-file.js'],
// No extension lookups
['pkgexports/no-ext', 'pkgexports/no-ext'],
]);

// No extension lookups
loadFixture('pkgexports/no-ext').catch(mustCall((err) => {
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
// ESM returns a full file path
assertStartsWith(err.message, isRequire ?
'Cannot find module \'pkgexports/no-ext\'' :
'Cannot find module');
}));
if (!isRequire) {
notFoundExports.set('pkgexports/subpath/file', 'pkgexports/subpath/file');
notFoundExports.set('pkgexports/subpath/dir1', 'pkgexports/subpath/dir1');
notFoundExports.set('pkgexports/subpath/dir2', 'pkgexports/subpath/dir2');
}

for (const [specifier, request] of notFoundExports) {
loadFixture(specifier).catch(mustCall((err) => {
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
// ESM returns a full file path
assertStartsWith(err.message, isRequire ?
`Cannot find module '${request}'` :
'Cannot find module');
}));
}

// The use of %2F escapes in paths fails loading
loadFixture('pkgexports/sub/..%2F..%2Fbar.js').catch(mustCall((err) => {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/node_modules/pkgexports/package.json

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/node_modules/pkgexports/subpath/dir1/dir1.js

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

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

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

2 changes: 2 additions & 0 deletions test/fixtures/node_modules/pkgexports/subpath/file.js

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

0 comments on commit ce967de

Please sign in to comment.