Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix $ pattern replacements #40044

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -10,6 +10,7 @@ const {
ObjectGetOwnPropertyNames,
ObjectPrototypeHasOwnProperty,
RegExp,
RegExpPrototypeSymbolReplace,
RegExpPrototypeTest,
SafeMap,
SafeSet,
Expand All @@ -18,7 +19,6 @@ const {
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -461,7 +461,7 @@ function resolvePackageTargetString(
} catch {}
if (!isURL) {
const exportTarget = pattern ?
StringPrototypeReplace(target, patternRegEx, subpath) :
RegExpPrototypeSymbolReplace(target, patternRegEx, () => subpath) :
target + subpath;
return packageResolve(exportTarget, packageJSONUrl, conditions);
}
Expand All @@ -485,8 +485,8 @@ function resolvePackageTargetString(
throwInvalidSubpath(match + subpath, packageJSONUrl, internal, base);

if (pattern)
return new URL(StringPrototypeReplace(resolved.href, patternRegEx,
subpath));
return new URL(RegExpPrototypeSymbolReplace(resolved.href, patternRegEx,
() => subpath));
return new URL(subpath, resolved);
}

Expand Down Expand Up @@ -930,7 +930,8 @@ function resolveAsCommonJS(specifier, parentURL) {
// Normalize the path separator to give a valid suggestion
// on Windows
if (process.platform === 'win32') {
found = StringPrototypeReplace(found, new RegExp(`\\${sep}`, 'g'), '/');
found = RegExpPrototypeSymbolReplace(found,
new RegExp(`\\${sep}`, 'g'), '/');
guybedford marked this conversation as resolved.
Show resolved Hide resolved
}
return found;
} catch {
Expand Down
2 changes: 2 additions & 0 deletions test/es-module/test-esm-exports.mjs
Expand Up @@ -156,6 +156,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/no-ext', `pkgexports${sep}asdf`],
// Pattern specificity
['pkgexports/dir2/trailer', `subpath${sep}dir2.js`],
// Pattern double $$ escaping!
['pkgexports/a/$$', `subpath${sep}$$.js`],
]);

if (!isRequire) {
Expand Down