-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
52 lines (48 loc) · 1.77 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
/**
* espower-loader - Power Assert feature instrumentor on the fly.
*
* https://github.com/power-assert-js/espower-loader
*
* Copyright (c) 2013-2017 Takuto Wada
* Licensed under the MIT license.
* https://github.com/power-assert-js/espower-loader/blob/master/MIT-LICENSE.txt
*/
var extensions = require.extensions;
var originalLoader = extensions['.js'];
var fs = require('fs');
var extend = require('xtend');
var minimatch = require('minimatch');
var convert = require('convert-source-map');
var sourceMapSupport = require('source-map-support');
var espowerSourceToSource = require('espower-source');
var pathToMap = {};
function espowerLoader (options) {
'use strict';
options = extend({ cwd: process.cwd() }, options);
var patternStartsWithSlash = (options.pattern.lastIndexOf('/', 0) === 0);
var separator = patternStartsWithSlash ? '' : '/';
var pattern = options.cwd + separator + options.pattern;
sourceMapSupport.install({
retrieveSourceMap: function (source) {
if (minimatch(source, pattern) && pathToMap[source]) {
return {
map: pathToMap[source]
};
}
return null;
}
});
var espowerOptions = extend({ sourceRoot: options.cwd }, options.espowerOptions);
extensions['.js'] = function (localModule, filepath) {
var output;
if (minimatch(filepath, pattern)){
output = espowerSourceToSource(fs.readFileSync(filepath, 'utf-8'), filepath, espowerOptions);
var map = convert.fromSource(output).toObject();
pathToMap[filepath] = map;
localModule._compile(output, filepath);
} else {
originalLoader(localModule, filepath);
}
};
}
module.exports = espowerLoader;