AliasFieldPlugin.js
1.8 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
49
50
51
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var DescriptionFileUtils = require("./DescriptionFileUtils");
var createInnerCallback = require("./createInnerCallback");
var getInnerRequest = require("./getInnerRequest");
function AliasFieldPlugin(source, field, target) {
this.source = source;
this.field = field;
this.target = target;
}
module.exports = AliasFieldPlugin;
AliasFieldPlugin.prototype.apply = function(resolver) {
var target = this.target;
var field = this.field;
resolver.plugin(this.source, function(request, callback) {
if(!request.descriptionFileData) return callback();
var innerRequest = getInnerRequest(resolver, request);
if(!innerRequest) return callback();
var fieldData = DescriptionFileUtils.getField(request.descriptionFileData, field);
if(typeof fieldData !== "object") {
if(callback.log) callback.log("Field '" + field + "' doesn't contain a valid alias configuration");
return callback();
}
var data1 = fieldData[innerRequest];
var data2 = fieldData[innerRequest.replace(/^\.\//, "")];
var data = typeof data1 !== "undefined" ? data1 : data2;
if(data === innerRequest) return callback();
if(data === undefined) return callback();
if(data === false) {
var ignoreObj = Object.assign({}, request, {
path: false
});
return callback(null, ignoreObj);
}
var obj = Object.assign({}, request, {
path: request.descriptionFileRoot,
request: data
});
resolver.doResolve(target, obj, "aliased from description file " + request.descriptionFilePath + " with mapping '" + innerRequest + "' to '" + data + "'", createInnerCallback(function(err, result) {
if(arguments.length > 0) return callback(err, result);
// Don't allow other aliasing or raw request
callback(null, null);
}, callback));
});
};