Skip to content

Commit f024687

Browse files
committed
Refactor ContextDependency to take an options object
merged with options object from async info
1 parent 9075637 commit f024687

11 files changed

Lines changed: 55 additions & 91 deletions

‎lib/CompatibilityPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CompatibilityPlugin {
3333
dep.loc = expr.loc;
3434
if(parser.state.current.dependencies.length > 1) {
3535
const last = parser.state.current.dependencies[parser.state.current.dependencies.length - 1];
36-
if(last.critical && last.request === "." && last.userRequest === "." && last.recursive)
36+
if(last.critical && last.options && last.options.request === "." && last.userRequest === "." && last.options.recursive)
3737
parser.state.current.dependencies.pop();
3838
}
3939
parser.state.current.addDependency(dep);

‎lib/ContextModuleFactory.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ module.exports = class ContextModuleFactory extends Tapable {
2323
const dependency = dependencies[0];
2424
this.applyPluginsAsyncWaterfall("before-resolve", Object.assign({
2525
context: context,
26-
request: dependency.request,
27-
recursive: dependency.recursive,
28-
regExp: dependency.regExp,
2926
dependencies: dependencies
3027
}, dependency.options), (err, beforeResolveResult) => {
3128
if(err) return callback(err);

‎lib/dependencies/AMDRequireContextDependency.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
const ContextDependency = require("./ContextDependency");
88
class AMDRequireContextDependency extends ContextDependency {
9-
constructor(request, recursive, regExp, range, valueRange) {
10-
super(request, recursive, regExp);
9+
constructor(options, range, valueRange) {
10+
super(options);
1111
this.range = range;
1212
this.valueRange = valueRange;
1313
}

‎lib/dependencies/CommonJsRequireContextDependency.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const ContextDependency = require("./ContextDependency");
77
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
88

99
class CommonJsRequireContextDependency extends ContextDependency {
10-
constructor(request, recursive, regExp, range, valueRange) {
11-
super(request, recursive, regExp);
10+
constructor(options, range, valueRange) {
11+
super(options);
1212
this.range = range;
1313
this.valueRange = valueRange;
1414
}

‎lib/dependencies/CommonJsRequireDependencyParserPlugin.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ class CommonJsRequireDependencyParserPlugin {
2121
const options = this.options;
2222
parser.plugin("expression require.cache", ParserHelpers.toConstantDependency("__webpack_require__.c"));
2323
parser.plugin("expression require", (expr) => {
24-
const dep = new CommonJsRequireContextDependency(options.unknownContextRequest, options.unknownContextRecursive, options.unknownContextRegExp, expr.range);
24+
const dep = new CommonJsRequireContextDependency({
25+
request: options.unknownContextRequest,
26+
recursive: options.unknownContextRecursive,
27+
regExp: options.unknownContextRegExp
28+
}, expr.range);
2529
dep.critical = options.unknownContextCritical && "require function is used in a way in which dependencies cannot be statically extracted";
2630
dep.loc = expr.loc;
2731
dep.optional = !!parser.scope.inTry;

‎lib/dependencies/ContextDependency.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@
66
const Dependency = require("../Dependency");
77
const CriticalDependencyWarning = require("./CriticalDependencyWarning");
88

9+
function equalRegExp(a, b) {
10+
if(a === b) return true;
11+
if(typeof a !== "object" || typeof b !== "object") return false;
12+
return a + "" === b + "";
13+
}
14+
915
class ContextDependency extends Dependency {
10-
constructor(request, recursive, regExp) {
16+
// options: { request, recursive, regExp, include, exclude, async, chunkName }
17+
constructor(options) {
1118
super();
12-
this.request = request;
13-
this.userRequest = request;
14-
this.recursive = recursive;
15-
this.regExp = regExp;
16-
19+
this.options = options;
20+
this.userRequest = this.options.request;
1721
this.hadGlobalOrStickyRegExp = false;
18-
if(this.regExp.global || this.regExp.sticky) {
19-
this.regExp = null;
22+
if(this.options.regExp.global || this.options.regExp.sticky) {
23+
this.options.regExp = null;
2024
this.hadGlobalOrStickyRegExp = true;
2125
}
22-
2326
}
2427

2528
isEqualResource(other) {
2629
if(!(other instanceof ContextDependency))
2730
return false;
2831

29-
return this.request === other.request &&
30-
this.recursive === other.recursive &&
31-
this.regExp === other.regExp;
32+
return this.options.request === other.options.request &&
33+
this.options.recursive === other.options.recursive &&
34+
equalRegExp(this.options.regExp, other.options.regExp) &&
35+
equalRegExp(this.options.include, other.options.include) &&
36+
equalRegExp(this.options.exclude, other.options.exclude) &&
37+
this.options.async === other.options.async &&
38+
this.options.chunkName === other.options.chunkName;
3239
}
3340

3441
getWarnings() {
@@ -43,7 +50,6 @@ class ContextDependency extends Dependency {
4350
}
4451
}
4552

46-
4753
Object.defineProperty(ContextDependency.prototype, "async", {
4854
configurable: false,
4955
get() {

‎lib/dependencies/ContextDependencyHelpers.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ ContextDependencyHelpers.create = function(Dep, range, param, expr, options, con
3737
}
3838
// If there are more than two quasis, maybe the generated RegExp can be more precise?
3939
regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
40-
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, contextOptions);
40+
dep = new Dep(Object.assign({
41+
request: context,
42+
recursive: options.wrappedContextRecursive,
43+
regExp
44+
}, contextOptions), range, valueRange);
4145
dep.loc = expr.loc;
4246
dep.replaces = [{
4347
range: prefixRange,
@@ -57,13 +61,21 @@ ContextDependencyHelpers.create = function(Dep, range, param, expr, options, con
5761
prefix = `.${prefix.substr(idx)}`;
5862
}
5963
regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
60-
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, contextOptions);
64+
dep = new Dep(Object.assign({
65+
request: context,
66+
recursive: options.wrappedContextRecursive,
67+
regExp
68+
}, contextOptions), range, valueRange);
6169
dep.loc = expr.loc;
6270
dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;
6371
dep.critical = options.wrappedContextCritical && "a part of the request of a dependency is an expression";
6472
return dep;
6573
} else {
66-
dep = new Dep(options.exprContextRequest, options.exprContextRecursive, options.exprContextRegExp, range, param.range, contextOptions);
74+
dep = new Dep(Object.assign({
75+
request: options.exprContextRequest,
76+
recursive: options.exprContextRecursive,
77+
regExp: options.exprContextRegExp
78+
}, contextOptions), range, param.range);
6779
dep.loc = expr.loc;
6880
dep.critical = options.exprContextCritical && "the request of a dependency is an expression";
6981
return dep;

‎lib/dependencies/ImportContextDependency.js‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,11 @@
66
const ContextDependency = require("./ContextDependency");
77
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
88

9-
function equalRegExp(a, b) {
10-
if(a === b) return true;
11-
if(typeof a !== "object" || typeof b !== "object") return false;
12-
return a + "" === b + "";
13-
}
14-
159
class ImportContextDependency extends ContextDependency {
16-
constructor(request, recursive, regExp, range, valueRange, options) {
17-
super(request, recursive, regExp);
10+
constructor(options, range, valueRange) {
11+
super(options);
1812
this.range = range;
1913
this.valueRange = valueRange;
20-
this.options = options;
21-
}
22-
23-
isEqualResource(other) {
24-
if(!super.isEqualResource(other)) return false;
25-
26-
if(!(other instanceof ImportContextDependency))
27-
return false;
28-
29-
return this.options.async === other.options.async &&
30-
this.options.chunkName === other.options.chunkName &&
31-
equalRegExp(this.options.include, other.options.include) &&
32-
equalRegExp(this.options.exclude, other.options.exclude);
3314
}
3415

3516
get type() {

‎lib/dependencies/RequireContextDependency.js‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,10 @@
66
const ContextDependency = require("./ContextDependency");
77
const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId");
88

9-
function equalRegExp(a, b) {
10-
if(a === b) return true;
11-
if(typeof a !== "object" || typeof b !== "object") return false;
12-
return a + "" === b + "";
13-
}
14-
159
class RequireContextDependency extends ContextDependency {
16-
constructor(request, recursive, regExp, range, options) {
17-
super(request, recursive, regExp);
10+
constructor(options, range) {
11+
super(options);
1812
this.range = range;
19-
20-
this.options = options;
21-
}
22-
23-
isEqualResource(other) {
24-
if(!super.isEqualResource(other)) return false;
25-
26-
if(!(other instanceof RequireContextDependency))
27-
return false;
28-
29-
return this.options.async === other.options.async &&
30-
this.options.chunkName === other.options.chunkName &&
31-
equalRegExp(this.options.include, other.options.include) &&
32-
equalRegExp(this.options.exclude, other.options.exclude);
3313
}
3414

3515
get type() {

‎lib/dependencies/RequireContextDependencyParserPlugin.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ module.exports = class RequireContextDependencyParserPlugin {
3838
{
3939
const requestExpr = parser.evaluateExpression(expr.arguments[0]);
4040
if(!requestExpr.isString()) return;
41-
const dep = new RequireContextDependency(requestExpr.string, recursive, regExp, expr.range, {
41+
const dep = new RequireContextDependency({
42+
request: requestExpr.string,
43+
recursive,
44+
regExp,
4245
async: asyncMode
43-
});
46+
}, expr.range);
4447
dep.loc = expr.loc;
4548
dep.optional = parser.scope.inTry;
4649
parser.state.current.addDependency(dep);

0 commit comments

Comments
 (0)