1
1
/* jshint node: true */
2
2
'use strict' ;
3
3
4
+ /**
5
+ * Module dependencies.
6
+ */
7
+
8
+ var Concat = require ( 'concat-with-sourcemaps' ) ;
9
+ var extend = require ( 'object-assign' ) ;
4
10
var through = require ( 'through2' ) ;
5
11
var gutil = require ( 'gulp-util' ) ;
6
- var extend = require ( 'object-assign' ) ;
12
+ var path = require ( 'path' ) ;
13
+
14
+ /**
15
+ * gulp-header plugin
16
+ */
7
17
8
- var headerPlugin = function ( headerText , data ) {
18
+ module . exports = function ( headerText , data ) {
9
19
headerText = headerText || '' ;
10
20
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
+ }
12
33
13
34
var template = gutil . template ( headerText , extend ( { file : file } , data ) ) ;
14
35
36
+ concat = new Concat ( true , fileName , gutil . linefeed ) ;
37
+
15
38
if ( file . isBuffer ( ) ) {
16
- file . contents = Buffer . concat ( [
17
- new Buffer ( template ) ,
18
- file . contents
19
- ] ) ;
39
+ concat . add ( fileName , new Buffer ( template ) ) ;
20
40
}
21
41
22
42
if ( file . isStream ( ) ) {
@@ -26,14 +46,23 @@ var headerPlugin = function(headerText, data) {
26
46
file . contents = file . contents . pipe ( stream ) ;
27
47
}
28
48
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
+
29
59
// make sure the file goes through the next gulp plugin
30
60
this . push ( file ) ;
61
+
31
62
// tell the stream engine that we are done with this file
32
63
cb ( ) ;
33
64
} ) ;
34
65
35
66
// returning the file stream
36
67
return stream ;
37
68
} ;
38
-
39
- module . exports = headerPlugin ;
0 commit comments