aboutsummaryrefslogtreecommitdiff
path: root/node_modules/enhanced-resolve/lib/AliasPlugin.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
commitde98e0b232509d5f40c135d540a70e415272ff85 (patch)
treea79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/enhanced-resolve/lib/AliasPlugin.js
parente0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff)
node_modules
Diffstat (limited to 'node_modules/enhanced-resolve/lib/AliasPlugin.js')
-rw-r--r--node_modules/enhanced-resolve/lib/AliasPlugin.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/node_modules/enhanced-resolve/lib/AliasPlugin.js b/node_modules/enhanced-resolve/lib/AliasPlugin.js
new file mode 100644
index 000000000..86f2a6fba
--- /dev/null
+++ b/node_modules/enhanced-resolve/lib/AliasPlugin.js
@@ -0,0 +1,57 @@
+/*
+ MIT License http://www.opensource.org/licenses/mit-license.php
+ Author Tobias Koppers @sokra
+*/
+var createInnerCallback = require("./createInnerCallback");
+
+function startsWith(string, searchString) {
+ var stringLength = string.length;
+ var searchLength = searchString.length;
+
+ // early out if the search length is greater than the search string
+ if(searchLength > stringLength) {
+ return false;
+ }
+ var index = -1;
+ while(++index < searchLength) {
+ if(string.charCodeAt(index) !== searchString.charCodeAt(index)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+function AliasPlugin(source, options, target) {
+ this.source = source;
+ this.name = options.name;
+ this.alias = options.alias;
+ this.onlyModule = options.onlyModule;
+ this.target = target;
+}
+module.exports = AliasPlugin;
+
+AliasPlugin.prototype.apply = function(resolver) {
+ var target = this.target;
+ var name = this.name;
+ var alias = this.alias;
+ var onlyModule = this.onlyModule;
+ resolver.plugin(this.source, function(request, callback) {
+ var innerRequest = request.request;
+ if(!innerRequest) return callback();
+ if(innerRequest === name || (!onlyModule && startsWith(innerRequest, name + "/"))) {
+ if(innerRequest !== alias && !startsWith(innerRequest, alias + "/")) {
+ var newRequestStr = alias + innerRequest.substr(name.length);
+ var obj = Object.assign({}, request, {
+ request: newRequestStr
+ });
+ return resolver.doResolve(target, obj, "aliased with mapping '" + name + "': '" + alias + "' to '" + newRequestStr + "'", createInnerCallback(function(err, result) {
+ if(arguments.length > 0) return callback(err, result);
+
+ // don't allow other aliasing or raw request
+ callback(null, null);
+ }, callback));
+ }
+ }
+ return callback();
+ });
+};