enhanced-resolve
Advanced tools
Comparing version 5.17.0 to 5.17.1
@@ -12,3 +12,6 @@ /* | ||
const { parseIdentifier } = require("./util/identifier"); | ||
const { checkImportsExportsFieldTarget } = require("./util/path"); | ||
const { | ||
invalidSegmentRegEx, | ||
deprecatedInvalidSegmentRegEx | ||
} = require("./util/path"); | ||
@@ -83,2 +86,4 @@ /** @typedef {import("./Resolver")} Resolver */ | ||
let paths; | ||
/** @type {string | null} */ | ||
let usedField; | ||
@@ -99,3 +104,6 @@ try { | ||
} | ||
paths = fieldProcessor(remainingRequest, this.conditionNames); | ||
[paths, usedField] = fieldProcessor( | ||
remainingRequest, | ||
this.conditionNames | ||
); | ||
} catch (/** @type {unknown} */ err) { | ||
@@ -123,5 +131,6 @@ if (resolveContext.log) { | ||
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback | ||
* @param {number} i index | ||
* @returns {void} | ||
*/ | ||
(p, callback) => { | ||
(p, callback, i) => { | ||
const parsedIdentifier = parseIdentifier(p); | ||
@@ -133,8 +142,29 @@ | ||
const error = checkImportsExportsFieldTarget(relativePath); | ||
if (relativePath.length === 0 || !relativePath.startsWith("./")) { | ||
if (paths.length === i) { | ||
return callback( | ||
new Error( | ||
`Invalid "exports" target "${p}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"` | ||
) | ||
); | ||
} | ||
if (error) { | ||
return callback(error); | ||
return callback(); | ||
} | ||
if ( | ||
invalidSegmentRegEx.exec(relativePath.slice(2)) !== null && | ||
deprecatedInvalidSegmentRegEx.test(relativePath.slice(2)) !== null | ||
) { | ||
if (paths.length === i) { | ||
return callback( | ||
new Error( | ||
`Invalid "exports" target "${p}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"` | ||
) | ||
); | ||
} | ||
return callback(); | ||
} | ||
/** @type {ResolveRequest} */ | ||
@@ -158,3 +188,8 @@ const obj = { | ||
resolveContext, | ||
callback | ||
(err, result) => { | ||
if (err) return callback(err); | ||
// Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400 | ||
if (result === undefined) return callback(null, null); | ||
callback(null, result); | ||
} | ||
); | ||
@@ -161,0 +196,0 @@ }, |
@@ -25,3 +25,3 @@ /* | ||
* @param {Iterator<T, Z>} iterator iterator | ||
* @param {(err?: null|Error, result?: null|Z) => void} callback callback after all items are iterated | ||
* @param {(err?: null|Error, result?: null|Z, i?: number) => void} callback callback after all items are iterated | ||
* @returns {void} | ||
@@ -40,3 +40,3 @@ */ | ||
if (err || result !== undefined || i >= array.length) { | ||
return callback(err, result); | ||
return callback(err, result, i); | ||
} | ||
@@ -43,0 +43,0 @@ if (loop === false) while (next()); |
@@ -12,3 +12,6 @@ /* | ||
const { parseIdentifier } = require("./util/identifier"); | ||
const { checkImportsExportsFieldTarget } = require("./util/path"); | ||
const { | ||
invalidSegmentRegEx, | ||
deprecatedInvalidSegmentRegEx | ||
} = require("./util/path"); | ||
@@ -86,2 +89,4 @@ /** @typedef {import("./Resolver")} Resolver */ | ||
let paths; | ||
/** @type {string | null} */ | ||
let usedField; | ||
@@ -102,3 +107,6 @@ try { | ||
} | ||
paths = fieldProcessor(remainingRequest, this.conditionNames); | ||
[paths, usedField] = fieldProcessor( | ||
remainingRequest, | ||
this.conditionNames | ||
); | ||
} catch (/** @type {unknown} */ err) { | ||
@@ -126,5 +134,6 @@ if (resolveContext.log) { | ||
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback | ||
* @param {number} i index | ||
* @returns {void} | ||
*/ | ||
(p, callback) => { | ||
(p, callback, i) => { | ||
const parsedIdentifier = parseIdentifier(p); | ||
@@ -136,11 +145,20 @@ | ||
const error = checkImportsExportsFieldTarget(path_); | ||
if (error) { | ||
return callback(error); | ||
} | ||
switch (path_.charCodeAt(0)) { | ||
// should be relative | ||
case dotCode: { | ||
if ( | ||
invalidSegmentRegEx.exec(path_.slice(2)) !== null && | ||
deprecatedInvalidSegmentRegEx.test(path_.slice(2)) !== null | ||
) { | ||
if (paths.length === i) { | ||
return callback( | ||
new Error( | ||
`Invalid "imports" target "${p}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"` | ||
) | ||
); | ||
} | ||
return callback(); | ||
} | ||
/** @type {ResolveRequest} */ | ||
@@ -164,3 +182,8 @@ const obj = { | ||
resolveContext, | ||
callback | ||
(err, result) => { | ||
if (err) return callback(err); | ||
// Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400 | ||
if (result === undefined) return callback(null, null); | ||
callback(null, result); | ||
} | ||
); | ||
@@ -187,3 +210,8 @@ break; | ||
resolveContext, | ||
callback | ||
(err, result) => { | ||
if (err) return callback(err); | ||
// Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400 | ||
if (result === undefined) return callback(null, null); | ||
callback(null, result); | ||
} | ||
); | ||
@@ -190,0 +218,0 @@ } |
@@ -695,3 +695,7 @@ /* | ||
} else { | ||
newStack = new Set([stackEntry]); | ||
// creating a set with new Set([item]) | ||
// allocates a new array that has to be garbage collected | ||
// this is an EXTREMELY hot path, so let's avoid it | ||
newStack = new Set(); | ||
newStack.add(stackEntry); | ||
} | ||
@@ -698,0 +702,0 @@ this.hooks.resolveStep.call(hook, request); |
@@ -19,3 +19,3 @@ /* | ||
* @param {Set<string>} conditionNames condition names | ||
* @returns {string[]} resolved paths | ||
* @returns {[string[], string | null]} resolved paths with used field | ||
*/ | ||
@@ -76,2 +76,3 @@ | ||
const { parseIdentifier } = require("./identifier"); | ||
const slashCode = "/".charCodeAt(0); | ||
@@ -105,3 +106,3 @@ const dotCode = ".".charCodeAt(0); | ||
return createFieldProcessor( | ||
buildImportsField(importsField), | ||
importsField, | ||
request => "#" + request, | ||
@@ -131,5 +132,6 @@ assertImportsFieldRequest, | ||
if (match === null) return []; | ||
if (match === null) return [[], null]; | ||
const [mapping, remainingRequest, isSubpathMapping, isPattern] = match; | ||
const [mapping, remainingRequest, isSubpathMapping, isPattern, usedField] = | ||
match; | ||
@@ -146,3 +148,3 @@ /** @type {DirectMapping|null} */ | ||
// matching not found | ||
if (direct === null) return []; | ||
if (direct === null) return [[], null]; | ||
} else { | ||
@@ -152,10 +154,13 @@ direct = /** @type {DirectMapping} */ (mapping); | ||
return directMapping( | ||
remainingRequest, | ||
isPattern, | ||
isSubpathMapping, | ||
direct, | ||
conditionNames, | ||
assertTarget | ||
); | ||
return [ | ||
directMapping( | ||
remainingRequest, | ||
isPattern, | ||
isSubpathMapping, | ||
direct, | ||
conditionNames, | ||
assertTarget | ||
), | ||
usedField | ||
]; | ||
}; | ||
@@ -209,14 +214,11 @@ } | ||
function assertExportTarget(exp, expectFolder) { | ||
if ( | ||
exp.charCodeAt(0) === slashCode || | ||
(exp.charCodeAt(0) === dotCode && exp.charCodeAt(1) !== slashCode) | ||
) { | ||
throw new Error( | ||
`Export should be relative path and start with "./", got ${JSON.stringify( | ||
exp | ||
)}.` | ||
); | ||
const parsedIdentifier = parseIdentifier(exp); | ||
if (!parsedIdentifier) { | ||
return; | ||
} | ||
const isFolder = exp.charCodeAt(exp.length - 1) === slashCode; | ||
const [relativePath] = parsedIdentifier; | ||
const isFolder = | ||
relativePath.charCodeAt(relativePath.length - 1) === slashCode; | ||
@@ -241,4 +243,12 @@ if (isFolder !== expectFolder) { | ||
function assertImportTarget(imp, expectFolder) { | ||
const isFolder = imp.charCodeAt(imp.length - 1) === slashCode; | ||
const parsedIdentifier = parseIdentifier(imp); | ||
if (!parsedIdentifier) { | ||
return; | ||
} | ||
const [relativePath] = parsedIdentifier; | ||
const isFolder = | ||
relativePath.charCodeAt(relativePath.length - 1) === slashCode; | ||
if (isFolder !== expectFolder) { | ||
@@ -282,3 +292,3 @@ throw new Error( | ||
* @param {ExportsField | ImportsField} field exports or import field | ||
* @returns {[MappingValue, string, boolean, boolean]|null} match or null, number is negative and one less when it's a folder mapping, number is request.length + 1 for direct mappings | ||
* @returns {[MappingValue, string, boolean, boolean, string]|null} match or null, number is negative and one less when it's a folder mapping, number is request.length + 1 for direct mappings | ||
*/ | ||
@@ -293,3 +303,3 @@ function findMatch(request, field) { | ||
return [target, "", false, false]; | ||
return [target, "", false, false, request]; | ||
} | ||
@@ -345,3 +355,4 @@ | ||
isSubpathMapping, | ||
isPattern | ||
isPattern, | ||
bestMatch | ||
]; | ||
@@ -574,37 +585,1 @@ } | ||
} | ||
/** | ||
* @param {ImportsField} field imports field | ||
* @returns {ImportsField} normalized imports field | ||
*/ | ||
function buildImportsField(field) { | ||
const keys = Object.keys(field); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
if (key.charCodeAt(0) !== hashCode) { | ||
throw new Error( | ||
`Imports field key should start with "#" (key: ${JSON.stringify(key)})` | ||
); | ||
} | ||
if (key.length === 1) { | ||
throw new Error( | ||
`Imports field key should have at least 2 characters (key: ${JSON.stringify( | ||
key | ||
)})` | ||
); | ||
} | ||
if (key.charCodeAt(1) === slashCode) { | ||
throw new Error( | ||
`Imports field key should not start with "#/" (key: ${JSON.stringify( | ||
key | ||
)})` | ||
); | ||
} | ||
} | ||
return field; | ||
} |
@@ -36,2 +36,10 @@ /* | ||
const invalidSegmentRegEx = | ||
/(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i; | ||
exports.invalidSegmentRegEx = invalidSegmentRegEx; | ||
const deprecatedInvalidSegmentRegEx = | ||
/(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i; | ||
exports.deprecatedInvalidSegmentRegEx = deprecatedInvalidSegmentRegEx; | ||
/** | ||
@@ -197,35 +205,1 @@ * @param {string} p a path | ||
exports.cachedJoin = cachedJoin; | ||
/** | ||
* @param {string} relativePath relative path | ||
* @returns {undefined|Error} nothing or an error | ||
*/ | ||
const checkImportsExportsFieldTarget = relativePath => { | ||
let lastNonSlashIndex = 0; | ||
let slashIndex = relativePath.indexOf("/", 1); | ||
let cd = 0; | ||
while (slashIndex !== -1) { | ||
const folder = relativePath.slice(lastNonSlashIndex, slashIndex); | ||
switch (folder) { | ||
case "..": { | ||
cd--; | ||
if (cd < 0) | ||
return new Error( | ||
`Trying to access out of package scope. Requesting ${relativePath}` | ||
); | ||
break; | ||
} | ||
case ".": | ||
break; | ||
default: | ||
cd++; | ||
break; | ||
} | ||
lastNonSlashIndex = slashIndex + 1; | ||
slashIndex = relativePath.indexOf("/", lastNonSlashIndex); | ||
} | ||
}; | ||
exports.checkImportsExportsFieldTarget = checkImportsExportsFieldTarget; |
{ | ||
"name": "enhanced-resolve", | ||
"version": "5.17.0", | ||
"version": "5.17.1", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -57,3 +57,3 @@ "description": "Offers a async require.resolve function. It's highly configurable.", | ||
"spelling": "cspell \"**\"", | ||
"test:only": "jest", | ||
"test:only": "node_modules/.bin/jest", | ||
"test:watch": "yarn test:only -- --watch", | ||
@@ -60,0 +60,0 @@ "test:coverage": "yarn test:only -- --collectCoverageFrom=\"lib/**/*.js\" --coverage", |
@@ -1111,3 +1111,3 @@ /* | ||
iterator: Iterator<T, Z>, | ||
callback: (err?: null | Error, result?: null | Z) => void | ||
callback: (err?: null | Error, result?: null | Z, i?: number) => void | ||
) => void; | ||
@@ -1114,0 +1114,0 @@ export type ResolveCallback = ( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
211733
6757