UNPKG

1.44 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.PatternMatchingHost = void 0;
11const resolver_1 = require("./resolver");
12/**
13 */
14class PatternMatchingHost extends resolver_1.ResolverHost {
15 constructor() {
16 super(...arguments);
17 this._patterns = new Map();
18 }
19 addPattern(pattern, replacementFn) {
20 // Simple GLOB pattern replacement.
21 const reString = '^(' +
22 (Array.isArray(pattern) ? pattern : [pattern])
23 .map((ex) => '(' +
24 ex
25 .split(/[/\\]/g)
26 .map((f) => f
27 .replace(/[-[\]{}()+?.^$|]/g, '\\$&')
28 .replace(/^\*\*/g, '(.+?)?')
29 .replace(/\*/g, '[^/\\\\]*'))
30 .join('[/\\\\]') +
31 ')')
32 .join('|') +
33 ')($|/|\\\\)';
34 this._patterns.set(new RegExp(reString), replacementFn);
35 }
36 _resolve(path) {
37 let newPath = path;
38 this._patterns.forEach((fn, re) => {
39 if (re.test(path)) {
40 newPath = fn(newPath);
41 }
42 });
43 return newPath;
44 }
45}
46exports.PatternMatchingHost = PatternMatchingHost;