Skip to content

fix(material/schematics): gracefully skip broken css files #25767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions integration/mdc-migration/golden/src/broken.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Does not compile (missing semicolons) */
.mat-button {
width: 100
height: 100
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Should not be migrated (filename is not .css or .scss) */
.mat-button {
width: 100px;
}
5 changes: 5 additions & 0 deletions integration/mdc-migration/sample-project/src/broken.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Does not compile (missing semicolons) */
.mat-button {
width: 100
height: 100
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Should not be migrated (filename is not .css or .scss) */
.mat-button {
width: 100px;
}
2 changes: 1 addition & 1 deletion src/cdk/schematics/ng-update/find-stylesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {join, Path} from '@angular-devkit/core';
import {Tree} from '@angular-devkit/schematics';

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

/**
* Finds stylesheets in the given directory from within the specified tree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ jasmine_node_test(
"//src/material/schematics:collection_assets",
"//src/material/schematics:ng_generate_assets",
],
shard_count = 10,
deps = [
":unit_tests_bundle",
# Runtime dependencies needed by the test and actual migration sources. These need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
this.fileSystem
.edit(stylesheet.filePath)
.remove(stylesheet.start, stylesheet.content.length)
.insertRight(stylesheet.start, this.migrate(stylesheet.content));
.insertRight(stylesheet.start, this.migrate(stylesheet.content, stylesheet.filePath));
}

migrate(styles: string): string {
migrate(styles: string, filename: string): string {
const processor = new postcss.Processor([
{
postcssPlugin: 'theming-styles-migration-plugin',
Expand All @@ -37,7 +37,13 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
},
]);

return processor.process(styles, {syntax: scss}).toString();
try {
return processor.process(styles, {syntax: scss}).toString();
} catch (e) {
this.context.logger.error(`${e}`);
this.context.logger.warn(`Failed to process stylesheet: ${filename} (see error above).`);
return styles;
}
}

atUseHandler(atRule: postcss.AtRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
node: ts.StringLiteralLike | ts.Identifier,
migration: TemplateMigration | ThemingStylesMigration,
) {
let migratedText = migration.migrate(node.text);
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName);
let migratedTextLines = migratedText.split('\n');

// Update quotes based on if its multiline or not to avoid compilation errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export class LegacyComponentsMigration extends Migration<null> {
RootExit: root => this._handleRootNode(root, stylesheet.filePath, namespace),
},
]);
processor.process(stylesheet.content, {syntax: scss}).sync();
try {
processor.process(stylesheet.content, {syntax: scss}).sync();
} catch (e) {
this.logger.error(`${e}`);
this.logger.warn(`Failed to process stylesheet: ${stylesheet.filePath} (see error above).`);
}
}

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