-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (39 loc) · 1.14 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
"use strict"
var path = require('path')
var defaults = require('defaults')
var Module = require('module')
module.exports = mapExtension
function mapExtension(directory) {
var _compile = Module.prototype._compile
Module.prototype._compile = overriddenCompile
var mappings = []
var beforeMappings = []
var afterMappings = []
function overriddenCompile(content, filename) {
var fns = beforeMappings.concat(mappings).concat(afterMappings)
content = fns.reduce(function(content, map) {
if (!doMapping(directory, filename)) return content
return map(content)
}, content)
return _compile.call(this, content, filename)
}
function map(fn) {
mappings.push(fn)
}
map.before = function before(fn) {
beforeMappings.push(fn)
}
map.after = function after(fn) {
afterMappings.push(fn)
}
return map
}
function doMapping(directory, filename) {
if (!directory) return true
// ignore if node_modules is anywhere in the path between requirer
// and requiree
return !(
/node_modules\//.test(path.relative(filename, directory)) ||
/node_modules\//.test(path.relative(directory, filename))
)
}