/
source-map-support.js
134 lines (112 loc) · 2.87 KB
/
source-map-support.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Module dependencies.
*/
var SourceMap = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var sourceMapResolve = require('source-map-resolve');
var fs = require('fs');
/**
* Expose `mixin()`.
*/
module.exports = mixin;
/**
* Mixin source map support into `compiler`.
*
* @param {Compiler} compiler
* @api public
*/
function mixin(compiler) {
var file = compiler.options.filename || 'generated.css';
compiler._comment = compiler.comment;
compiler.map = new SourceMap({ file: file });
compiler.position = { line: 1, column: 1 };
compiler.files = {};
for (var k in exports) compiler[k] = exports[k];
}
/**
* Update position.
*
* @param {String} str
* @api private
*/
exports.updatePosition = function(str) {
var lines = str.match(/\n/g);
if (lines) this.position.line += lines.length;
var i = str.lastIndexOf('\n');
this.position.column = ~i ? str.length - i : this.position.column + str.length;
};
/**
* Emit `str`.
*
* @param {String} str
* @param {Number} [pos]
* @param {Boolean} [startOnly]
* @return {String}
* @api private
*/
exports.emit = function(str, pos, startOnly) {
var sourceFile = pos && pos.source || 'source.css';
if (pos && pos.start) {
this.map.addMapping({
source: sourceFile,
generated: {
line: this.position.line,
column: Math.max(this.position.column - 1, 0)
},
original: {
line: pos.start.line,
column: pos.start.column - 1
}
});
this.addFile(pos);
}
this.updatePosition(str);
if (!startOnly && pos && pos.end) {
this.map.addMapping({
source: sourceFile,
generated: {
line: this.position.line,
column: Math.max(this.position.column - 1, 0)
},
original: {
line: pos.end.line,
column: pos.end.column - 1
}
});
this.addFile(pos);
}
return str;
};
/**
* Adds a file to the source map output if it has not already been added
* @param {Object} pos
*/
exports.addFile = function(pos) {
var file = pos.source || 'source.css';
if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
if (typeof pos.content !== 'string') return;
this.files[file] = pos.content;
};
/**
* Applies any original source maps to the output.
*/
exports.applySourceMaps = function() {
Object.keys(this.files).forEach(function(file) {
var originalMap = sourceMapResolve.resolveSync(
this.files[file], file, fs.readFileSync);
if (originalMap) {
originalMap = new SourceMapConsumer(originalMap.map);
this.map.applySourceMap(originalMap, file);
}
}, this);
};
/**
* Process comments, drops sourceMap comments.
* @param {Object} node
*/
exports.comment = function(node) {
if (/^# sourceMappingURL=/.test(node.comment))
return this.emit('', node.position);
else
return this._comment(node);
};