-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (82 loc) · 3.42 KB
/
index.js
File metadata and controls
107 lines (82 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
var fs = require('fs');
var path = require('path');
var through = require('through');
var PluginError = require('gulp-util').PluginError;
var File = require('gulp-util').File;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var SourceNode = require('source-map').SourceNode;
module.exports = function(fileName, opts) {
if (!fileName) {
throw new PluginError('gulp-concat-sourcemap', 'Missing fileName option for gulp-concat-sourcemap');
}
opts = opts || {};
var firstFile = null;
var sourceNode = new SourceNode();
function bufferContents(file) {
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', new PluginError('gulp-concat-sourcemap', 'Streaming not supported'));
if (!firstFile) firstFile = file;
var rel = path.relative(file.cwd, file.path).replace(/\\/g, '/');
if(opts.prefix) {
var p = opts.prefix;
while(p-- > 0) {
rel = rel.substring(rel.indexOf('/') + 1);
}
}
// remove utf-8 byte order mark
if (file.contents[0] === 0xEF && file.contents[1] === 0xBB && file.contents[2] === 0xBF) {
file.contents = file.contents.slice(3);
}
if(file.sourceMap && file.sourceMap.mappings != '') {
sourceNode.add(SourceNode.fromStringWithSourceMap(file.contents.toString('utf8') + '\n\n', new SourceMapConsumer(file.sourceMap)));
} else {
file.contents.toString('utf8').split('\n').forEach(function(line, j){
sourceNode.add(new SourceNode(j + 1, 0, rel, line + '\n'));
});
sourceNode.add('\n');
}
if (opts.sourcesContent) {
sourceNode.setSourceContent(rel, file.contents.toString('utf8'));
}
}
function endStream(){
if (!firstFile) return this.emit('end');
var contentPath = path.join(firstFile.base, fileName),
mapPath = contentPath + '.map';
if(!firstFile.sourceMap) {
if (/\.css$/.test(fileName)) {
sourceNode.add('/*# sourceMappingURL=' + (opts.sourceMappingBaseURL || '') + fileName + '.map' + ' */');
} else {
sourceNode.add('//# sourceMappingURL=' + (opts.sourceMappingBaseURL || '') + fileName + '.map');
}
}
var codeMap = sourceNode.toStringWithSourceMap({
file: fileName,
sourceRoot: opts.sourceRoot || ''
});
var sourceMap = codeMap.map.toJSON();
sourceMap.file = path.basename(sourceMap.file);
var contentFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: contentPath,
contents: new Buffer(codeMap.code)
});
if(firstFile.sourceMap){
contentFile.sourceMap = sourceMap;
} else {
var mapFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: mapPath,
contents: new Buffer(JSON.stringify(sourceMap, null, ' '))
});
}
this.emit('data', contentFile);
if(!firstFile.sourceMap) this.emit('data', mapFile);
this.emit('end');
}
return through(bufferContents, endStream);
};