Skip to content

Commit

Permalink
fix: fixed an issue whereby files encoded with UTF8-BOM would cause i…
Browse files Browse the repository at this point in the history
…mports to be ignored joneff#121
  • Loading branch information
Rik Crompton committed Mar 1, 2024
1 parent 705d157 commit 6aac86a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions __tests__/__fixtures__/import-utf8-bom.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './utf8-bom';
3 changes: 3 additions & 0 deletions __tests__/__fixtures__/two.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.two {
display: block;
}
1 change: 1 addition & 0 deletions __tests__/__fixtures__/utf8-bom.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './two';
39 changes: 39 additions & 0 deletions __tests__/utf8bom.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

});
5 changes: 4 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
});

Expand Down

0 comments on commit 6aac86a

Please sign in to comment.