/
index.js
45 lines (41 loc) · 1.24 KB
/
index.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
"use strict";
var through = require('through2');
var acornGlobals = require('acorn-globals');
var MagicString = require('magic-string');
/*
* Convenience method for turning a through2 stream
* into a full string. Otherwise we may just get chunks,
* because Node buffers are chunked based on file size.
*/
function streamToString(callback) {
var contents = '';
return through(
function (chunk, enc, next) {
contents += chunk.toString('utf8');
next();
},
function (next) { // flush function
callback.bind(this)(contents, next);
}
);
}
function replaceify(filename, opts) {
var replacements = opts.replacements || {};
return streamToString(function (contents, next) {
try {
var globals = acornGlobals(contents);
var magicString = new MagicString(contents);
globals.forEach(function (theGlobal) {
if (theGlobal.name in replacements && replacements.hasOwnProperty(theGlobal.name)) {
theGlobal.nodes.forEach(function (node) {
magicString.overwrite(node.start, node.end, replacements[theGlobal.name]);
});
}
});
contents = magicString.toString();
} catch (e) {}
this.push(contents);
next();
});
}
module.exports = replaceify;