source: trip-planner-front/node_modules/@ngtools/webpack/src/paths-plugin.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 6.7 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.TypeScriptPathsPlugin = void 0;
30const path = __importStar(require("path"));
31class TypeScriptPathsPlugin {
32 constructor(options) {
33 this.options = options;
34 }
35 update(options) {
36 this.options = options;
37 }
38 apply(resolver) {
39 const target = resolver.ensureHook('resolve');
40 resolver.getHook('described-resolve').tapAsync('TypeScriptPathsPlugin',
41 // eslint-disable-next-line @typescript-eslint/no-explicit-any
42 (request, resolveContext, callback) => {
43 if (!this.options) {
44 callback();
45 return;
46 }
47 if (!request || request.typescriptPathMapped) {
48 callback();
49 return;
50 }
51 const originalRequest = request.request || request.path;
52 if (!originalRequest) {
53 callback();
54 return;
55 }
56 // Only work on Javascript/TypeScript issuers.
57 if (!request.context.issuer || !request.context.issuer.match(/\.[jt]sx?$/)) {
58 callback();
59 return;
60 }
61 // Relative or absolute requests are not mapped
62 if (originalRequest.startsWith('.') || originalRequest.startsWith('/')) {
63 callback();
64 return;
65 }
66 // Ignore all webpack special requests
67 if (originalRequest.startsWith('!!')) {
68 callback();
69 return;
70 }
71 const replacements = findReplacements(originalRequest, this.options.paths || {});
72 const tryResolve = () => {
73 var _a;
74 const potential = replacements.shift();
75 if (!potential) {
76 callback();
77 return;
78 }
79 const potentialRequest = {
80 ...request,
81 request: path.resolve(((_a = this.options) === null || _a === void 0 ? void 0 : _a.baseUrl) || '', potential),
82 typescriptPathMapped: true,
83 };
84 resolver.doResolve(target, potentialRequest, '', resolveContext,
85 // eslint-disable-next-line @typescript-eslint/no-explicit-any
86 (error, result) => {
87 if (error) {
88 callback(error);
89 }
90 else if (result) {
91 callback(undefined, result);
92 }
93 else {
94 tryResolve();
95 }
96 });
97 };
98 tryResolve();
99 });
100 }
101}
102exports.TypeScriptPathsPlugin = TypeScriptPathsPlugin;
103function findReplacements(originalRequest, paths) {
104 // check if any path mapping rules are relevant
105 const pathMapOptions = [];
106 for (const pattern in paths) {
107 // get potentials and remove duplicates; JS Set maintains insertion order
108 const potentials = Array.from(new Set(paths[pattern]));
109 if (potentials.length === 0) {
110 // no potential replacements so skip
111 continue;
112 }
113 // can only contain zero or one
114 const starIndex = pattern.indexOf('*');
115 if (starIndex === -1) {
116 if (pattern === originalRequest) {
117 pathMapOptions.push({
118 starIndex,
119 partial: '',
120 potentials,
121 });
122 }
123 }
124 else if (starIndex === 0 && pattern.length === 1) {
125 if (potentials.length === 1 && potentials[0] === '*') {
126 // identity mapping -> noop
127 continue;
128 }
129 pathMapOptions.push({
130 starIndex,
131 partial: originalRequest,
132 potentials,
133 });
134 }
135 else if (starIndex === pattern.length - 1) {
136 if (originalRequest.startsWith(pattern.slice(0, -1))) {
137 pathMapOptions.push({
138 starIndex,
139 partial: originalRequest.slice(pattern.length - 1),
140 potentials,
141 });
142 }
143 }
144 else {
145 const [prefix, suffix] = pattern.split('*');
146 if (originalRequest.startsWith(prefix) && originalRequest.endsWith(suffix)) {
147 pathMapOptions.push({
148 starIndex,
149 partial: originalRequest.slice(prefix.length).slice(0, -suffix.length),
150 potentials,
151 });
152 }
153 }
154 }
155 if (pathMapOptions.length === 0) {
156 return [];
157 }
158 // exact matches take priority then largest prefix match
159 pathMapOptions.sort((a, b) => {
160 if (a.starIndex === -1) {
161 return -1;
162 }
163 else if (b.starIndex === -1) {
164 return 1;
165 }
166 else {
167 return b.starIndex - a.starIndex;
168 }
169 });
170 const replacements = [];
171 pathMapOptions.forEach((option) => {
172 for (const potential of option.potentials) {
173 let replacement;
174 const starIndex = potential.indexOf('*');
175 if (starIndex === -1) {
176 replacement = potential;
177 }
178 else if (starIndex === potential.length - 1) {
179 replacement = potential.slice(0, -1) + option.partial;
180 }
181 else {
182 const [prefix, suffix] = potential.split('*');
183 replacement = prefix + option.partial + suffix;
184 }
185 replacements.push(replacement);
186 }
187 });
188 return replacements;
189}
Note: See TracBrowser for help on using the repository browser.