Skip to content

Commit 7cd084d

Browse files
crisbetoandrewseguin
authored andcommitted
fix(material/schematics): don't drop prebuilt imports in theming API migration
Currently the theming API migration drops any imports starting with `~@angular/material/`, assuming that they're Sass APIs. This can result in prebuilt style imports being removed by mistake. These changes add a regex based on which we'll exclude some imports from the migration. Fixes #22697.
1 parent 8d97acf commit 7cd084d

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ interface ExtraSymbols {
4141
* matched, the prefix would be `~@angular/cdk/`.
4242
* @param newMaterialImportPath New import to the Material theming API (e.g. `~@angular/material`).
4343
* @param newCdkImportPath New import to the CDK Sass APIs (e.g. `~@angular/cdk`).
44+
* @param excludedImports Pattern that can be used to exclude imports from being processed.
4445
*/
4546
export function migrateFileContent(content: string,
4647
oldMaterialPrefix: string,
4748
oldCdkPrefix: string,
4849
newMaterialImportPath: string,
4950
newCdkImportPath: string,
50-
extraMaterialSymbols: ExtraSymbols = {}): string {
51-
const materialResults = detectImports(content, oldMaterialPrefix);
52-
const cdkResults = detectImports(content, oldCdkPrefix);
51+
extraMaterialSymbols: ExtraSymbols = {},
52+
excludedImports?: RegExp): string {
53+
const materialResults = detectImports(content, oldMaterialPrefix, excludedImports);
54+
const cdkResults = detectImports(content, oldCdkPrefix, excludedImports);
5355

5456
// Try to migrate the symbols even if there are no imports. This is used
5557
// to cover the case where the Components symbols were used transitively.
@@ -77,8 +79,10 @@ export function migrateFileContent(content: string,
7779
* Counts the number of imports with a specific prefix and extracts their namespaces.
7880
* @param content File content in which to look for imports.
7981
* @param prefix Prefix that the imports should start with.
82+
* @param excludedImports Pattern that can be used to exclude imports from being processed.
8083
*/
81-
function detectImports(content: string, prefix: string): DetectImportResult {
84+
function detectImports(content: string, prefix: string,
85+
excludedImports?: RegExp): DetectImportResult {
8286
if (prefix[prefix.length - 1] !== '/') {
8387
// Some of the logic further down makes assumptions about the import depth.
8488
throw Error(`Prefix "${prefix}" has to end in a slash.`);
@@ -95,6 +99,10 @@ function detectImports(content: string, prefix: string): DetectImportResult {
9599
while (match = pattern.exec(content)) {
96100
const [fullImport, type] = match;
97101

102+
if (excludedImports?.test(fullImport)) {
103+
continue;
104+
}
105+
98106
if (type === 'use') {
99107
const namespace = extractNamespaceFromUseStatement(fullImport);
100108

src/material/schematics/ng-update/migrations/theming-api-v12/theming-api-migration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export class ThemingApiMigration extends DevkitMigration<null> {
2222
if (extname(stylesheet.filePath) === '.scss') {
2323
const content = stylesheet.content;
2424
const migratedContent = content ? migrateFileContent(content,
25-
'~@angular/material/', '~@angular/cdk/', '~@angular/material', '~@angular/cdk') : content;
25+
'~@angular/material/', '~@angular/cdk/', '~@angular/material', '~@angular/cdk',
26+
undefined, /material\/prebuilt-themes|cdk\/.*-prebuilt/) : content;
2627

2728
if (migratedContent && migratedContent !== content) {
2829
this.fileSystem.edit(stylesheet.filePath)

src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,4 +716,22 @@ describe('v12 theming API migration', () => {
716716
`@include mat.mdc-button-theme();`,
717717
].join('\n'));
718718
});
719+
720+
it('should not drop imports of prebuilt styles', async () => {
721+
writeLines(THEME_PATH, [
722+
`@import '~@angular/material/prebuilt-themes/indigo-pink.css';`,
723+
`@import '~@angular/material/theming';`,
724+
`@import '~@angular/cdk/overlay-prebuilt.css';`,
725+
`@include mat-core();`,
726+
]);
727+
728+
await runMigration();
729+
730+
expect(splitFile(THEME_PATH)).toEqual([
731+
`@use '~@angular/material' as mat;`,
732+
`@import '~@angular/material/prebuilt-themes/indigo-pink.css';`,
733+
`@import '~@angular/cdk/overlay-prebuilt.css';`,
734+
`@include mat.core();`,
735+
]);
736+
});
719737
});

0 commit comments

Comments
 (0)