webpack/lib/util/identifier.js

37 lines
992 B
JavaScript
Raw Normal View History

"use strict";
const path = require("path");
2017-04-07 21:37:38 +08:00
const looksLikeAbsolutePath = (maybeAbsolutePath) => {
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
};
2017-04-07 21:37:38 +08:00
const normalizePathSeparator = (p) => p.replace(/\\/g, "/");
const _makePathsRelative = (context, identifier) => {
return identifier
.split(/([|! ])/)
.map(str => looksLikeAbsolutePath(str) ?
normalizePathSeparator(path.relative(context, str)) : str)
.join("");
2017-07-18 04:47:18 +08:00
};
exports.makePathsRelative = (context, identifier, cache) => {
if(!cache) return _makePathsRelative(context, identifier);
const relativePaths = cache.relativePaths || (cache.relativePaths = new Map());
if(!relativePaths.has(context)) {
relativePaths.set(context, new Map());
}
const contextCache = relativePaths.get(context);
if(contextCache.has(identifier)) {
return contextCache.get(identifier);
} else {
let relativePath = _makePathsRelative(context, identifier);
contextCache.set(identifier, relativePath);
return relativePath;
}
};