Skip to content

Commit

Permalink
preExisting sourceMapping checks to not modify the original file
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccready committed Nov 18, 2016
1 parent 25f5778 commit 6972337
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ reports
*.tgz
.idea
*.lock
tmp
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
"devDependencies": {
"coveralls": "2.X",
"faucet": "0.0.X",
"gulp": "3.X",
"hook-std": "0.2.X",
"istanbul": "0.X",
"jshint": "2.X",
"object-assign": "^4.1.0",
"tape": "4.X"
},
"files": [
Expand Down
9 changes: 6 additions & 3 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function init(options) {

var fileContent = file.contents.toString();
var sourceMap;
var preExisting = utils.getPreExisting(fileContent);

if (options.loadMaps) {
debug('loadMaps');
Expand Down Expand Up @@ -113,11 +114,11 @@ function init(options) {
}
sourceMap.sourcesContent[i] = sourceContent;
}
});

// remove source map comment from source
file.contents = new Buffer(fileContent, 'utf8');
});
}
// remove source map comment from source
file.contents = new Buffer(fileContent, 'utf8');
}

if (!sourceMap && options.identityMap) {
Expand Down Expand Up @@ -199,6 +200,8 @@ function init(options) {
sourcesContent: [fileContent]
};
}
else if(preExisting !== null && typeof preExisting !== 'undefined')
sourceMap.preExisting = preExisting

sourceMap.file = unixStylePath(file.relative);
file.sourceMap = sourceMap;
Expand Down
56 changes: 54 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var path = require('path');
var path = require('path'),
detectNewline = require('detect-newline');

function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
Expand All @@ -9,8 +10,59 @@ var PLUGIN_NAME = require('../package.json').name;

var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;

var debug = require('debug-fabulous')()(PLUGIN_NAME + ':utils');

var sourceMapUrlRegEx = /\/\/\# sourceMappingURL\=.*/g


var getCommentFormatter = function (file) {
var extension = file.relative.split('.').pop(),
fileContents = file.contents.toString(),
newline = detectNewline.graceful(fileContents || ''),
commentFormatter = function(url) {
return '';
};

if (file.sourceMap.preExisting){
debug('preExisting commentFormatter');
commentFormatter = function(url) {
return file.sourceMap.preExisting;
};
return commentFormatter
}

switch (extension) {
case 'css':
debug('css commentFormatter');
commentFormatter = function(url) {
return newline + "/*# sourceMappingURL=" + url + " */" + newline;
};
break;
case 'js':
debug('js commentFormatter');
commentFormatter = function(url) {
return newline + "//# sourceMappingURL=" + url + newline;
};
break;
default:
debug('unknown commentFormatter')
}

return commentFormatter;
}

var getPreExisting = function(fileContent){
if(sourceMapUrlRegEx.test(fileContent)){
debug('has preExisting');
return fileContent.match(sourceMapUrlRegEx)[0];
}
}

module.exports = {
unixStylePath: unixStylePath,
PLUGIN_NAME: PLUGIN_NAME,
urlRegex: urlRegex
urlRegex: urlRegex,
sourceMapUrlRegEx: sourceMapUrlRegEx,
getCommentFormatter: getCommentFormatter,
getPreExisting: getPreExisting
};
41 changes: 13 additions & 28 deletions src/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ var utils = require('./utils'),
fs = require('graceful-fs'),
path = require('path'),
File = require('vinyl'),
stripBom = require('strip-bom'),
detectNewline = require('detect-newline');
stripBom = require('strip-bom');

/**
* Write the source map
Expand Down Expand Up @@ -92,34 +91,13 @@ function write(destPath, options) {
delete sourceMap.sourcesContent;
}

var extension = file.relative.split('.').pop();
var newline = detectNewline.graceful(file.contents.toString());
var commentFormatter;

switch (extension) {
case 'css':
commentFormatter = function(url) {
return newline + "/*# sourceMappingURL=" + url + " */" + newline;
};
break;
case 'js':
commentFormatter = function(url) {
return newline + "//# sourceMappingURL=" + url + newline;
};
break;
default:
/* jshint ignore:start */
commentFormatter = function(url) {
return "";
};
/* jshint ignore:end */
}

var comment;
var comment,
commentFormatter = utils.getCommentFormatter(file);

if (destPath === undefined || destPath === null) {
// encode source map into comment
var base64Map = new Buffer(JSON.stringify(sourceMap)).toString('base64');
debug("basic comment")
comment = commentFormatter('data:application/json;charset=' + options.charset + ';base64,' + base64Map);
} else {
var mapFile = path.join(destPath, file.relative) + '.map';
Expand Down Expand Up @@ -174,16 +152,23 @@ function write(destPath, options) {
}
sourceMapPathRelative = prefix + path.join('/', sourceMapPathRelative);
}
debug("destPath comment")
comment = commentFormatter(unixStylePath(sourceMapPathRelative));

