-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
48 lines (36 loc) · 1.49 KB
/
plugin.js
File metadata and controls
48 lines (36 loc) · 1.49 KB
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
var path = require('path'),
fs = require('fs'),
_ = require('lodash');
function InheritancePlugin(chain) {
this.chain = chain.map(function(src) {
return path.resolve(src);
}).reverse();
}
InheritancePlugin.prototype.apply = function(compiler) {
var chain = this.chain;
compiler.plugin('normal-module-factory', function(nmf) {
nmf.plugin('before-resolve', function(result, callback) {
var currentPath = _.find(chain, function (p) {
return result.context.indexOf(p) != -1;
}) || result.context;
if (!/!$/.test(result.request)) {
_.find(chain, function(p) {
var newContext = result.context.replace(currentPath, p),
filePath = path.join(newContext, result.request);
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile() ||
fs.existsSync(filePath + '.js') ||
fs.existsSync(path.join(filePath, 'index.js'))) {
result.context = newContext;
return true;
}
return false;
});
} else {
result.context = result.context.replace(currentPath, chain[chain.indexOf(currentPath) + 1]);
result.request = result.request.replace(/!$/, '');
}
return callback(null, result);
});
});
};
module.exports = InheritancePlugin;