Skip to content

Commit 98ebc70

Browse files
authored
fix(cdk/schematics): strip bom from sass files (#25364)
We have some code to strip BOM from TS files when running a migration, but it doesn't apply to migrations of Sass files. These changes reuse the same code for Sass migrations, because the BOM can cause errors in the Sass compiler (see #24227 (comment)).
1 parent bf6f38a commit 98ebc70

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ export class ComponentResourceCollector {
9999
// Need to add an offset of one to the start because the template quotes are
100100
// not part of the template content.
101101
const templateStartIdx = el.getStart() + 1;
102+
const content = stripBom(el.text);
102103
this.resolvedStylesheets.push({
103104
filePath,
104105
container: node,
105-
content: el.text,
106+
content,
106107
inline: true,
107108
start: templateStartIdx,
108109
getCharacterAndLineOfPosition: pos =>
@@ -175,7 +176,9 @@ export class ComponentResourceCollector {
175176
filePath: WorkspacePath,
176177
container: ts.ClassDeclaration | null,
177178
): ResolvedResource | null {
178-
const fileContent = this._fileSystem.read(filePath);
179+
// Strip the BOM to avoid issues with the Sass compiler. See:
180+
// https://github.com/angular/components/issues/24227#issuecomment-1200934258
181+
const fileContent = stripBom(this._fileSystem.read(filePath) || '');
179182

180183
if (!fileContent) {
181184
return null;
@@ -193,3 +196,8 @@ export class ComponentResourceCollector {
193196
};
194197
}
195198
}
199+
200+
/** Strips the BOM from a string. */
201+
function stripBom(content: string): string {
202+
return content.replace(/\uFEFF/g, '');
203+
}

0 commit comments

Comments
 (0)