if (options.sourceMappingURL && typeof options.sourceMappingURL === 'function') {
debug("options.sourceMappingURL comment")
comment = commentFormatter(options.sourceMappingURL(file));
}
}

// append source map comment
if (options.addComment)
file.contents = Buffer.concat([file.contents, new Buffer(comment)]);
if (options.addComment){
if(options.preExisting && utils.getPreExisting(String(file.contents))){
//normally the comment is pre-stripped but this option lets us know it is not
}
else
file.contents = Buffer.concat([file.contents, new Buffer(comment)]);
}

this.push(file);
callback();
Expand Down
7 changes: 7 additions & 0 deletions test/assets/helloworld.map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ test('init: should import an existing inline source map', function(t) {
t.ok(data, 'should pass something through');
t.ok(data instanceof File, 'should pass a vinyl file through');
t.ok(data.sourceMap, 'should add a source map object');
t.notOk(/sourceMappingURL/.test(data.contents.toString()), 'should not have sourcemapping in data.contents');
t.equal(String(data.sourceMap.version), '3', 'should have version 3');
t.deepEqual(data.sourceMap.sources, [
'test1.js', 'test2.js'
Expand All @@ -179,14 +180,6 @@ test('init: should import an existing inline source map', function(t) {
}).write(makeFileWithInlineSourceMap());
});

test('init: should remove inline sourcemap', function(t) {
var pipeline = sourcemaps.init({loadMaps: true});
pipeline.on('data', function(data) {
t.notOk(/sourceMappingURL/.test(data.contents.toString()), 'should not have sourcemapping');
t.end();
}).write(makeFileWithInlineSourceMap());
});

test('init: should load external source map file referenced in comment with the \/\/# syntax', function(t) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
Expand Down Expand Up @@ -400,10 +393,16 @@ test('init: should output an error message if debug option is set and sourceCont

pipeline.on('data', function() {
unhook();
// debug.save(null);
t.ok(history.length == 4, 'history len');
t.ok(history[2].match(/No source content for \"missingfile\". Loading from file./), 'should log missing source content');
t.ok(history[3].match(/source file not found: /), 'should warn about missing file');
var hasRegex = function(regex){
return function(s){
return regex.test(s);
};
};
t.ok(
history.some(
hasRegex(/No source content for \"missingfile\". Loading from file./)),
'should log missing source content');
t.ok(history.some(hasRegex(/source file not found: /)), 'should warn about missing file');
t.end();
}).write(file);

Expand Down
59 changes: 59 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
var gulp = require('gulp');
var test = require('tape');
var $ = require('..');
var PLUGIN_NAME = require('../src/utils').PLUGIN_NAME;
var debug = require('debug-fabulous')()(PLUGIN_NAME + ':test:integration');
var join = require('path').join;
var fs = require('fs');
var sourceContent = fs.readFileSync(join(__dirname, 'assets/helloworld.js')).toString();

debug('running');

test('creates inline mapping', function(t) {

gulp.src(join(__dirname, './assets/helloworld.js'))
.pipe($.init())
.pipe($.write())
// .pipe(gulp.dest('tmp'))
.on('data', function(data) {
t.ok(data.sourceMap, 'should add a source map object');
t.deepEqual(
data.contents.toString(),
sourceContent + "\n//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlcyI6WyJoZWxsb3dvcmxkLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIid1c2Ugc3RyaWN0JztcblxuZnVuY3Rpb24gaGVsbG9Xb3JsZCgpIHtcbiAgICBjb25zb2xlLmxvZygnSGVsbG8gd29ybGQhJyk7XG59XG4iXSwiZmlsZSI6ImhlbGxvd29ybGQuanMifQ==\n",
'file should be sourcemapped'
);
t.end();
})
.on('error', function() {
t.fail('emitted error');
t.end();
})
.on('close', function() {
t.end();
});
});

test('creates re-uses existing mapping', function(t) {
gulp.src(join(__dirname, './assets/helloworld.map.js'))
.pipe($.init({loadMaps:true}))
.pipe($.write())
// .pipe(gulp.dest('tmp'))
.on('data', function(data) {
t.ok(data.sourceMap, 'should add a source map object');
t.ok(!!data.sourceMap.preExisting, 'should know the sourcemap pre-existed');
t.deepEqual(
data.contents.toString(),
sourceContent + "\n//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlcyI6WyJoZWxsb3dvcmxkLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIid1c2Ugc3RyaWN0JztcblxuZnVuY3Rpb24gaGVsbG9Xb3JsZCgpIHtcbiAgICBjb25zb2xlLmxvZygnSGVsbG8gd29ybGQhJyk7XG59XG4iXSwiZmlsZSI6ImhlbGxvd29ybGQuanMifQ==",
'file should be sourcemapped'
);
t.end();
})
.on('error', function() {
t.fail('emitted error');
t.end();
})
.on('close', function() {
t.end();
});
});
Loading

0 comments on commit 6972337

Please sign in to comment.