-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (54 loc) · 1.49 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* espowerify - Browserify transform for power-assert.
*
* https://github.com/power-assert-js/espowerify
*
* Copyright (c) 2014-2016 Takuto Wada
* Licensed under the MIT license.
* https://github.com/power-assert-js/espowerify/blob/master/MIT-LICENSE.txt
*/
'use strict';
var through = require('through');
var path = require('path');
var extend = require('xtend');
var espowerSource = require('espower-source');
function endsWith (str, suffix) {
return str.slice(str.length - suffix.length) === suffix;
}
function isTransformTarget (filepath, options) {
if (path.extname(filepath) === '.json') {
return false;
}
if (!options || !options._flags || !options._flags.entries) {
return true;
}
return options._flags.entries.some(function (entryPath) {
return endsWith(filepath, path.normalize(entryPath));
});
}
/**
* Apply espower through the browserify transform chain.
*
* @param {String} filepath
* @param {Object} options
* @return {Stream}
*/
function espowerify(filepath, options) {
if (!isTransformTarget(filepath, options)) {
return through();
}
var data = '';
var stream = through(write, end);
function write(buf) {
data += buf;
}
function end() {
var espowerOptions = extend({
sourceRoot: process.cwd()
}, options);
stream.queue(espowerSource(data, filepath, espowerOptions));
stream.queue(null);
}
return stream;
}
module.exports = espowerify;