webpack/lib/util/identifier.js

108 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-07-30 23:08:51 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
2018-07-30 23:08:51 +08:00
const path = require("path");
/**
* @typedef {Object} MakeRelativePathsCache
* @property {Map<string, Map<string, string>>=} relativePaths
*/
/**
* @param {string} context context for relative path
* @param {string} maybeAbsolutePath path to make relative
* @returns {string} relative path in request style
*/
const absoluteToRequest = (context, maybeAbsolutePath) => {
if (/^\/.*\/$/.test(maybeAbsolutePath)) {
// this 'path' is actually a regexp generated by dynamic requires.
// Don't treat it as an absolute path.
return maybeAbsolutePath;
}
if (/^[a-zA-Z]:\\/.test(maybeAbsolutePath)) {
const splitPath = maybeAbsolutePath.split("?", 2);
splitPath[0] = path.win32.relative(context, splitPath[0]);
if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
splitPath[0] = splitPath[0].replace(/\\/g, "/");
2018-12-08 16:21:35 +08:00
if (!/^\.\.\//.test(splitPath[0])) {
splitPath[0] = "./" + splitPath[0];
}
}
return splitPath.join("?");
} else if (/^\//.test(maybeAbsolutePath)) {
const splitPath = maybeAbsolutePath.split("?", 2);
splitPath[0] = path.posix.relative(context, splitPath[0]);
2018-12-08 16:21:35 +08:00
if (!/^\.\.\//.test(splitPath[0])) {
splitPath[0] = "./" + splitPath[0];
}
return splitPath.join("?");
} else {
// not an absolute path
return maybeAbsolutePath;
}
};
/**
*
* @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
.split(/([|!])/)
.map(str => absoluteToRequest(context, str))
.join("");
2017-07-18 04:47:18 +08:00
};
const makePathsRelativeCache = 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
*/
exports.makePathsRelative = (context, identifier, associatedObjectForCache) => {
if (!associatedObjectForCache) return _makePathsRelative(context, identifier);
let relativePaths = makePathsRelativeCache.get(associatedObjectForCache);
if (relativePaths === undefined) {
relativePaths = new Map();
makePathsRelativeCache.set(associatedObjectForCache, relativePaths);
}
2017-07-18 14:48:39 +08:00
let cachedResult;
2017-07-18 15:10:00 +08:00
let contextCache = relativePaths.get(context);
2018-08-21 08:26:50 +08:00
if (contextCache === undefined) {
2018-02-25 09:00:20 +08:00
relativePaths.set(context, (contextCache = new Map()));
2017-07-18 14:48:39 +08:00
} else {
2017-07-18 15:10:00 +08:00
cachedResult = contextCache.get(identifier);
2017-07-18 14:48:39 +08:00
}
2018-08-21 08:26:50 +08:00
if (cachedResult !== undefined) {
2017-07-18 14:48:39 +08:00
return cachedResult;
} else {
2017-07-18 14:48:39 +08:00
const relativePath = _makePathsRelative(context, identifier);
contextCache.set(identifier, relativePath);
return relativePath;
}
};
/**
* @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
.split("!")
.map(r => absoluteToRequest(context, r))
.join("!");
};
exports.contextify = contextify;