forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceSource.js
More file actions
41 lines (36 loc) · 977 Bytes
/
ReplaceSource.js
File metadata and controls
41 lines (36 loc) · 977 Bytes
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Source = require("webpack-core/lib/Source");
function ReplaceSource(source) {
Source.call(this);
this._source = source;
this.replacements = [];
}
module.exports = ReplaceSource;
ReplaceSource.prototype = Object.create(Source.prototype);
ReplaceSource.prototype.replace = function(start, end, newValue) {
this.replacements.push([start, end, newValue]);
};
ReplaceSource.prototype.insert = function(pos, newValue) {
this.replacements.push([pos, pos-1, newValue]);
};
ReplaceSource.prototype._bake = function() {
this.replacements.sort(function(a, b) {
return b[0] - a[0];
});
var result = [this._source.source()];
this.replacements.forEach(function(repl) {
var remSource = result.pop();
result.push(
remSource.substr(repl[1]+1),
repl[2],
remSource.substr(0, repl[0])
);
});
result = result.reverse().join("");
return {
source: result
}
};