Skip to content

Commit

Permalink
refactor(@angular-devkit/core): use picomatch for PatternMatchingHost…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
clydin authored and alan-agius4 committed Aug 30, 2023
1 parent fdb16f7 commit a2d70df
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/angular_devkit/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ ts_library(
module_root = "src/index.d.ts",
deps = [
"@npm//@types/node",
"@npm//@types/picomatch",
"@npm//ajv",
"@npm//ajv-formats",
"@npm//jsonc-parser",
"@npm//picomatch",
"@npm//rxjs",
"@npm//source-map",
# @node_module: typescript:es2015.proxy
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ajv-formats": "2.1.1",
"ajv": "8.12.0",
"jsonc-parser": "3.2.0",
"picomatch": "2.3.1",
"rxjs": "7.8.1",
"source-map": "0.7.4"
},
Expand Down
28 changes: 6 additions & 22 deletions packages/angular_devkit/core/src/virtual-fs/host/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import { parse as parseGlob } from 'picomatch';
import { Path } from '../path';
import { ResolverHost } from './resolver';

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

addPattern(pattern: string | string[], replacementFn: ReplacementFunction) {
// Simple GLOB pattern replacement.
const reString =
'^(' +
(Array.isArray(pattern) ? pattern : [pattern])
.map(
(ex) =>
'(' +
ex
.split(/[/\\]/g)
.map((f) =>
f
.replace(/[-[\]{}()+?.^$|]/g, '\\$&')
.replace(/^\*\*/g, '(.+?)?')
.replace(/\*/g, '[^/\\\\]*'),
)
.join('[/\\\\]') +
')',
)
.join('|') +
')($|/|\\\\)';

this._patterns.set(new RegExp(reString), replacementFn);
const patterns = Array.isArray(pattern) ? pattern : [pattern];
for (const glob of patterns) {
const { output } = parseGlob(glob);
this._patterns.set(new RegExp(`^${output}$`), replacementFn);
}
}

protected _resolve(path: Path) {
Expand Down

0 comments on commit a2d70df

Please sign in to comment.