source: trip-planner-front/node_modules/enhanced-resolve/lib/AliasPlugin.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.9 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");
9
10/** @typedef {import("./Resolver")} Resolver */
11/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
12/** @typedef {{alias: string|Array<string>|false, name: string, onlyModule?: boolean}} AliasOption */
13
14module.exports = class AliasPlugin {
15 /**
16 * @param {string | ResolveStepHook} source source
17 * @param {AliasOption | Array<AliasOption>} options options
18 * @param {string | ResolveStepHook} target target
19 */
20 constructor(source, options, target) {
21 this.source = source;
22 this.options = Array.isArray(options) ? options : [options];
23 this.target = target;
24 }
25
26 /**
27 * @param {Resolver} resolver the resolver
28 * @returns {void}
29 */
30 apply(resolver) {
31 const target = resolver.ensureHook(this.target);
32 resolver
33 .getHook(this.source)
34 .tapAsync("AliasPlugin", (request, resolveContext, callback) => {
35 const innerRequest = request.request || request.path;
36 if (!innerRequest) return callback();
37 forEachBail(
38 this.options,
39 (item, callback) => {
40 let shouldStop = false;
41 if (
42 innerRequest === item.name ||
43 (!item.onlyModule && innerRequest.startsWith(item.name + "/"))
44 ) {
45 const remainingRequest = innerRequest.substr(item.name.length);
46 const resolveWithAlias = (alias, callback) => {
47 if (alias === false) {
48 const ignoreObj = {
49 ...request,
50 path: false
51 };
52 return callback(null, ignoreObj);
53 }
54 if (
55 innerRequest !== alias &&
56 !innerRequest.startsWith(alias + "/")
57 ) {
58 shouldStop = true;
59 const newRequestStr = alias + remainingRequest;
60 const obj = {
61 ...request,
62 request: newRequestStr,
63 fullySpecified: false
64 };
65 return resolver.doResolve(
66 target,
67 obj,
68 "aliased with mapping '" +
69 item.name +
70 "': '" +
71 alias +
72 "' to '" +
73 newRequestStr +
74 "'",
75 resolveContext,
76 (err, result) => {
77 if (err) return callback(err);
78 if (result) return callback(null, result);
79 return callback();
80 }
81 );
82 }
83 return callback();
84 };
85 const stoppingCallback = (err, result) => {
86 if (err) return callback(err);
87
88 if (result) return callback(null, result);
89 // Don't allow other aliasing or raw request
90 if (shouldStop) return callback(null, null);
91 return callback();
92 };
93 if (Array.isArray(item.alias)) {
94 return forEachBail(
95 item.alias,
96 resolveWithAlias,
97 stoppingCallback
98 );
99 } else {
100 return resolveWithAlias(item.alias, stoppingCallback);
101 }
102 }
103 return callback();
104 },
105 callback
106 );
107 });
108 }
109};
Note: See TracBrowser for help on using the repository browser.