Skip to content

Commit 4469bd6

Browse files
committed
add sourcemap support. #7
1 parent 78e7dee commit 4469bd6

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

index.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
/* jshint node: true */
22
'use strict';
33

4+
/**
5+
* Module dependencies.
6+
*/
7+
8+
var Concat = require('concat-with-sourcemaps');
9+
var extend = require('object-assign');
410
var through = require('through2');
511
var gutil = require('gulp-util');
6-
var extend = require('object-assign');
12+
var path = require('path');
13+
14+
/**
15+
* gulp-header plugin
16+
*/
717

8-
var headerPlugin = function(headerText, data) {
18+
module.exports = function (headerText, data) {
919
headerText = headerText || '';
1020

11-
var stream = through.obj(function(file, enc, cb) {
21+
var fileName;
22+
var concat;
23+
24+
var stream = through.obj(function (file, enc, cb) {
25+
26+
if (typeof file === 'string') {
27+
fileName = file;
28+
} else if (typeof file.path === 'string') {
29+
fileName = path.basename(file.path);
30+
} else {
31+
fileName = '';
32+
}
1233

1334
var template = gutil.template(headerText, extend({file : file}, data));
1435

36+
concat = new Concat(true, fileName, gutil.linefeed);
37+
1538
if (file.isBuffer()) {
16-
file.contents = Buffer.concat([
17-
new Buffer(template),
18-
file.contents
19-
]);
39+
concat.add(fileName, new Buffer(template));
2040
}
2141

2242
if (file.isStream()) {
@@ -26,14 +46,23 @@ var headerPlugin = function(headerText, data) {
2646
file.contents = file.contents.pipe(stream);
2747
}
2848

49+
// add sourcemap
50+
concat.add(file.relative, file.contents, file.sourceMap);
51+
52+
file.contents = concat.content;
53+
54+
// apply source map
55+
if (concat.sourceMapping) {
56+
file.sourceMap = JSON.parse(concat.sourceMap);
57+
}
58+
2959
// make sure the file goes through the next gulp plugin
3060
this.push(file);
61+
3162
// tell the stream engine that we are done with this file
3263
cb();
3364
});
3465

3566
// returning the file stream
3667
return stream;
3768
};
38-
39-
module.exports = headerPlugin;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"dependencies": {
4444
"gulp-util": "*",
4545
"object-assign": "*",
46-
"through2": "*"
46+
"through2": "*",
47+
"concat-with-sourcemaps": "*"
4748
},
4849
"devDependencies": {
4950
"event-stream": "^3.1.7",

0 commit comments

Comments
 (0)