Skip to content

Commit

Permalink
fix(material/schematics): don't migrate unknown stylesheet formats (#…
Browse files Browse the repository at this point in the history
…26450)

Fixes that we were trying to parse stylesheets that we don't support (e.g. `.sass`).

Fixes #26396.

(cherry picked from commit 3038d72)
  • Loading branch information
crisbeto committed Jan 18, 2023
1 parent de53216 commit 0fc52fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

override visitStylesheet(stylesheet: ResolvedResource) {
let migratedContent = this.migrate(stylesheet.content, stylesheet.filePath);
let migratedContent = this.migrate(stylesheet.content, stylesheet.filePath, stylesheet.inline);

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

this.fileSystem
.edit(stylesheet.filePath)
.remove(stylesheet.start, stylesheet.content.length)
.insertRight(stylesheet.start, migratedContent);
if (migratedContent !== stylesheet.content) {
this.fileSystem
.edit(stylesheet.filePath)
.remove(stylesheet.start, stylesheet.content.length)
.insertRight(stylesheet.start, migratedContent);
}
}

migrate(styles: string, filename: string): string {
migrate(styles: string, filename: string, isInline: boolean): string {
const extension = extname(filename).toLowerCase();

if (!isInline && extension && extension !== '.css' && extension !== '.scss') {
return styles;
}

const processor = new postcss.Processor([
{
postcssPlugin: 'theming-styles-migration-plugin',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
}

private _migratePropertyAssignment(
node: ts.StringLiteralLike | ts.Identifier,
node: ts.StringLiteralLike,
migration: TemplateMigration | ThemingStylesMigration,
) {
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName);
let migratedText = migration.migrate(node.text, node.getSourceFile().fileName, true);
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 @@ -19,11 +19,18 @@ import {
MIGRATED_CORE_SYMBOLS,
} from './constants';
import {Migration, ResolvedResource, TargetVersion, WorkspacePath} from '@angular/cdk/schematics';
import {extname} from 'path';

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

override visitStylesheet(stylesheet: ResolvedResource): void {
const extension = extname(stylesheet.filePath).toLowerCase();

if (!stylesheet.inline && extension && extension !== '.css' && extension !== '.scss') {
return;
}

let namespace: string | undefined = undefined;
const processor = new postcss.Processor([
{
Expand Down

0 comments on commit 0fc52fc

Please sign in to comment.