From 1b2f9a48c79a23bcb8d79f07d2479478fb04de74 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 19 Apr 2017 15:55:03 -0500 Subject: [PATCH] Breaking: Remove extra newline added before sourcemap comment --- lib/helpers.js | 13 ++++++------- package.json | 3 +-- test/write.js | 12 ++++++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 46cb4af..c080e65 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -6,9 +6,11 @@ var fs = require('graceful-fs'); var File = require('vinyl'); var async = require('async'); var convert = require('convert-source-map'); -var stripBom = require('strip-bom'); -var detectNewline = require('detect-newline'); var normalizePath = require('normalize-path'); +var fileNormalize = require('file-normalize'); + +var stripBom = fileNormalize.stripBOM; +var appendBuffer = fileNormalize.appendBuffer; var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//; @@ -200,9 +202,6 @@ function createSourceMapFile(opts) { // TODO: any way to make this function not require file? function commentFormatter(file, url) { - // TODO: can this be built into convert-source-map? - var newline = detectNewline(file.contents.toString()); - // TODO: Not sure I agree with this if (file.extname !== '.js' && file.extname !== '.css') { return ''; @@ -212,7 +211,7 @@ function commentFormatter(file, url) { multiline: (file.extname === '.css') }; - return newline + convert.generateMapFileComment(url, opts) + newline; + return convert.generateMapFileComment(url, opts); } function includeContent(file, state, options, callback) { @@ -306,7 +305,7 @@ function contentIncluded(file, state, options, callback) { // append source map comment if (options.addComment) { - file.contents = Buffer.concat([file.contents, new Buffer(comment)]); + file.contents = appendBuffer(file.contents, comment); } callback(); diff --git a/package.json b/package.json index 666ea0b..20948d4 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,10 @@ "dependencies": { "async": "^2.1.4", "convert-source-map": "1.3.0", - "detect-newline": "^2.1.0", + "file-normalize": "^1.1.0", "graceful-fs": "^4.1.6", "normalize-path": "^2.1.1", "object.defaults": "^1.0.0", - "strip-bom": "^2.0.0", "through2": "^2.0.1", "vinyl": "^1.2.0" } diff --git a/test/write.js b/test/write.js index 9121c38..4108e0d 100644 --- a/test/write.js +++ b/test/write.js @@ -183,7 +183,7 @@ describe('write', function() { // TODO: Vinyl.isVinyl expect(updatedFile instanceof File).toExist(); expect(updatedFile).toEqual(file); - expect(String(updatedFile.contents)).toBe( sourceContent + '\n//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\n'); + expect(String(updatedFile.contents)).toBe( sourceContent + '//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\n'); done(err); }); }); @@ -192,7 +192,7 @@ describe('write', function() { var file = makeFile(); file.path = file.path.replace('.js', '.css'); sourcemaps.write(file, function(err, updatedFile) { - expect(String(updatedFile.contents)).toBe(sourceContent + '\n/*# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + ' */\n'); + expect(String(updatedFile.contents)).toBe(sourceContent + '/*# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + ' */\n'); done(err); }); }); @@ -210,7 +210,7 @@ describe('write', function() { var file = makeFile(); file.contents = new Buffer(file.contents.toString().replace(/\n/g, '\r\n')); sourcemaps.write(file, function(err, updatedFile) { - expect(String(updatedFile.contents)).toBe(sourceContent.replace(/\n/g, '\r\n') + '\r\n//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\r\n'); + expect(String(updatedFile.contents)).toBe(sourceContent.replace(/\n/g, '\r\n') + '//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\r\n'); done(err); }); }); @@ -220,7 +220,7 @@ describe('write', function() { sourcemaps.write(file, { path: '../maps', destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile instanceof File).toExist(); expect(updatedFile).toEqual(file); - expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=../maps/helloworld.js.map\n'); + expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=../maps/helloworld.js.map\n'); expect(updatedFile.sourceMap.file).toBe('../dist/helloworld.js'); expect(sourceMapFile instanceof File).toExist(); expect(sourceMapFile.path).toBe(path.join(__dirname, 'maps/helloworld.js.map')); @@ -239,7 +239,7 @@ describe('write', function() { it('should create shortest path to map in file comment', function(done) { var file = makeNestedFile(); sourcemaps.write(file, { path: 'dir1/maps' }, function(err, updatedFile) { - expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=../maps/dir1/dir2/helloworld.js.map\n'); + expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=../maps/dir1/dir2/helloworld.js.map\n'); done(err); }); }); @@ -428,7 +428,7 @@ describe('write', function() { } }, function(err, updatedFile) { if (/helloworld\.js$/.test(updatedFile.path)) { - expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=http://maps.example.com/dir1/dir2/helloworld.js.map\n'); + expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=http://maps.example.com/dir1/dir2/helloworld.js.map\n'); done(err); } });