Skip to content

Instantly share code, notes, and snippets.

@benaadams
Created August 11, 2015 13:22
Show Gist options
  • Save benaadams/ec6354e81dff7184a53b to your computer and use it in GitHub Desktop.
Save benaadams/ec6354e81dff7184a53b to your computer and use it in GitHub Desktop.
Add UTF8 BOM as gulp pipe
function addBOM() {
// Creating a stream through which each file will pass
return through.obj( function ( file, enc, cb ) {
if ( file.isNull() ) {
// return empty file
cb( null, file );
}
var buf = file.contents;
var decoder = new StringDecoder( 'utf8' );
var contents = decoder.write( file.contents );
var missingBOM = ( contents[0] !== 0xEF && contents[1] !== 0xBE && contents[2] !== 0xBB );
if ( file.isBuffer() ) {
if ( missingBOM ) {
file.contents = new Buffer( '\ufeff' + contents, 'utf-8' );
} else {
file.contents = new Buffer( contents, 'utf-8' );
}
}
if ( file.isStream() ) {
var stream = through();
if ( missingBOM ) {
stream.write( '\ufeff' + contents, 'utf-8' );
} else {
stream.write( contents, 'utf-8' );
}
file.contents = stream;
}
cb( null, file );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment