Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed an issue whereby files encoded with UTF8-BOM would cause imports to be skipped #122

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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