From 6aac86abaef8d00c46123ae4541bf4a0b28b1631 Mon Sep 17 00:00:00 2001 From: Rik Crompton Date: Fri, 1 Mar 2024 15:58:34 +0000 Subject: [PATCH] fix: fixed an issue whereby files encoded with UTF8-BOM would cause imports to be ignored #121 --- __tests__/__fixtures__/import-utf8-bom.scss | 1 + __tests__/__fixtures__/two.scss | 3 ++ __tests__/__fixtures__/utf8-bom.scss | 1 + __tests__/utf8bom.test.js | 39 +++++++++++++++++++++ src/parse.ts | 5 ++- 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 __tests__/__fixtures__/import-utf8-bom.scss create mode 100644 __tests__/__fixtures__/two.scss create mode 100644 __tests__/__fixtures__/utf8-bom.scss create mode 100644 __tests__/utf8bom.test.js diff --git a/__tests__/__fixtures__/import-utf8-bom.scss b/__tests__/__fixtures__/import-utf8-bom.scss new file mode 100644 index 0000000..09e311e --- /dev/null +++ b/__tests__/__fixtures__/import-utf8-bom.scss @@ -0,0 +1 @@ +@import './utf8-bom'; \ No newline at end of file diff --git a/__tests__/__fixtures__/two.scss b/__tests__/__fixtures__/two.scss new file mode 100644 index 0000000..c101c43 --- /dev/null +++ b/__tests__/__fixtures__/two.scss @@ -0,0 +1,3 @@ +.two { + display: block; +} \ No newline at end of file diff --git a/__tests__/__fixtures__/utf8-bom.scss b/__tests__/__fixtures__/utf8-bom.scss new file mode 100644 index 0000000..4311ec0 --- /dev/null +++ b/__tests__/__fixtures__/utf8-bom.scss @@ -0,0 +1 @@ +@import './two'; \ No newline at end of file diff --git a/__tests__/utf8bom.test.js b/__tests__/utf8bom.test.js new file mode 100644 index 0000000..dd26bbf --- /dev/null +++ b/__tests__/utf8bom.test.js @@ -0,0 +1,39 @@ +const fs = require('fs'); +const path = require('path'); +const assert = require('assert'); + +const baka = require('../dist/index'); +const { mkdir, del } = require('../lib/utils'); + +const dist = path.join(__dirname, 'dist'); +const fixtures = path.join(__dirname, '__fixtures__'); + + +describe.only('utf8bom', () => { + + beforeAll(() => { + mkdir(dist); + }); + + describe('baka:full', () => { + + test('source vs flat', () => { + // arrange + const src = path.join(fixtures, 'import-utf8-bom.scss'); + const result = path.join(dist, 'import-utf8-bom-flat.scss'); + + del(result ); + + // act + baka.build(src, result); + + // assert + const resultBuffer = fs.readFileSync(result); + const resultContent = resultBuffer.toString(); + + assert.equal(resultContent.indexOf('.two') >= 0, true); + }); + + }); + +}); diff --git a/src/parse.ts b/src/parse.ts index 8427306..1c18c39 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -80,12 +80,15 @@ export function parse( file: string, options: SharedOptions ) { return '// File already imported_once. Skipping output.'; } const buffer = fs.readFileSync(file, 'utf8'); + // remove non-printing characters that can occur when reading a utf8-BOM file + const content = buffer.replace(/[\u200B-\u200D\uFEFF]/g, ''); + let output = ''; importedPaths.push(path.dirname( file )); importedFiles.add( file ); - output += buffer.replace(RE_IMPORT, ( match, filePath, annotation ) => { + output += content.replace(RE_IMPORT, ( match, filePath, annotation ) => { return importReplacer( match, filePath, annotation, options ); });