Created
August 11, 2015 13:22
-
-
Save benaadams/ec6354e81dff7184a53b to your computer and use it in GitHub Desktop.
Add UTF8 BOM as gulp pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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