mirror of https://github.com/webpack/webpack.git
				
				
				
			
		
			
				
	
	
		
			130 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| /*
 | |
| 	MIT License http://www.opensource.org/licenses/mit-license.php
 | |
| */
 | |
| 
 | |
| "use strict";
 | |
| 
 | |
| const path = require("path");
 | |
| 
 | |
| const WINDOWS_ABS_PATH_REGEXP = /^[a-zA-Z]:\\/;
 | |
| const SEGEMENTS_SPLIT_REGEXP = /([|!])/;
 | |
| const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;
 | |
| 
 | |
| /**
 | |
|  * @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 (maybeAbsolutePath[0] === "/") {
 | |
| 		if (
 | |
| 			maybeAbsolutePath.length > 1 &&
 | |
| 			maybeAbsolutePath[maybeAbsolutePath.length - 1] === "/"
 | |
| 		) {
 | |
| 			// this 'path' is actually a regexp generated by dynamic requires.
 | |
| 			// Don't treat it as an absolute path.
 | |
| 			return maybeAbsolutePath;
 | |
| 		}
 | |
| 
 | |
| 		const querySplitPos = maybeAbsolutePath.indexOf("?");
 | |
| 		let resource =
 | |
| 			querySplitPos === -1
 | |
| 				? maybeAbsolutePath
 | |
| 				: maybeAbsolutePath.slice(0, querySplitPos);
 | |
| 		resource = path.posix.relative(context, resource);
 | |
| 		if (!resource.startsWith("../")) {
 | |
| 			resource = "./" + resource;
 | |
| 		}
 | |
| 		return querySplitPos === -1
 | |
| 			? resource
 | |
| 			: resource + maybeAbsolutePath.slice(querySplitPos);
 | |
| 	}
 | |
| 
 | |
| 	if (WINDOWS_ABS_PATH_REGEXP.test(maybeAbsolutePath)) {
 | |
| 		const querySplitPos = maybeAbsolutePath.indexOf("?");
 | |
| 		let resource =
 | |
| 			querySplitPos === -1
 | |
| 				? maybeAbsolutePath
 | |
| 				: maybeAbsolutePath.slice(0, querySplitPos);
 | |
| 		resource = path.win32.relative(context, resource);
 | |
| 		if (!WINDOWS_ABS_PATH_REGEXP.test(resource)) {
 | |
| 			resource = resource.replace(WINDOWS_PATH_SEPARATOR_REGEXP, "/");
 | |
| 			if (!resource.startsWith("../")) {
 | |
| 				resource = "./" + resource;
 | |
| 			}
 | |
| 		}
 | |
| 		return querySplitPos === -1
 | |
| 			? resource
 | |
| 			: resource + maybeAbsolutePath.slice(querySplitPos);
 | |
| 	}
 | |
| 
 | |
| 	// 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(SEGEMENTS_SPLIT_REGEXP)
 | |
| 		.map(str => absoluteToRequest(context, str))
 | |
| 		.join("");
 | |
| };
 | |
| 
 | |
| 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);
 | |
| 	}
 | |
| 
 | |
| 	let cachedResult;
 | |
| 	let contextCache = relativePaths.get(context);
 | |
| 	if (contextCache === undefined) {
 | |
| 		relativePaths.set(context, (contextCache = new Map()));
 | |
| 	} else {
 | |
| 		cachedResult = contextCache.get(identifier);
 | |
| 	}
 | |
| 
 | |
| 	if (cachedResult !== undefined) {
 | |
| 		return cachedResult;
 | |
| 	} else {
 | |
| 		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;
 |