source: frontend/node_modules/enhanced-resolve/lib/getPaths.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/** @typedef {import("./Resolver").FileSystem} FileSystem */
9/** @typedef {{ paths: string[], segments: string[] }} GetPathsResult */
10
11/**
12 * Walk `path` from tip to root, returning every ancestor directory (plus the
13 * input itself) in `paths`, and each corresponding segment name in `segments`.
14 *
15 * The return value may be shared across callers via `getPathsCached` — treat
16 * it as read-only. Callers that need to mutate (currently only
17 * `SymlinkPlugin`) should `slice()` the arrays locally before writing.
18 * @param {string} path path
19 * @returns {GetPathsResult} paths and segments
20 */
21function getPaths(path) {
22 if (path === "/") return { paths: ["/"], segments: [""] };
23 const parts = path.split(/(.*?[\\/]+)/);
24 const paths = [path];
25 const segments = [parts[parts.length - 1]];
26 let part = parts[parts.length - 1];
27 path = path.slice(0, Math.max(0, path.length - part.length - 1));
28 for (let i = parts.length - 2; i > 2; i -= 2) {
29 paths.push(path);
30 part = parts[i];
31 path = path.slice(0, Math.max(0, path.length - part.length)) || "/";
32 segments.push(part.slice(0, -1));
33 }
34 [, part] = parts;
35 segments.push(part);
36 paths.push(part);
37 return {
38 paths,
39 segments,
40 };
41}
42
43/**
44 * Per-filesystem memoization of `getPaths`. Kept in a standalone WeakMap
45 * rather than being hung off `resolver.pathCache` so that adding this cache
46 * does not change the hidden-class shape of `pathCache` — which is accessed
47 * on the hot path of every resolve as `resolver.pathCache.{join,dirname,
48 * basename}.fn(...)`. CodSpeed caught that shape change as a ~1–2%
49 * instruction-count regression on `cache-predicate`, so we keep pathCache
50 * shape-stable by owning this cache here instead.
51 *
52 * The cache lifetime is tied to the filesystem object (same invariant as
53 * `_pathCacheByFs` in `Resolver.js`): when the user swaps filesystems the
54 * entries become unreachable and get collected.
55 * @type {WeakMap<FileSystem, Map<string, GetPathsResult>>}
56 */
57const _getPathsCacheByFs = new WeakMap();
58
59/**
60 * Memoized `getPaths`. The returned object is shared across callers — do
61 * not mutate the `paths` or `segments` arrays in-place; `slice()` first if
62 * you need a mutable copy.
63 * @param {FileSystem} fileSystem filesystem used as the cache namespace
64 * @param {string} path path
65 * @returns {GetPathsResult} paths and segments
66 */
67function getPathsCached(fileSystem, path) {
68 let cache = _getPathsCacheByFs.get(fileSystem);
69 if (cache === undefined) {
70 cache = new Map();
71 _getPathsCacheByFs.set(fileSystem, cache);
72 } else {
73 const cached = cache.get(path);
74 if (cached !== undefined) return cached;
75 }
76 const result = getPaths(path);
77 cache.set(path, result);
78 return result;
79}
80
81module.exports = getPaths;
82module.exports.getPathsCached = getPathsCached;
Note: See TracBrowser for help on using the repository browser.