Skip to content

Commit af9f0f4

Browse files
committed
New: Initial implementation
1 parent b2b16e3 commit af9f0f4

4 files changed

Lines changed: 291 additions & 2 deletions

File tree

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
# map-file
1+
# @gulp-sourcemaps/map-file
2+
3+
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url]
4+
25
Gulp plugin for changing the file property of a sourcemap.
6+
7+
## Example
8+
9+
```js
10+
var mapFile = require('@gulp-sourcemaps/map-file');
11+
12+
gulp.src(...)
13+
.pipe(sourcemaps.init())
14+
.pipe(sourcemaps.write())
15+
.pipe(mapFile(function(filePath, file) {
16+
return '../' + filePath;
17+
}))
18+
.pipe(gulp.dest(...))
19+
```
20+
21+
## API
22+
23+
### `mapFile(mapFn)`
24+
25+
Takes a map function as the only argument. Returns an `objectMode` Transform stream.
26+
27+
#### `mapFn(filePath, file)`
28+
29+
The map function is called once per each [`Vinyl`][vinyl-url] object passed through the stream that contains a sourcemap. The map function is called with the `filePath` string from the `file` property of the sourcemap and the `file` object it originated from. The return value replaces the original value.
30+
31+
If a `Vinyl` object doesn't have a `sourceMap` property, the file is passed through the stream without having the `mapFn` called. If the `sourceMap.file` property doesn't exist, `filePath` will be undefined.
32+
33+
All `file` properties are normalized to use `/` instead of `\\` as path separators.
34+
35+
## License
36+
37+
MIT
38+
39+
[vinyl-url]: https://github.com/gulpjs/vinyl
40+
41+
[downloads-image]: http://img.shields.io/npm/dm/@gulp-sourcemaps/map-file.svg
42+
[npm-url]: https://npmjs.org/package/@gulp-sourcemaps/map-file
43+
[npm-image]: http://img.shields.io/npm/v/@gulp-sourcemaps/map-file.svg
44+
45+
[travis-url]: https://travis-ci.org/gulp-sourcemaps/map-file
46+
[travis-image]: http://img.shields.io/travis/gulp-sourcemaps/map-file.svg?label=travis-ci
47+
48+
[appveyor-url]: https://ci.appveyor.com/project/phated/map-file
49+
[appveyor-image]: https://img.shields.io/appveyor/ci/phated/map-file.svg?label=appveyor
50+
51+
[coveralls-url]: https://coveralls.io/r/gulp-sourcemaps/map-file
52+
[coveralls-image]: http://img.shields.io/coveralls/gulp-sourcemaps/map-file.svg

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var through = require('through2');
4+
var normalize = require('normalize-path');
5+
6+
function mapFile(mapFn) {
7+
8+
function transform(file, _, cb) {
9+
if (!file.sourceMap) {
10+
return cb(null, file);
11+
}
12+
13+
function mapper(filePath) {
14+
var result = filePath;
15+
if (typeof mapFn === 'function') {
16+
result = mapFn(filePath, file);
17+
}
18+
19+
return normalize(result);
20+
}
21+
22+
file.sourceMap.file = mapper(file.sourceMap.file);
23+
24+
cb(null, file);
25+
}
26+
27+
return through.obj(transform);
28+
}
29+
30+
module.exports = mapFile;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"cover": "istanbul cover _mocha --report lcovonly",
2424
"coveralls": "npm run cover && istanbul-coveralls"
2525
},
26-
"dependencies": {},
26+
"dependencies": {
27+
"normalize-path": "^2.0.1",
28+
"through2": "^2.0.3"
29+
},
2730
"devDependencies": {
2831
"eslint": "^1.7.3",
2932
"eslint-config-gulp": "^2.0.0",

test/index.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
5+
var miss = require('mississippi');
6+
var File = require('vinyl');
7+
var normalize = require('normalize-path');
8+
9+
var mapFile = require('../');
10+
11+
var pipe = miss.pipe;
12+
var from = miss.from;
13+
var concat = miss.concat;
14+
15+
function makeFile() {
16+
var file = new File({
17+
cwd: __dirname,
18+
base: __dirname + '/assets',
19+
path: __dirname + '/assets/helloworld.js',
20+
contents: null,
21+
});
22+
23+
file.sourceMap = {
24+
version: 3,
25+
file: 'helloworld.js',
26+
names: [],
27+
mappings: '',
28+
sources: ['helloworld.js', 'helloworld2.js'],
29+
};
30+
31+
return file;
32+
}
33+
34+
describe('mapFile', function() {
35+
36+
it('ignores a file without sourceMap property', function(done) {
37+
var file = makeFile();
38+
delete file.sourceMap;
39+
40+
var spy = expect.createSpy();
41+
42+
function assert(files) {
43+
expect(files.length).toEqual(1);
44+
expect(spy).toNotHaveBeenCalled();
45+
}
46+
47+
pipe([
48+
from.obj([file]),
49+
mapFile(spy),
50+
concat(assert),
51+
], done);
52+
});
53+
54+
it('only ignores a file without sourceMap property', function(done) {
55+
var file = makeFile();
56+
delete file.sourceMap;
57+
var file2 = makeFile();
58+
59+
function mapFn(filePath) {
60+
return filePath;
61+
}
62+
63+
var spy = expect.createSpy().andCall(mapFn);
64+
65+
function assert(files) {
66+
expect(files.length).toEqual(2);
67+
expect(spy.calls.length).toEqual(1);
68+
}
69+
70+
pipe([
71+
from.obj([file, file2]),
72+
mapFile(spy),
73+
concat(assert),
74+
], done);
75+
});
76+
77+
it('calls map function per file', function(done) {
78+
var file = makeFile();
79+
80+
function mapFn(filePath) {
81+
return '/test/' + filePath;
82+
}
83+
84+
function assert(files) {
85+
expect(files.length).toEqual(1);
86+
expect(files[0].sourceMap.file).toEqual('/test/helloworld.js');
87+
}
88+
89+
pipe([
90+
from.obj([file]),
91+
mapFile(mapFn),
92+
concat(assert),
93+
], done);
94+
});
95+
96+
it('normalizes Windows paths to unix paths', function(done) {
97+
var file = makeFile();
98+
99+
function mapFn(filePath) {
100+
return '\\test\\' + filePath;
101+
}
102+
103+
function assert(files) {
104+
expect(files.length).toEqual(1);
105+
expect(files[0].sourceMap.file).toEqual('/test/helloworld.js');
106+
}
107+
108+
pipe([
109+
from.obj([file]),
110+
mapFile(mapFn),
111+
concat(assert),
112+
], done);
113+
});
114+
115+
it('does not need a map function', function(done) {
116+
var file = makeFile();
117+
118+
function assert(files) {
119+
expect(files.length).toEqual(1);
120+
expect(files[0].sourceMap.file).toEqual('helloworld.js');
121+
}
122+
123+
pipe([
124+
from.obj([file]),
125+
mapFile(),
126+
concat(assert),
127+
], done);
128+
});
129+
130+
it('ignores non-function argument', function(done) {
131+
var file = makeFile();
132+
133+
function assert(files) {
134+
expect(files.length).toEqual(1);
135+
expect(files[0].sourceMap.file).toEqual('helloworld.js');
136+
}
137+
138+
pipe([
139+
from.obj([file]),
140+
mapFile('invalid argument'),
141+
concat(assert),
142+
], done);
143+
});
144+
145+
it('still normalizes without a map function', function(done) {
146+
var file = makeFile();
147+
file.sourceMap.file = '\\test\\' + file.sourceMap.file;
148+
149+
function assert(files) {
150+
expect(files.length).toEqual(1);
151+
expect(files[0].sourceMap.file).toEqual('/test/helloworld.js');
152+
}
153+
154+
pipe([
155+
from.obj([file]),
156+
mapFile(),
157+
concat(assert),
158+
], done);
159+
});
160+
161+
it('calls map function with the filePath and the vinyl file', function(done) {
162+
var file = makeFile();
163+
164+
function mapFn(filePath, file) {
165+
expect(File.isVinyl(file)).toEqual(true);
166+
167+
return file.base + '/' + filePath;
168+
}
169+
170+
function assert(files) {
171+
expect(files.length).toEqual(1);
172+
173+
var file = files[0];
174+
var base = normalize(file.base);
175+
176+
expect(file.sourceMap.file).toEqual(base + '/helloworld.js');
177+
}
178+
179+
pipe([
180+
from.obj([file]),
181+
mapFile(mapFn),
182+
concat(assert),
183+
], done);
184+
});
185+
186+
it('calls map function with undefined if sourceMap.file is undefined', function(done) {
187+
var file = makeFile();
188+
delete file.sourceMap.file;
189+
190+
function mapFn(filePath) {
191+
expect(filePath).toEqual(undefined);
192+
return 'test';
193+
}
194+
195+
function assert(files) {
196+
expect(files.length).toEqual(1);
197+
expect(files[0].sourceMap.file).toEqual('test');
198+
}
199+
200+
pipe([
201+
from.obj([file]),
202+
mapFile(mapFn),
203+
concat(assert),
204+
], done);
205+
});
206+
});

0 commit comments

Comments
 (0)