Skip to content

Commit 1ffe189

Browse files
committed
fix(material/schematics): gracefully skip broken css files
also don't treat files like something.css.map as a CSS file.
1 parent fd11f2b commit 1ffe189

File tree

8 files changed

+33
-6
lines changed

8 files changed

+33
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Does not compile (missing semicolons) */
2+
.mat-button {
3+
width: 100
4+
height: 100
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Should not be migrated (filename is not .css or .scss) */
2+
.mat-button {
3+
width: 100px;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Does not compile (missing semicolons) */
2+
.mat-button {
3+
width: 100
4+
height: 100
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Should not be migrated (filename is not .css or .scss) */
2+
.mat-button {
3+
width: 100px;
4+
}

src/cdk/schematics/ng-update/find-stylesheets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {join, Path} from '@angular-devkit/core';
1010
import {Tree} from '@angular-devkit/schematics';
1111

1212
/** Regular expression that matches stylesheet paths */
13-
const STYLESHEET_REGEX = /.*\.(css|scss)/;
13+
const STYLESHEET_REGEX = /.*\.(css|scss)$/;
1414

1515
/**
1616
* Finds stylesheets in the given directory from within the specified tree.

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
2222
this.fileSystem
2323
.edit(stylesheet.filePath)
2424
.remove(stylesheet.start, stylesheet.content.length)
25-
.insertRight(stylesheet.start, this.migrate(stylesheet.content));
25+
.insertRight(stylesheet.start, this.migrate(stylesheet.content, stylesheet.filePath));
2626
}
2727

28-
migrate(styles: string): string {
28+
migrate(styles: string, filename: string): string {
2929
const processor = new postcss.Processor([
3030
{
3131
postcssPlugin: 'theming-styles-migration-plugin',
@@ -37,7 +37,12 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
3737
},
3838
]);
3939

40-
return processor.process(styles, {syntax: scss}).toString();
40+
try {
41+
return processor.process(styles, {syntax: scss}).toString();
42+
} catch (e) {
43+
console.warn(`Failed to process stylesheet: `, filename);
44+
return styles;
45+
}
4146
}
4247

4348
atUseHandler(atRule: postcss.AtRule) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
134134
node: ts.StringLiteralLike | ts.Identifier,
135135
migration: TemplateMigration | ThemingStylesMigration,
136136
) {
137-
let migratedText = migration.migrate(node.text);
137+
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName);
138138
let migratedTextLines = migratedText.split('\n');
139139

140140
// 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export class LegacyComponentsMigration extends Migration<null> {
3737
RootExit: root => this._handleRootNode(root, stylesheet.filePath, namespace),
3838
},
3939
]);
40-
processor.process(stylesheet.content, {syntax: scss}).sync();
40+
try {
41+
processor.process(stylesheet.content, {syntax: scss}).sync();
42+
} catch (e) {
43+
console.warn(`Failed to process stylesheet: `, stylesheet.filePath);
44+
}
4145
}
4246

4347
/** Returns the namespace of the given at-rule if it is importing from @angular/material. */

0 commit comments

Comments
 (0)