Skip to content

Commit 0fc52fc

Browse files
committed
fix(material/schematics): don't migrate unknown stylesheet formats (#26450)
Fixes that we were trying to parse stylesheets that we don't support (e.g. `.sass`). Fixes #26396. (cherry picked from commit 3038d72)
1 parent de53216 commit 0fc52fc

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/material/schematics/ng-generate/mdc-migration/rules/theming-styles.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {Migration, ResolvedResource} from '@angular/cdk/schematics';
1010
import {SchematicContext} from '@angular-devkit/schematics';
11+
import {extname} from 'path';
1112
import * as postcss from 'postcss';
1213
import * as scss from 'postcss-scss';
1314
import {ComponentMigrator, MIGRATORS, PERMANENT_MIGRATORS} from '.';
@@ -24,20 +25,28 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
2425
private _namespace: string;
2526

2627
override visitStylesheet(stylesheet: ResolvedResource) {
27-
let migratedContent = this.migrate(stylesheet.content, stylesheet.filePath);
28+
let migratedContent = this.migrate(stylesheet.content, stylesheet.filePath, stylesheet.inline);
2829

2930
// Note: needs to run after `migrate` so that the `namespace` has been resolved.
3031
if (this._namespace) {
3132
migratedContent = migrateTypographyConfigs(migratedContent, this._namespace);
3233
}
3334

34-
this.fileSystem
35-
.edit(stylesheet.filePath)
36-
.remove(stylesheet.start, stylesheet.content.length)
37-
.insertRight(stylesheet.start, migratedContent);
35+
if (migratedContent !== stylesheet.content) {
36+
this.fileSystem
37+
.edit(stylesheet.filePath)
38+
.remove(stylesheet.start, stylesheet.content.length)
39+
.insertRight(stylesheet.start, migratedContent);
40+
}
3841
}
3942

40-
migrate(styles: string, filename: string): string {
43+
migrate(styles: string, filename: string, isInline: boolean): string {
44+
const extension = extname(filename).toLowerCase();
45+
46+
if (!isInline && extension && extension !== '.css' && extension !== '.scss') {
47+
return styles;
48+
}
49+
4150
const processor = new postcss.Processor([
4251
{
4352
postcssPlugin: 'theming-styles-migration-plugin',

src/material/schematics/ng-generate/mdc-migration/rules/ts-migration/runtime-migration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
225225
}
226226

227227
private _migratePropertyAssignment(
228-
node: ts.StringLiteralLike | ts.Identifier,
228+
node: ts.StringLiteralLike,
229229
migration: TemplateMigration | ThemingStylesMigration,
230230
) {
231-
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName);
231+
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName, true);
232232
let migratedTextLines = migratedText.split('\n');
233233

234234
// Update quotes based on if its multiline or not to avoid compilation errors

src/material/schematics/ng-update/migrations/legacy-components-v15/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ import {
1919
MIGRATED_CORE_SYMBOLS,
2020
} from './constants';
2121
import {Migration, ResolvedResource, TargetVersion, WorkspacePath} from '@angular/cdk/schematics';
22+
import {extname} from 'path';
2223

2324
export class LegacyComponentsMigration extends Migration<null> {
2425
enabled = this.targetVersion === TargetVersion.V15;
2526

2627
override visitStylesheet(stylesheet: ResolvedResource): void {
28+
const extension = extname(stylesheet.filePath).toLowerCase();
29+
30+
if (!stylesheet.inline && extension && extension !== '.css' && extension !== '.scss') {
31+
return;
32+
}
33+
2734
let namespace: string | undefined = undefined;
2835
const processor = new postcss.Processor([
2936
{

0 commit comments

Comments
 (0)