Skip to content

Commit 2409e70

Browse files
authored
fix(cdk/schematics): account for single string in styles and new styleUrl (#27798)
Updates the `ComponentResourceCollector` to account for the new changes in v17 where `@Component.styles` can be a single string and there's a new `@Component.styleUrl` property.
1 parent 48a988a commit 2409e70

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/cdk/schematics/update-tool/component-resource-collector.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export class ComponentResourceCollector {
4545
resolvedTemplates: ResolvedResource[] = [];
4646
resolvedStylesheets: ResolvedResource[] = [];
4747

48-
constructor(public typeChecker: ts.TypeChecker, private _fileSystem: FileSystem) {}
48+
constructor(
49+
public typeChecker: ts.TypeChecker,
50+
private _fileSystem: FileSystem,
51+
) {}
4952

5053
visitNode(node: ts.Node) {
5154
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
@@ -95,8 +98,12 @@ export class ComponentResourceCollector {
9598

9699
const propertyName = getPropertyNameText(property.name);
97100

98-
if (propertyName === 'styles' && ts.isArrayLiteralExpression(property.initializer)) {
99-
property.initializer.elements.forEach(el => {
101+
if (propertyName === 'styles') {
102+
const elements = ts.isArrayLiteralExpression(property.initializer)
103+
? property.initializer.elements
104+
: [property.initializer];
105+
106+
elements.forEach(el => {
100107
if (ts.isStringLiteralLike(el)) {
101108
// Need to add an offset of one to the start because the template quotes are
102109
// not part of the template content.
@@ -135,16 +142,15 @@ export class ComponentResourceCollector {
135142
if (propertyName === 'styleUrls' && ts.isArrayLiteralExpression(property.initializer)) {
136143
property.initializer.elements.forEach(el => {
137144
if (ts.isStringLiteralLike(el)) {
138-
const stylesheetPath = this._fileSystem.resolve(sourceFileDirPath, el.text);
139-
const stylesheet = this.resolveExternalStylesheet(stylesheetPath, node);
140-
141-
if (stylesheet) {
142-
this.resolvedStylesheets.push(stylesheet);
143-
}
145+
this._trackExternalStylesheet(sourceFileDirPath, el, node);
144146
}
145147
});
146148
}
147149

150+
if (propertyName === 'styleUrl' && ts.isStringLiteralLike(property.initializer)) {
151+
this._trackExternalStylesheet(sourceFileDirPath, property.initializer, node);
152+
}
153+
148154
if (propertyName === 'templateUrl' && ts.isStringLiteralLike(property.initializer)) {
149155
const templateUrl = property.initializer.text;
150156
const templatePath = this._fileSystem.resolve(sourceFileDirPath, templateUrl);
@@ -197,6 +203,19 @@ export class ComponentResourceCollector {
197203
getCharacterAndLineOfPosition: pos => getLineAndCharacterFromPosition(lineStartsMap, pos),
198204
};
199205
}
206+
207+
private _trackExternalStylesheet(
208+
sourceFileDirPath: string,
209+
node: ts.StringLiteralLike,
210+
container: ts.ClassDeclaration,
211+
) {
212+
const stylesheetPath = this._fileSystem.resolve(sourceFileDirPath, node.text);
213+
const stylesheet = this.resolveExternalStylesheet(stylesheetPath, container);
214+
215+
if (stylesheet) {
216+
this.resolvedStylesheets.push(stylesheet);
217+
}
218+
}
200219
}
201220

202221
/** Strips the BOM from a string. */

0 commit comments

Comments
 (0)