Skip to content

Commit

Permalink
revert mapSource absolute path as default
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccready committed Jan 12, 2017
1 parent d7c99a7 commit fb4027a
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 85 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "index.js",
"scripts": {
"lint": "jshint ./src/**/*.js test/*.js",
"test": "npm run lint && faucet test/*.js",
"test": "npm run lint && faucet test/*.js $@",
"tap": "tape test/*.js",
"cover": "istanbul cover --dir reports/coverage tape \"test/*.js\"",
"coveralls": "istanbul cover tape \"test/*.js\" --report lcovonly && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
Expand Down Expand Up @@ -49,7 +49,8 @@
"jshint": "2.X",
"lodash": "4.17.4",
"object-assign": "^4.1.0",
"tape": "4.X"
"tape": "4.X",
"yargs": "6.6.0"
},
"files": [
"index.js",
Expand Down
28 changes: 18 additions & 10 deletions src/write/index.internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = function(destPath, options) {
stripBom = require('strip-bom'),
makeDebug = require('debug-fabulous')();

var rootDebug = makeDebug(PLUGIN_NAME + ':write:internals');
rootDebug(utils.logCb("options"));
rootDebug(utils.logCb(options));

function setSourceRoot(file) {
var debug = makeDebug(PLUGIN_NAME + ':write:internals:setSourceRoot');

Expand Down Expand Up @@ -37,21 +41,25 @@ module.exports = function(destPath, options) {
return;
}

debug(utils.logCb("file.path: " + file.path));
debug(utils.logCb("file.cwd: " + file.cwd));
debug(utils.logCb("file.base: " + file.base));

file.sourceMap.sources = file.sourceMap.sources.map(function(filePath) {
// keep the references files like ../node_modules within the sourceRoot
debug(utils.logCb("filePath: " + filePath));
debug(utils.logCb("file.path: " + file.path));
debug(utils.logCb("file.cwd: " + file.cwd));
debug(utils.logCb("file.base: " + file.base));

if (!file.dirname){
debug(utils.logCb('!file.dirname'));
filePath = path.join(file.base, filePath).replace(file.cwd, '');
} else {
debug(utils.logCb('file.dirname: ' + file.dirname));
filePath = path.resolve(file.dirname, filePath).replace(file.cwd, '');
}
if (options.mapSourcesAbsolute === true){
debug(utils.logCb('mapSourcesAbsolute'));

if (!file.dirname){
debug(utils.logCb('!file.dirname'));
filePath = path.join(file.base, filePath).replace(file.cwd, '');
} else {
debug(utils.logCb('file.dirname: ' + file.dirname));
filePath = path.resolve(file.dirname, filePath).replace(file.cwd, '');
}
}
return unixStylePath(filePath);
});
}
Expand Down
11 changes: 8 additions & 3 deletions src/write/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ var utils = require('../utils'),
function write(destPath, options) {
var debug = require('debug-fabulous')()(PLUGIN_NAME + ':write');

debug(utils.logCb("destPath"));
debug(utils.logCb(destPath));

debug(utils.logCb("original options"));
debug(utils.logCb(options));

if (options === undefined && typeof destPath !== 'string') {
options = destPath;
destPath = undefined;
Expand All @@ -28,9 +34,8 @@ function write(destPath, options) {
if (options.charset === undefined)
options.charset = "utf8";

debug(function() {
return options;
});
debug(utils.logCb("derrived options"));
debug(utils.logCb(options));

var internals = internalsInit(destPath, options);

Expand Down
76 changes: 35 additions & 41 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

var test = require('tape');
//BEGIN PRE-HOOK of debug
var yargs = require('yargs').argv;
var debug = require('debug-fabulous')();
// Begin inner conflict with self here..
// nmccready:
//
//
// bliss: Turning debug on here clutters the test output;
// but, having debug off in portions results in not having 100% test coverage.
//
// touche: If we demand 100% coverage, then we need clutter logging :_(
//
// partial-bliss: The alternative is to have ignore cover statements on some of the logging.
// touche: However, those logging statements could potentially fail
debug.save('gulp-sourcemaps:init');
debug.save('gulp-sourcemaps:init:*');
debug.enable(debug.load());

if (!yargs.ignoreLogTests){
debug.save('gulp-sourcemaps:*');
debug.enable(debug.load());
}
//END PRE-HOOK of debug (must be loaded before our main module (sourcemaps))
var sourcemaps = require('..');
var File = require('vinyl');
Expand Down Expand Up @@ -381,32 +373,34 @@ test('init: handle null contents', function(t) {
}).write(makeNullFile());
});

//should always be last as disabling a debug namespace does not work
test('init: should output an error message if debug option is set and sourceContent is missing', function(t) {

var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');

var history = [];
console.log('HOOKING');
var unhook = hookStd.stderr(function(s) {
history.push(s);
});
var pipeline = sourcemaps.init({loadMaps: true});

pipeline.on('data', function() {
unhook();
var hasRegex = function(regex){
return function(s){
return regex.test(s);
if (!yargs.ignoreLogTests){
//should always be last as disabling a debug namespace does not work
test('init: should output an error message if debug option is set and sourceContent is missing', function(t) {

var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');

var history = [];
console.log('HOOKING');
var unhook = hookStd.stderr(function(s) {
history.push(s);
});
var pipeline = sourcemaps.init({loadMaps: true});

pipeline.on('data', function() {
unhook();
var hasRegex = function(regex){
return function(s){
return regex.test(s);
};
};
};
console.log(history);
t.ok(
history.some(
hasRegex(/No source content for "missingfile". Loading from file./g)), 'should log missing source content');
t.ok(history.some(hasRegex(/source file not found: /g)), 'should warn about missing file');
t.end();
}).write(file);
console.log(history);
t.ok(
history.some(
hasRegex(/No source content for "missingfile". Loading from file./g)), 'should log missing source content');
t.ok(history.some(hasRegex(/source file not found: /g)), 'should warn about missing file');
t.end();
}).write(file);

});
});
}
37 changes: 35 additions & 2 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var test = require('tape');
var sourcemaps = require('..');
var PLUGIN_NAME = require('../src/utils').PLUGIN_NAME;
var debug = require('debug-fabulous')()(PLUGIN_NAME + ':test:integration');
var yargs = require('yargs').argv;
var debug = require('debug-fabulous')();
if (!yargs.ignoreLogTests){
debug.save('gulp-sourcemaps:*');
debug.enable(debug.load());
}
var join = require('path').join;
var fs = require('fs');
var sourceContent = fs.readFileSync(join(__dirname, 'assets/helloworld.js')).toString();
Expand Down Expand Up @@ -231,6 +235,7 @@ test('combined: mapped preExisting with two tasks', function(t) {
});
});


// - thanks @twiggy https://github.com/floridoo/gulp-sourcemaps/issues/270#issuecomment-271723208
test('sources: is valid with concat', function(t) {

Expand All @@ -245,6 +250,34 @@ test('sources: is valid with concat', function(t) {
.on('data', function(file) {
if (!/.*\.map/.test(file.path)) return;

var contents = JSON.parse(file.contents.toString());
_.each(contents.sources, function(s, i){
t.deepEqual(s, "test" + (i+3) + ".js", "source is correct, test" + (i+3) + ".js");
});
})
.on('error', function() {
t.fail('emitted error');
t.end();
}).on('finish', function(){
moveHtml('sources_concat', t);
});

});

// - thanks @twiggy https://github.com/floridoo/gulp-sourcemaps/issues/270#issuecomment-271723208
test('sources: mapSourcesAbsolute: is valid with concat', function(t) {

gulp.src([
join(__dirname, './assets/test3.js'),
join(__dirname, './assets/test4.js'),
])
.pipe(sourcemaps.init())
.pipe($.concat("index.js"))
.pipe(sourcemaps.write('.', {mapSourcesAbsolute:true}))
.pipe(gulp.dest('tmp/sources_concat'))
.on('data', function(file) {
if (!/.*\.map/.test(file.path)) return;

var contents = JSON.parse(file.contents.toString());
_.each(contents.sources, function(s, i){
t.deepEqual(s, "/test/assets/test" + (i+3) + ".js", "source is correct, test" + (i+3) + ".js");
Expand Down
69 changes: 42 additions & 27 deletions test/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var yargs = require('yargs').argv;
var test = require('tape');
var sourcemaps = require('..');
var File = require('vinyl');
Expand All @@ -8,9 +9,11 @@ var path = require('path');
var fs = require('fs');
var hookStd = require('hook-std');
var debug = require('debug-fabulous')();
debug.save('gulp-sourcemaps:write');
debug.save('gulp-sourcemaps:write:*');
debug.enable(debug.load());

if (!yargs.ignoreLogTests){
debug.save('gulp-sourcemaps:*');
debug.enable(debug.load());
}
var assign = require('object-assign');
var utils = require('../src/utils');
var convert = require('convert-source-map');
Expand Down Expand Up @@ -358,6 +361,16 @@ test('write: should not throw when unable to fetch missing sourceContent', funct
test('write: should set the sourceRoot by option sourceRoot', function(t) {
var file = makeFile();
var pipeline = sourcemaps.write({sourceRoot: '/testSourceRoot'});
pipeline.on('data', function(data) {
t.deepEqual(data.sourceMap.sources, ['helloworld.js'], 'should have the correct sources');
t.equal(data.sourceMap.sourceRoot, '/testSourceRoot', 'should set sourceRoot');
t.end();
}).write(file);
});

test('write: should set the mapSourcesAbsolute by option mapSourcesAbsolute', function(t) {
var file = makeFile();
var pipeline = sourcemaps.write({sourceRoot: '/testSourceRoot', mapSourcesAbsolute: true});
pipeline.on('data', function(data) {
t.deepEqual(data.sourceMap.sources, ['/assets/helloworld.js'], 'should have the correct sources');
t.equal(data.sourceMap.sourceRoot, '/testSourceRoot', 'should set sourceRoot');
Expand Down Expand Up @@ -623,28 +636,30 @@ test('write: should allow to change sources', function(t) {
}).write(file);
});

//should always be last as disabling a debug namespace does not work
test('write: should output an error message if debug option is set and sourceContent is missing', function(t) {
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;

var history = [];
var unhook = hookStd.stderr(function(s) {
history.push(s);
});
var pipeline = sourcemaps.write();

var hasRegex = function(regex){
return function(s){
return regex.test(s);
if (!yargs.ignoreLogTests){
//should always be last as disabling a debug namespace does not work
test('write: should output an error message if debug option is set and sourceContent is missing', function(t) {
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;

var history = [];
var unhook = hookStd.stderr(function(s) {
history.push(s);
});
var pipeline = sourcemaps.write();

var hasRegex = function(regex){
return function(s){
return regex.test(s);
};
};
};
pipeline.on('data', function() {
unhook();
console.log(JSON.stringify(history));
t.ok(history.some(hasRegex(/No source content for "helloworld.js.invalid". Loading from file./g)), 'should log missing source content');
t.ok(history.some(hasRegex(/source file not found: /g)), 'should warn about missing file');
t.end();
}).write(file);
});
pipeline.on('data', function() {
unhook();
console.log(JSON.stringify(history));
t.ok(history.some(hasRegex(/No source content for "helloworld.js.invalid". Loading from file./g)), 'should log missing source content');
t.ok(history.some(hasRegex(/source file not found: /g)), 'should warn about missing file');
t.end();
}).write(file);
});
}

0 comments on commit fb4027a

Please sign in to comment.