feat: refactoring project
This commit is contained in:
140
node_modules/webpack/lib/util/identifier.js
generated
vendored
140
node_modules/webpack/lib/util/identifier.js
generated
vendored
@@ -85,24 +85,48 @@ const requestToAbsolute = (context, relativePath) => {
|
||||
return relativePath;
|
||||
};
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {function(string, object=): T} MakeCacheableResult
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {function(string): T} BindCacheResultFn
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {function(object): BindCacheResultFn<T>} BindCache
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {(function(string): T)} realFn real function
|
||||
* @returns {MakeCacheableResult<T> & { bindCache: BindCache<T> }} cacheable function
|
||||
*/
|
||||
const makeCacheable = realFn => {
|
||||
/** @type {WeakMap<object, Map<string, ParsedResource>>} */
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {Map<string, T>} CacheItem
|
||||
*/
|
||||
/** @type {WeakMap<object, CacheItem<T>>} */
|
||||
const cache = new WeakMap();
|
||||
|
||||
/**
|
||||
* @param {object} associatedObjectForCache an object to which the cache will be attached
|
||||
* @returns {CacheItem<T>} cache item
|
||||
*/
|
||||
const getCache = associatedObjectForCache => {
|
||||
const entry = cache.get(associatedObjectForCache);
|
||||
if (entry !== undefined) return entry;
|
||||
/** @type {Map<string, ParsedResource>} */
|
||||
/** @type {Map<string, T>} */
|
||||
const map = new Map();
|
||||
cache.set(associatedObjectForCache, map);
|
||||
return map;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str the path with query and fragment
|
||||
* @param {object=} associatedObjectForCache an object to which the cache will be attached
|
||||
* @returns {ParsedResource} parsed parts
|
||||
*/
|
||||
/** @type {MakeCacheableResult<T> & { bindCache: BindCache<T> }} */
|
||||
const fn = (str, associatedObjectForCache) => {
|
||||
if (!associatedObjectForCache) return realFn(str);
|
||||
const cache = getCache(associatedObjectForCache);
|
||||
@@ -113,8 +137,13 @@ const makeCacheable = realFn => {
|
||||
return result;
|
||||
};
|
||||
|
||||
/** @type {BindCache<T>} */
|
||||
fn.bindCache = associatedObjectForCache => {
|
||||
const cache = getCache(associatedObjectForCache);
|
||||
/**
|
||||
* @param {string} str string
|
||||
* @returns {T} value
|
||||
*/
|
||||
return str => {
|
||||
const entry = cache.get(str);
|
||||
if (entry !== undefined) return entry;
|
||||
@@ -127,16 +156,21 @@ const makeCacheable = realFn => {
|
||||
return fn;
|
||||
};
|
||||
|
||||
/** @typedef {function(string, string, object=): string} MakeCacheableWithContextResult */
|
||||
/** @typedef {function(string, string): string} BindCacheForContextResultFn */
|
||||
/** @typedef {function(string): string} BindContextCacheForContextResultFn */
|
||||
/** @typedef {function(object=): BindCacheForContextResultFn} BindCacheForContext */
|
||||
/** @typedef {function(string, object=): BindContextCacheForContextResultFn} BindContextCacheForContext */
|
||||
|
||||
/**
|
||||
* @param {function(string, string): string} fn function
|
||||
* @returns {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} cacheable function with context
|
||||
*/
|
||||
const makeCacheableWithContext = fn => {
|
||||
/** @type {WeakMap<object, Map<string, Map<string, string>>>} */
|
||||
const cache = new WeakMap();
|
||||
|
||||
/**
|
||||
* @param {string} context context used to create relative path
|
||||
* @param {string} identifier identifier used to create relative path
|
||||
* @param {object=} associatedObjectForCache an object to which the cache will be attached
|
||||
* @returns {string} the returned relative path
|
||||
*/
|
||||
/** @type {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} */
|
||||
const cachedFn = (context, identifier, associatedObjectForCache) => {
|
||||
if (!associatedObjectForCache) return fn(context, identifier);
|
||||
|
||||
@@ -156,17 +190,13 @@ const makeCacheableWithContext = fn => {
|
||||
|
||||
if (cachedResult !== undefined) {
|
||||
return cachedResult;
|
||||
} else {
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
}
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object=} associatedObjectForCache an object to which the cache will be attached
|
||||
* @returns {function(string, string): string} cached function
|
||||
*/
|
||||
/** @type {BindCacheForContext} */
|
||||
cachedFn.bindCache = associatedObjectForCache => {
|
||||
let innerCache;
|
||||
if (associatedObjectForCache) {
|
||||
@@ -195,21 +225,16 @@ const makeCacheableWithContext = fn => {
|
||||
|
||||
if (cachedResult !== undefined) {
|
||||
return cachedResult;
|
||||
} else {
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
}
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
return boundFn;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} context context used to create relative path
|
||||
* @param {object=} associatedObjectForCache an object to which the cache will be attached
|
||||
* @returns {function(string): string} cached function
|
||||
*/
|
||||
/** @type {BindContextCacheForContext} */
|
||||
cachedFn.bindContextCache = (context, associatedObjectForCache) => {
|
||||
let innerSubCache;
|
||||
if (associatedObjectForCache) {
|
||||
@@ -235,11 +260,10 @@ const makeCacheableWithContext = fn => {
|
||||
const cachedResult = innerSubCache.get(identifier);
|
||||
if (cachedResult !== undefined) {
|
||||
return cachedResult;
|
||||
} else {
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
}
|
||||
const result = fn(context, identifier);
|
||||
innerSubCache.set(identifier, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
return boundFn;
|
||||
@@ -249,64 +273,58 @@ const makeCacheableWithContext = fn => {
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} context context for relative path
|
||||
* @param {string} identifier identifier for path
|
||||
* @returns {string} a converted relative path
|
||||
*/
|
||||
const _makePathsRelative = (context, identifier) => {
|
||||
return identifier
|
||||
const _makePathsRelative = (context, identifier) =>
|
||||
identifier
|
||||
.split(SEGMENTS_SPLIT_REGEXP)
|
||||
.map(str => absoluteToRequest(context, str))
|
||||
.join("");
|
||||
};
|
||||
|
||||
exports.makePathsRelative = makeCacheableWithContext(_makePathsRelative);
|
||||
module.exports.makePathsRelative = makeCacheableWithContext(_makePathsRelative);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} context context for relative path
|
||||
* @param {string} identifier identifier for path
|
||||
* @returns {string} a converted relative path
|
||||
*/
|
||||
const _makePathsAbsolute = (context, identifier) => {
|
||||
return identifier
|
||||
const _makePathsAbsolute = (context, identifier) =>
|
||||
identifier
|
||||
.split(SEGMENTS_SPLIT_REGEXP)
|
||||
.map(str => requestToAbsolute(context, str))
|
||||
.join("");
|
||||
};
|
||||
|
||||
exports.makePathsAbsolute = makeCacheableWithContext(_makePathsAbsolute);
|
||||
module.exports.makePathsAbsolute = makeCacheableWithContext(_makePathsAbsolute);
|
||||
|
||||
/**
|
||||
* @param {string} context absolute context path
|
||||
* @param {string} request any request string may containing absolute paths, query string, etc.
|
||||
* @returns {string} a new request string avoiding absolute paths when possible
|
||||
*/
|
||||
const _contextify = (context, request) => {
|
||||
return request
|
||||
const _contextify = (context, request) =>
|
||||
request
|
||||
.split("!")
|
||||
.map(r => absoluteToRequest(context, r))
|
||||
.join("!");
|
||||
};
|
||||
|
||||
const contextify = makeCacheableWithContext(_contextify);
|
||||
exports.contextify = contextify;
|
||||
module.exports.contextify = contextify;
|
||||
|
||||
/**
|
||||
* @param {string} context absolute context path
|
||||
* @param {string} request any request string
|
||||
* @returns {string} a new request string using absolute paths when possible
|
||||
*/
|
||||
const _absolutify = (context, request) => {
|
||||
return request
|
||||
const _absolutify = (context, request) =>
|
||||
request
|
||||
.split("!")
|
||||
.map(r => requestToAbsolute(context, r))
|
||||
.join("!");
|
||||
};
|
||||
|
||||
const absolutify = makeCacheableWithContext(_absolutify);
|
||||
exports.absolutify = absolutify;
|
||||
module.exports.absolutify = absolutify;
|
||||
|
||||
const PATH_QUERY_FRAGMENT_REGEXP =
|
||||
/^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
|
||||
@@ -320,7 +338,9 @@ const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
|
||||
* @returns {ParsedResource} parsed parts
|
||||
*/
|
||||
const _parseResource = str => {
|
||||
const match = PATH_QUERY_FRAGMENT_REGEXP.exec(str);
|
||||
const match =
|
||||
/** @type {[string, string, string | undefined, string | undefined]} */
|
||||
(/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
|
||||
return {
|
||||
resource: str,
|
||||
path: match[1].replace(/\0(.)/g, "$1"),
|
||||
@@ -328,7 +348,7 @@ const _parseResource = str => {
|
||||
fragment: match[3] || ""
|
||||
};
|
||||
};
|
||||
exports.parseResource = makeCacheable(_parseResource);
|
||||
module.exports.parseResource = makeCacheable(_parseResource);
|
||||
|
||||
/**
|
||||
* Parse resource, skips fragment part
|
||||
@@ -336,14 +356,16 @@ exports.parseResource = makeCacheable(_parseResource);
|
||||
* @returns {ParsedResourceWithoutFragment} parsed parts
|
||||
*/
|
||||
const _parseResourceWithoutFragment = str => {
|
||||
const match = PATH_QUERY_REGEXP.exec(str);
|
||||
const match =
|
||||
/** @type {[string, string, string | undefined]} */
|
||||
(/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
|
||||
return {
|
||||
resource: str,
|
||||
path: match[1].replace(/\0(.)/g, "$1"),
|
||||
query: match[2] ? match[2].replace(/\0(.)/g, "$1") : ""
|
||||
};
|
||||
};
|
||||
exports.parseResourceWithoutFragment = makeCacheable(
|
||||
module.exports.parseResourceWithoutFragment = makeCacheable(
|
||||
_parseResourceWithoutFragment
|
||||
);
|
||||
|
||||
@@ -353,7 +375,7 @@ exports.parseResourceWithoutFragment = makeCacheable(
|
||||
* @param {boolean} enforceRelative true returns ./ for empty paths
|
||||
* @returns {string} repeated ../ to leave the directory of the provided filename to be back on output dir
|
||||
*/
|
||||
exports.getUndoPath = (filename, outputPath, enforceRelative) => {
|
||||
module.exports.getUndoPath = (filename, outputPath, enforceRelative) => {
|
||||
let depth = -1;
|
||||
let append = "";
|
||||
outputPath = outputPath.replace(/[\\/]$/, "");
|
||||
@@ -365,8 +387,8 @@ exports.getUndoPath = (filename, outputPath, enforceRelative) => {
|
||||
const i = outputPath.lastIndexOf("/");
|
||||
const j = outputPath.lastIndexOf("\\");
|
||||
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
|
||||
if (pos < 0) return outputPath + "/";
|
||||
append = outputPath.slice(pos + 1) + "/" + append;
|
||||
if (pos < 0) return `${outputPath}/`;
|
||||
append = `${outputPath.slice(pos + 1)}/${append}`;
|
||||
outputPath = outputPath.slice(0, pos);
|
||||
}
|
||||
} else if (part !== ".") {
|
||||
|
||||
Reference in New Issue
Block a user