Skip to content

Commit

Permalink
New: Initial Implementation (closes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 22, 2017
1 parent 1fc8da2 commit 6ff3070
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 2 deletions.
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var through = require('through2');
var normalize = require('normalize-path');

function mapSources(mapFn) {

function transform(file, _, cb) {
if (!file.sourceMap || !file.sourceMap.sources) {
return cb(null, file);
}

function mapper(sourcePath) {
var result = sourcePath;
if (typeof mapFn === 'function') {
result = mapFn(sourcePath, file);
}

return normalize(result);
}

file.sourceMap.sources = file.sourceMap.sources.map(mapper);

cb(null, file);
}

return through.obj(transform);
}

module.exports = mapSources;
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"cover": "istanbul cover _mocha --report lcovonly",
"coveralls": "npm run cover && istanbul-coveralls"
},
"dependencies": {},
"dependencies": {
"normalize-path": "^2.0.1",
"through2": "^2.0.3"
},
"devDependencies": {
"eslint": "^1.7.3",
"eslint-config-gulp": "^2.0.0",
Expand All @@ -32,7 +35,9 @@
"istanbul-coveralls": "^1.0.3",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"mocha": "^2.4.5"
"mississippi": "^1.3.0",
"mocha": "^2.4.5",
"vinyl": "^2.0.1"
},
"keywords": [
"sourcemap",
Expand Down
230 changes: 230 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
'use strict';

var expect = require('expect');

var miss = require('mississippi');
var File = require('vinyl');

var mapSources = require('../');

var pipe = miss.pipe;
var from = miss.from;
var concat = miss.concat;

function makeFile() {
var file = new File({
cwd: __dirname,
base: __dirname + '/assets',
path: __dirname + '/assets/helloworld.js',
contents: null,
});

file.sourceMap = {
version: 3,
file: 'helloworld.js',
names: [],
mappings: '',
sources: ['helloworld.js', 'helloworld2.js'],
};

return file;
}

describe('mapSources', function() {

it('ignores a file without sourceMap property', function(done) {
var file = makeFile();
delete file.sourceMap;

var spy = expect.createSpy();

function assert(files) {
expect(files.length).toEqual(1);
expect(spy).toNotHaveBeenCalled();
}

pipe([
from.obj([file]),
mapSources(spy),
concat(assert),
], done);
});

it('only ignores a file without sourceMap property', function(done) {
var file = makeFile();
delete file.sourceMap;
var file2 = makeFile();

function mapFn(sourcePath) {
return sourcePath;
}

var spy = expect.createSpy().andCall(mapFn);

function assert(files) {
expect(files.length).toEqual(2);
// This is 2 because there are 2 sources on file2
// If it were incorrect, it would have been called 4 times
expect(spy.calls.length).toEqual(2);
}

pipe([
from.obj([file, file2]),
mapSources(spy),
concat(assert),
], done);
});

it('ignores a file without sourceMap.sources property', function(done) {
var file = makeFile();
delete file.sourceMap.sources;

var spy = expect.createSpy();

function assert(files) {
expect(files.length).toEqual(1);
expect(spy).toNotHaveBeenCalled();
}

pipe([
from.obj([file]),
mapSources(spy),
concat(assert),
], done);
});

it('only ignores a file without sourceMap.sources property', function(done) {
var file = makeFile();
delete file.sourceMap.sources;
var file2 = makeFile();

function mapFn(sourcePath) {
return sourcePath;
}

var spy = expect.createSpy().andCall(mapFn);

function assert(files) {
expect(files.length).toEqual(2);
// This is 2 because there are 2 sources on file2
// If it were incorrect, it would have been called 4 times
expect(spy.calls.length).toEqual(2);
}

pipe([
from.obj([file, file2]),
mapSources(spy),
concat(assert),
], done);
});

it('calls map function on each source', function(done) {
var file = makeFile();

function mapFn(sourcePath) {
return '/test/' + sourcePath;
}

function assert(files) {
expect(files.length).toEqual(1);
expect(files[0].sourceMap.sources).toEqual(['/test/helloworld.js', '/test/helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources(mapFn),
concat(assert),
], done);
});

it('normalizes Windows paths to unix paths', function(done) {
var file = makeFile();

function mapFn(sourcePath) {
return '\\test\\' + sourcePath;
}

function assert(files) {
expect(files.length).toEqual(1);
expect(files[0].sourceMap.sources).toEqual(['/test/helloworld.js', '/test/helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources(mapFn),
concat(assert),
], done);
});

it('does not need a map function', function(done) {
var file = makeFile();

function assert(files) {
expect(files.length).toEqual(1);
expect(files[0].sourceMap.sources).toEqual(['helloworld.js', 'helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources(),
concat(assert),
], done);
});

it('ignores non-function argument', function(done) {
var file = makeFile();

function assert(files) {
expect(files.length).toEqual(1);
expect(files[0].sourceMap.sources).toEqual(['helloworld.js', 'helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources('invalid argument'),
concat(assert),
], done);
});

it('still normalizes without a map function', function(done) {
var file = makeFile();
file.sourceMap.sources = file.sourceMap.sources.map(function(sourcePath) {
return '\\test\\' + sourcePath;
});

function assert(files) {
expect(files.length).toEqual(1);
expect(files[0].sourceMap.sources).toEqual(['/test/helloworld.js', '/test/helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources(),
concat(assert),
], done);
});

it('calls map function with each sourcePath and the vinyl file', function(done) {
var file = makeFile();

function mapFn(sourcePath, file) {
expect(File.isVinyl(file)).toEqual(true);

return file.base + '/' + sourcePath;
}

function assert(files) {
expect(files.length).toEqual(1);

var file = files[0];

expect(file.sourceMap.sources).toEqual([file.base + '/helloworld.js', file.base + '/helloworld2.js']);
}

pipe([
from.obj([file]),
mapSources(mapFn),
concat(assert),
], done);
});
});

0 comments on commit 6ff3070

Please sign in to comment.