Skip to content

Commit

Permalink
fix(material/schematics): avoid parsing stylesheets that don't includ…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
crisbeto committed Dec 2, 2024
1 parent c891926 commit 4ef3baa
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
WorkspacePath,
} from '@angular/cdk/schematics';

const MATERIAL_IMPORT_PATH = '@angular/material';

export class MatCoreMigration extends Migration<UpgradeData, DevkitContext> {
override enabled = true;
private _namespace: string | undefined;
Expand All @@ -25,6 +27,11 @@ export class MatCoreMigration extends Migration<UpgradeData, DevkitContext> {
}

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([
{
Expand Down Expand Up @@ -77,7 +84,7 @@ export class MatCoreMigration extends Migration<UpgradeData, DevkitContext> {

/** 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';
}
}
Expand Down

0 comments on commit 4ef3baa

Please sign in to comment.