diff --git a/index.js b/index.js index 85a21f3..6dafe6c 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ var fs = require('fs'), es = require('event-stream'), gutil = require('gulp-util'), glob = require('glob'), - applySourceMap = require('vinyl-sourcemaps-apply'); + applySourceMap = require('vinyl-sourcemaps-apply'), + stripBom = require('strip-bom'); var SourceMapGenerator = require('source-map').SourceMapGenerator; var SourceMapConsumer = require('source-map').SourceMapConsumer; @@ -134,7 +135,8 @@ function processInclude(content, filePath, sourceMap) { if (!inExtensions(globbedFilePath)) continue; // Get file contents and apply recursive include on result - var fileContents = fs.readFileSync(globbedFilePath); + // Unicode byte order marks are stripped from the start of included files + var fileContents = stripBom(fs.readFileSync(globbedFilePath)); var result = processInclude(fileContents.toString(), globbedFilePath, sourceMap); var resultContent = result.content; diff --git a/package.json b/package.json index 22c3df4..bacf00f 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "glob": "^5.0.12", "gulp-util": "~2.2.10", "source-map": "^0.5.1", + "strip-bom": "^2.0.0", "vinyl-sourcemaps-apply": "^0.2.0" } } diff --git a/test/expected/html/basic-include-output-with-unicode-BOM.html b/test/expected/html/basic-include-output-with-unicode-BOM.html new file mode 100644 index 0000000..8e79b38 --- /dev/null +++ b/test/expected/html/basic-include-output-with-unicode-BOM.html @@ -0,0 +1,10 @@ + + + + + Test + + +
This is a test
+ + \ No newline at end of file diff --git a/test/fixtures/html/basic-include-with-unicode-BOM.html b/test/fixtures/html/basic-include-with-unicode-BOM.html new file mode 100644 index 0000000..9b95047 --- /dev/null +++ b/test/fixtures/html/basic-include-with-unicode-BOM.html @@ -0,0 +1,10 @@ + + + + + Test + + + + + \ No newline at end of file diff --git a/test/fixtures/html/test-with-unicode-BOM.html b/test/fixtures/html/test-with-unicode-BOM.html new file mode 100644 index 0000000..f6e5cb8 --- /dev/null +++ b/test/fixtures/html/test-with-unicode-BOM.html @@ -0,0 +1 @@ +
This is a test
\ No newline at end of file diff --git a/test/main.js b/test/main.js index e58542c..fc437cb 100644 --- a/test/main.js +++ b/test/main.js @@ -152,4 +152,23 @@ describe("gulp-include", function () { })) .pipe(assert.end(done)); }); -}) \ No newline at end of file + + it('should strip unicode byte order marks from included files', function (done) { + var file = new gutil.File({ + base: "test/fixtures/", + path: "test/fixtures/html/basic-include-with-unicode-BOM.html", + contents: fs.readFileSync("test/fixtures/html/basic-include-with-unicode-BOM.html") + }); + + testInclude = include(); + testInclude.on("data", function (newFile) { + should.exist(newFile); + should.exist(newFile.contents); + + String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/html/basic-include-output-with-unicode-BOM.html"), "utf8")) + done(); + }); + + testInclude.write(file); + }) +})