source: frontend/node_modules/enhanced-resolve/lib/ModulesUtils.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: 4.5 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
8const forEachBail = require("./forEachBail");
9const { getPathsCached } = require("./getPaths");
10
11/** @typedef {import("./Resolver")} Resolver */
12/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
13/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
14/** @typedef {import("./Resolver").ResolveContext} ResolveContext */
15/** @typedef {(err?: null | Error, result?: null | ResolveRequest) => void} InnerCallback */
16
17/**
18 * Per-(directories-array) cache of the flat `addrs` list produced for a given
19 * `request.path`. For a fixed directories configuration the fan-out of
20 * `ancestor × directory` is deterministic per request.path, and many resolves
21 * share the same starting directory (sibling files in one project, loops over
22 * a batch of imports, etc.) — caching avoids the `getPaths` regex split plus
23 * `len(paths) × len(directories)` join calls per resolve.
24 *
25 * The outer map is keyed on the directories array reference (plugin-owned,
26 * stable for the lifetime of the resolver), and the inner map on the
27 * starting `request.path`. Kept private to this module (rather than hung off
28 * `resolver.pathCache`) so the pathCache's hidden-class shape is unchanged —
29 * that avoids perturbing the interpreter-mode IC state for the
30 * `resolver.pathCache.{join,dirname,basename}.fn(...)` accesses that run on
31 * every resolve, which the CodSpeed instruction-count harness is sensitive to.
32 * @type {WeakMap<string[], Map<string, string[]>>}
33 */
34const _addrsCacheByDirs = new WeakMap();
35
36/**
37 * @param {Resolver} resolver resolver
38 * @param {string[]} directories directories
39 * @param {ResolveStepHook} target target
40 * @param {ResolveRequest} request request
41 * @param {ResolveContext} resolveContext resolve context
42 * @param {InnerCallback} callback callback
43 * @returns {void}
44 */
45function modulesResolveHandler(
46 resolver,
47 directories,
48 target,
49 request,
50 resolveContext,
51 callback,
52) {
53 const fs = resolver.fileSystem;
54 const requestPath = /** @type {string} */ (request.path);
55 // Compute-or-reuse the flat `addrs` list. Inlined (rather than a helper
56 // function) so the cache-hit path — which is the vast majority of
57 // invocations — stays a single WeakMap + Map lookup with no function-call
58 // overhead. See `_addrsCacheByDirs` above for caching rationale.
59 let addrs;
60 let perPath = _addrsCacheByDirs.get(directories);
61 if (perPath === undefined) {
62 perPath = new Map();
63 _addrsCacheByDirs.set(directories, perPath);
64 } else {
65 addrs = perPath.get(requestPath);
66 }
67 if (addrs === undefined) {
68 const { paths } = getPathsCached(fs, requestPath);
69 const pathsLen = paths.length;
70 const dirsLen = directories.length;
71 // Pre-size the flat array rather than going through `map().reduce()`
72 // with intermediate arrays + spreads.
73 // eslint-disable-next-line unicorn/no-new-array
74 addrs = new Array(pathsLen * dirsLen);
75 let idx = 0;
76 const joinFn = resolver.pathCache.join.fn;
77 for (let pi = 0; pi < pathsLen; pi++) {
78 const pathItem = paths[pi];
79 for (let di = 0; di < dirsLen; di++) {
80 addrs[idx++] = joinFn(pathItem, directories[di]);
81 }
82 }
83 perPath.set(requestPath, addrs);
84 }
85 // Hoist the dot-prefixed request out of the per-addr iterator. `addrs`
86 // can have up to `paths.length × directories.length` entries (e.g. 36
87 // for an 8-deep source dir × 4-module config), and concatenating the
88 // same `./${request.request}` string on every iteration is wasted
89 // work — it's constant for the whole fan-out.
90 const relRequest = `./${request.request}`;
91 forEachBail(
92 addrs,
93 /**
94 * @param {string} addr addr
95 * @param {(err?: null | Error, result?: null | ResolveRequest) => void} callback callback
96 * @returns {void}
97 */
98 (addr, callback) => {
99 fs.stat(addr, (err, stat) => {
100 if (!err && stat && stat.isDirectory()) {
101 /** @type {ResolveRequest} */
102 const obj = {
103 ...request,
104 path: addr,
105 request: relRequest,
106 module: false,
107 };
108 const message = `looking for modules in ${addr}`;
109 return resolver.doResolve(
110 target,
111 obj,
112 message,
113 resolveContext,
114 callback,
115 );
116 }
117 if (resolveContext.log) {
118 resolveContext.log(`${addr} doesn't exist or is not a directory`);
119 }
120 if (resolveContext.missingDependencies) {
121 resolveContext.missingDependencies.add(addr);
122 }
123 return callback();
124 });
125 },
126 callback,
127 );
128}
129
130module.exports = {
131 modulesResolveHandler,
132};
Note: See TracBrowser for help on using the repository browser.