webpack/lib/dependencies/ContextDependencyHelpers.js

69 lines
2.6 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var ContextDependencyHelpers = exports;
2016-11-17 20:43:14 +08:00
/**
* Escapes regular expression metacharacters
2016-11-21 22:38:56 +08:00
* @param {string} str String to quote
* @return {string} Escaped string
2016-11-17 20:43:14 +08:00
*/
function quotemeta(str) {
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&")
}
ContextDependencyHelpers.create = function(Dep, range, param, expr, options) {
2016-11-15 21:03:53 +08:00
var dep, prefix, postfix, prefixRange, valueRange, idx, context, regExp;
if(param.isTemplateString()) {
prefix = param.quasis[0].string;
postfix = param.quasis.length > 1 ? param.quasis[param.quasis.length - 1].string : "";
prefixRange = [param.quasis[0].range[0], param.quasis[0].range[1]];
valueRange = param.range;
idx = prefix.lastIndexOf("/");
context = ".";
if(idx >= 0) {
2013-01-31 01:49:25 +08:00
context = prefix.substr(0, idx);
prefix = "." + prefix.substr(idx);
}
2016-11-15 21:03:53 +08:00
// If there are more than two quasis, maybe the generated RegExp can be more precise?
regExp = new RegExp("^" +
2016-11-17 20:43:14 +08:00
quotemeta(prefix) +
2016-11-15 21:03:53 +08:00
options.wrappedContextRegExp.source +
2016-11-17 20:43:14 +08:00
quotemeta(postfix) + "$");
2016-11-15 21:03:53 +08:00
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange);
dep.loc = expr.loc;
dep.replaces = [{
range: prefixRange,
value: prefix
}];
dep.critical = options.wrappedContextCritical && "a part of the request of a dependency is an expression";
return dep;
} else if(param.isWrapped() && (param.prefix && param.prefix.isString() || param.postfix && param.postfix.isString())) {
prefix = param.prefix && param.prefix.isString() ? param.prefix.string : "";
postfix = param.postfix && param.postfix.isString() ? param.postfix.string : "";
prefixRange = param.prefix && param.prefix.isString() ? param.prefix.range : null;
valueRange = [prefixRange ? prefixRange[1] : param.range[0], param.range[1]];
idx = prefix.lastIndexOf("/");
context = ".";
if(idx >= 0) {
context = prefix.substr(0, idx);
prefix = "." + prefix.substr(idx);
}
regExp = new RegExp("^" +
2016-11-17 20:43:14 +08:00
quotemeta(prefix) +
options.wrappedContextRegExp.source +
2016-11-17 20:43:14 +08:00
quotemeta(postfix) + "$");
2016-01-19 08:52:28 +08:00
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange);
2013-02-13 21:42:34 +08:00
dep.loc = expr.loc;
dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;
dep.critical = options.wrappedContextCritical && "a part of the request of a dependency is an expression";
2013-01-31 01:49:25 +08:00
return dep;
} else {
2016-01-19 08:52:28 +08:00
dep = new Dep(options.exprContextRequest, options.exprContextRecursive, options.exprContextRegExp, range, param.range);
2013-02-13 21:42:34 +08:00
dep.loc = expr.loc;
dep.critical = options.exprContextCritical && "the request of a dependency is an expression";
2013-02-13 21:42:34 +08:00
return dep;
2013-01-31 01:49:25 +08:00
}
};