source: imaps-frontend/node_modules/enhanced-resolve/lib/AliasPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.1 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 { PathType, getType } = require("./util/path");
10
11/** @typedef {import("./Resolver")} Resolver */
12/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
13/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
14/** @typedef {string | Array<string> | false} Alias */
15/** @typedef {{alias: Alias, name: string, onlyModule?: boolean}} AliasOption */
16
17module.exports = class AliasPlugin {
18 /**
19 * @param {string | ResolveStepHook} source source
20 * @param {AliasOption | Array<AliasOption>} options options
21 * @param {string | ResolveStepHook} target target
22 */
23 constructor(source, options, target) {
24 this.source = source;
25 this.options = Array.isArray(options) ? options : [options];
26 this.target = target;
27 }
28
29 /**
30 * @param {Resolver} resolver the resolver
31 * @returns {void}
32 */
33 apply(resolver) {
34 const target = resolver.ensureHook(this.target);
35 /**
36 * @param {string} maybeAbsolutePath path
37 * @returns {null|string} absolute path with slash ending
38 */
39 const getAbsolutePathWithSlashEnding = maybeAbsolutePath => {
40 const type = getType(maybeAbsolutePath);
41 if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
42 return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
43 }
44 return null;
45 };
46 /**
47 * @param {string} path path
48 * @param {string} maybeSubPath sub path
49 * @returns {boolean} true, if path is sub path
50 */
51 const isSubPath = (path, maybeSubPath) => {
52 const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
53 if (!absolutePath) return false;
54 return path.startsWith(absolutePath);
55 };
56 resolver
57 .getHook(this.source)
58 .tapAsync("AliasPlugin", (request, resolveContext, callback) => {
59 const innerRequest = request.request || request.path;
60 if (!innerRequest) return callback();
61
62 forEachBail(
63 this.options,
64 (item, callback) => {
65 /** @type {boolean} */
66 let shouldStop = false;
67
68 const matchRequest =
69 innerRequest === item.name ||
70 (!item.onlyModule &&
71 (request.request
72 ? innerRequest.startsWith(`${item.name}/`)
73 : isSubPath(innerRequest, item.name)));
74
75 const splitName = item.name.split("*");
76 const matchWildcard = !item.onlyModule && splitName.length === 2;
77
78 if (matchRequest || matchWildcard) {
79 /**
80 * @param {Alias} alias alias
81 * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
82 * @returns {void}
83 */
84 const resolveWithAlias = (alias, callback) => {
85 if (alias === false) {
86 /** @type {ResolveRequest} */
87 const ignoreObj = {
88 ...request,
89 path: false
90 };
91 if (typeof resolveContext.yield === "function") {
92 resolveContext.yield(ignoreObj);
93 return callback(null, null);
94 }
95 return callback(null, ignoreObj);
96 }
97
98 let newRequestStr;
99
100 const [prefix, suffix] = splitName;
101 if (
102 matchWildcard &&
103 innerRequest.startsWith(prefix) &&
104 innerRequest.endsWith(suffix)
105 ) {
106 const match = innerRequest.slice(
107 prefix.length,
108 innerRequest.length - suffix.length
109 );
110 newRequestStr = item.alias.toString().replace("*", match);
111 }
112
113 if (
114 matchRequest &&
115 innerRequest !== alias &&
116 !innerRequest.startsWith(alias + "/")
117 ) {
118 /** @type {string} */
119 const remainingRequest = innerRequest.slice(item.name.length);
120 newRequestStr = alias + remainingRequest;
121 }
122
123 if (newRequestStr !== undefined) {
124 shouldStop = true;
125 /** @type {ResolveRequest} */
126 const obj = {
127 ...request,
128 request: newRequestStr,
129 fullySpecified: false
130 };
131 return resolver.doResolve(
132 target,
133 obj,
134 "aliased with mapping '" +
135 item.name +
136 "': '" +
137 alias +
138 "' to '" +
139 newRequestStr +
140 "'",
141 resolveContext,
142 (err, result) => {
143 if (err) return callback(err);
144 if (result) return callback(null, result);
145 return callback();
146 }
147 );
148 }
149 return callback();
150 };
151
152 /**
153 * @param {null|Error} [err] error
154 * @param {null|ResolveRequest} [result] result
155 * @returns {void}
156 */
157 const stoppingCallback = (err, result) => {
158 if (err) return callback(err);
159
160 if (result) return callback(null, result);
161 // Don't allow other aliasing or raw request
162 if (shouldStop) return callback(null, null);
163 return callback();
164 };
165
166 if (Array.isArray(item.alias)) {
167 return forEachBail(
168 item.alias,
169 resolveWithAlias,
170 stoppingCallback
171 );
172 } else {
173 return resolveWithAlias(item.alias, stoppingCallback);
174 }
175 }
176
177 return callback();
178 },
179 callback
180 );
181 });
182 }
183};
Note: See TracBrowser for help on using the repository browser.