From 63ca33bc8642975ee7133c08041b5ceacccf6f3d Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 1 Aug 2022 14:51:47 +0200 Subject: [PATCH] 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 https://github.com/angular/components/issues/24227#issuecomment-1200934258). (cherry picked from commit 98ebc7088a301af61bed95ef3acfa40df8b6f84b) --- .../update-tool/component-resource-collector.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cdk/schematics/update-tool/component-resource-collector.ts b/src/cdk/schematics/update-tool/component-resource-collector.ts index e04785db1cd9..5fe2857405d1 100644 --- a/src/cdk/schematics/update-tool/component-resource-collector.ts +++ b/src/cdk/schematics/update-tool/component-resource-collector.ts @@ -99,10 +99,11 @@ export class ComponentResourceCollector { // Need to add an offset of one to the start because the template quotes are // not part of the template content. const templateStartIdx = el.getStart() + 1; + const content = stripBom(el.text); this.resolvedStylesheets.push({ filePath, container: node, - content: el.text, + content, inline: true, start: templateStartIdx, getCharacterAndLineOfPosition: pos => @@ -175,7 +176,9 @@ export class ComponentResourceCollector { filePath: WorkspacePath, container: ts.ClassDeclaration | null, ): ResolvedResource | null { - const fileContent = this._fileSystem.read(filePath); + // Strip the BOM to avoid issues with the Sass compiler. See: + // https://github.com/angular/components/issues/24227#issuecomment-1200934258 + const fileContent = stripBom(this._fileSystem.read(filePath) || ''); if (!fileContent) { return null; @@ -193,3 +196,8 @@ export class ComponentResourceCollector { }; } } + +/** Strips the BOM from a string. */ +function stripBom(content: string): string { + return content.replace(/\uFEFF/g, ''); +}