From c3955854469db7bc07dbfc511391bbcd00ae7666 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 2 Dec 2024 11:43:48 +0100 Subject: [PATCH] fix(material/schematics): avoid parsing stylesheets that don't include Material Adds a check in the `mat.core` migration so that it avoids parsing stylesheets that don't contain `@angular/material` altogether. This both makes the schematic faster and avoids potential issues for stylesheets we don't care about. (cherry picked from commit 4ef3baaf088420f5eef9961c9df75bd4f24c04d4) --- .../schematics/ng-update/migrations/mat-core-removal.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/material/schematics/ng-update/migrations/mat-core-removal.ts b/src/material/schematics/ng-update/migrations/mat-core-removal.ts index cf486a0a7430..64c0b84d2808 100644 --- a/src/material/schematics/ng-update/migrations/mat-core-removal.ts +++ b/src/material/schematics/ng-update/migrations/mat-core-removal.ts @@ -16,6 +16,8 @@ import { WorkspacePath, } from '@angular/cdk/schematics'; +const MATERIAL_IMPORT_PATH = '@angular/material'; + export class MatCoreMigration extends Migration { override enabled = true; private _namespace: string | undefined; @@ -25,6 +27,11 @@ export class MatCoreMigration extends Migration { } override visitStylesheet(stylesheet: ResolvedResource): void { + // Avoid parsing the template Material isn't used. + if (!stylesheet.content.includes(MATERIAL_IMPORT_PATH)) { + return; + } + try { const processor = new postcss.Processor([ { @@ -77,7 +84,7 @@ export class MatCoreMigration extends Migration { /** Sets the namespace if the given at-rule if it is importing from @angular/material. */ private _getNamespace(node: postcss.AtRule): void { - if (!this._namespace && node.params.startsWith('@angular/material', 1)) { + if (!this._namespace && node.params.startsWith(MATERIAL_IMPORT_PATH, 1)) { this._namespace = node.params.split(/\s+/)[2] || 'material'; } }