2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-04-05 21:00:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const ContextDependencyHelpers = exports;
|
2013-01-31 01:49:25 +08:00
|
|
|
|
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
|
2018-05-08 20:31:51 +08:00
|
|
|
* @returns {string} Escaped string
|
2016-11-17 20:43:14 +08:00
|
|
|
*/
|
2017-11-08 18:32:05 +08:00
|
|
|
const quotemeta = str => {
|
2017-01-11 17:51:58 +08:00
|
|
|
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2016-11-17 20:43:14 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
ContextDependencyHelpers.create = (
|
|
|
|
Dep,
|
|
|
|
range,
|
|
|
|
param,
|
|
|
|
expr,
|
|
|
|
options,
|
|
|
|
contextOptions
|
|
|
|
) => {
|
2017-04-05 21:00:50 +08:00
|
|
|
let dep;
|
|
|
|
let prefix;
|
|
|
|
let postfix;
|
|
|
|
let prefixRange;
|
|
|
|
let valueRange;
|
|
|
|
let idx;
|
|
|
|
let context;
|
|
|
|
let regExp;
|
2018-02-25 09:00:20 +08:00
|
|
|
if (param.isTemplateString()) {
|
2016-11-15 21:03:53 +08:00
|
|
|
prefix = param.quasis[0].string;
|
2018-02-25 09:00:20 +08:00
|
|
|
postfix =
|
|
|
|
param.quasis.length > 1
|
|
|
|
? param.quasis[param.quasis.length - 1].string
|
|
|
|
: "";
|
2016-11-15 21:03:53 +08:00
|
|
|
prefixRange = [param.quasis[0].range[0], param.quasis[0].range[1]];
|
|
|
|
valueRange = param.range;
|
|
|
|
idx = prefix.lastIndexOf("/");
|
|
|
|
context = ".";
|
2018-02-25 09:00:20 +08:00
|
|
|
if (idx >= 0) {
|
2013-01-31 01:49:25 +08:00
|
|
|
context = prefix.substr(0, idx);
|
2017-04-05 21:00:50 +08:00
|
|
|
prefix = `.${prefix.substr(idx)}`;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2016-11-15 21:03:53 +08:00
|
|
|
// If there are more than two quasis, maybe the generated RegExp can be more precise?
|
2018-02-25 09:00:20 +08:00
|
|
|
regExp = new RegExp(
|
|
|
|
`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(
|
|
|
|
postfix
|
|
|
|
)}$`
|
|
|
|
);
|
|
|
|
dep = new Dep(
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
request: context,
|
|
|
|
recursive: options.wrappedContextRecursive,
|
|
|
|
regExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
valueRange
|
|
|
|
);
|
2016-11-15 21:03:53 +08:00
|
|
|
dep.loc = expr.loc;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.replaces = [
|
|
|
|
{
|
|
|
|
range: prefixRange,
|
|
|
|
value: prefix
|
|
|
|
}
|
|
|
|
];
|
|
|
|
dep.critical =
|
|
|
|
options.wrappedContextCritical &&
|
|
|
|
"a part of the request of a dependency is an expression";
|
2016-11-15 21:03:53 +08:00
|
|
|
return dep;
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (
|
|
|
|
param.isWrapped() &&
|
|
|
|
((param.prefix && param.prefix.isString()) ||
|
|
|
|
(param.postfix && param.postfix.isString()))
|
|
|
|
) {
|
2016-11-15 21:03:53 +08:00
|
|
|
prefix = param.prefix && param.prefix.isString() ? param.prefix.string : "";
|
2018-02-25 09:00:20 +08:00
|
|
|
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]
|
|
|
|
];
|
2016-11-15 21:03:53 +08:00
|
|
|
idx = prefix.lastIndexOf("/");
|
|
|
|
context = ".";
|
2018-02-25 09:00:20 +08:00
|
|
|
if (idx >= 0) {
|
2016-11-15 21:03:53 +08:00
|
|
|
context = prefix.substr(0, idx);
|
2017-04-05 21:00:50 +08:00
|
|
|
prefix = `.${prefix.substr(idx)}`;
|
2016-11-15 21:03:53 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
regExp = new RegExp(
|
|
|
|
`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(
|
|
|
|
postfix
|
|
|
|
)}$`
|
|
|
|
);
|
|
|
|
dep = new Dep(
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
request: context,
|
|
|
|
recursive: options.wrappedContextRecursive,
|
|
|
|
regExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
valueRange
|
|
|
|
);
|
2013-02-13 21:42:34 +08:00
|
|
|
dep.loc = expr.loc;
|
2015-02-28 07:51:15 +08:00
|
|
|
dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;
|
2018-02-25 09:00:20 +08:00
|
|
|
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 {
|
2018-02-25 09:00:20 +08:00
|
|
|
dep = new Dep(
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
request: options.exprContextRequest,
|
|
|
|
recursive: options.exprContextRecursive,
|
|
|
|
regExp: options.exprContextRegExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
param.range
|
|
|
|
);
|
2013-02-13 21:42:34 +08:00
|
|
|
dep.loc = expr.loc;
|
2018-02-25 09:00:20 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|