Skip to content

Commit a2d70df

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/core): use picomatch for PatternMatchingHost glob support
The glob support in the `PatternMatchingHost` class now uses the capabilities of the `picomatch` package to convert glob strings into regular expressions. This removes custom string replacement code that previously was used. The `picomatch` package is already used by `@angular-devkit/build-angular` and is present in the repository but is a new dependency for the `@angular-devkit/core` package specifically. (cherry picked from commit f9372ac)
1 parent fdb16f7 commit a2d70df

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

packages/angular_devkit/core/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ ts_library(
3434
module_root = "src/index.d.ts",
3535
deps = [
3636
"@npm//@types/node",
37+
"@npm//@types/picomatch",
3738
"@npm//ajv",
3839
"@npm//ajv-formats",
3940
"@npm//jsonc-parser",
41+
"@npm//picomatch",
4042
"@npm//rxjs",
4143
"@npm//source-map",
4244
# @node_module: typescript:es2015.proxy

packages/angular_devkit/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ajv-formats": "2.1.1",
2929
"ajv": "8.12.0",
3030
"jsonc-parser": "3.2.0",
31+
"picomatch": "2.3.1",
3132
"rxjs": "7.8.1",
3233
"source-map": "0.7.4"
3334
},

packages/angular_devkit/core/src/virtual-fs/host/pattern.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { parse as parseGlob } from 'picomatch';
910
import { Path } from '../path';
1011
import { ResolverHost } from './resolver';
1112

@@ -17,28 +18,11 @@ export class PatternMatchingHost<StatsT extends object = {}> extends ResolverHos
1718
protected _patterns = new Map<RegExp, ReplacementFunction>();
1819

1920
addPattern(pattern: string | string[], replacementFn: ReplacementFunction) {
20-
// Simple GLOB pattern replacement.
21-
const reString =
22-
'^(' +
23-
(Array.isArray(pattern) ? pattern : [pattern])
24-
.map(
25-
(ex) =>
26-
'(' +
27-
ex
28-
.split(/[/\\]/g)
29-
.map((f) =>
30-
f
31-
.replace(/[-[\]{}()+?.^$|]/g, '\\$&')
32-
.replace(/^\*\*/g, '(.+?)?')
33-
.replace(/\*/g, '[^/\\\\]*'),
34-
)
35-
.join('[/\\\\]') +
36-
')',
37-
)
38-
.join('|') +
39-
')($|/|\\\\)';
40-
41-
this._patterns.set(new RegExp(reString), replacementFn);
21+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
22+
for (const glob of patterns) {
23+
const { output } = parseGlob(glob);
24+
this._patterns.set(new RegExp(`^${output}$`), replacementFn);
25+
}
4226
}
4327

4428
protected _resolve(path: Path) {

0 commit comments

Comments
 (0)