Skip to content

Commit ef8c6fb

Browse files
willmendesnetoTheLarkInn
authored andcommitted
refactor(ConstPlugin): upgrade to ES6 (webpack#3845)
1 parent bb8e28b commit ef8c6fb

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

lib/ConstPlugin.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,63 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var ConstDependency = require("./dependencies/ConstDependency");
6-
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
5+
"use strict";
76

8-
var NullFactory = require("./NullFactory");
7+
const ConstDependency = require("./dependencies/ConstDependency");
8+
const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
9+
const NullFactory = require("./NullFactory");
910

10-
function getQuery(request) {
11-
var i = request.indexOf("?");
12-
return i < 0 ? "" : request.substr(i);
11+
const getQuery = (request) => {
12+
const i = request.indexOf("?");
13+
return request.indexOf("?") < 0 ? "" : request.substr(i);
1314
}
1415

15-
function ConstPlugin() {}
16-
module.exports = ConstPlugin;
17-
18-
ConstPlugin.prototype.apply = function(compiler) {
19-
compiler.plugin("compilation", function(compilation, params) {
20-
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
21-
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
16+
class ConstPlugin {
17+
apply(compiler) {
18+
compiler.plugin("compilation", (compilation, params) => {
19+
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
20+
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
2221

23-
params.normalModuleFactory.plugin("parser", function(parser) {
24-
parser.plugin("statement if", function(statement) {
25-
var param = this.evaluateExpression(statement.test);
26-
var bool = param.asBool();
27-
if(typeof bool === "boolean") {
28-
if(statement.test.type !== "Literal") {
29-
var dep = new ConstDependency(bool + "", param.range);
30-
dep.loc = statement.loc;
31-
this.state.current.addDependency(dep);
22+
params.normalModuleFactory.plugin("parser", parser => {
23+
parser.plugin("statement if", function(statement) {
24+
const param = this.evaluateExpression(statement.test);
25+
const bool = param.asBool();
26+
if(typeof bool === "boolean") {
27+
if(statement.test.type !== "Literal") {
28+
const dep = new ConstDependency(`${bool}`, param.range);
29+
dep.loc = statement.loc;
30+
this.state.current.addDependency(dep);
31+
}
32+
return bool;
3233
}
33-
return bool;
34-
}
35-
});
36-
parser.plugin("expression ?:", function(expression) {
37-
var param = this.evaluateExpression(expression.test);
38-
var bool = param.asBool();
39-
if(typeof bool === "boolean") {
40-
if(expression.test.type !== "Literal") {
41-
var dep = new ConstDependency(" " + bool + "", param.range);
42-
dep.loc = expression.loc;
43-
this.state.current.addDependency(dep);
34+
});
35+
parser.plugin("expression ?:", function(expression) {
36+
const param = this.evaluateExpression(expression.test);
37+
const bool = param.asBool();
38+
if(typeof bool === "boolean") {
39+
if(expression.test.type !== "Literal") {
40+
const dep = new ConstDependency(` ${bool}`, param.range);
41+
dep.loc = expression.loc;
42+
this.state.current.addDependency(dep);
43+
}
44+
return bool;
4445
}
45-
return bool;
46-
}
47-
});
48-
parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
49-
if(!this.state.module) return;
50-
var res = new BasicEvaluatedExpression();
51-
res.setString(getQuery(this.state.module.resource));
52-
res.setRange(expr.range);
53-
return res;
54-
});
55-
parser.plugin("expression __resourceQuery", function() {
56-
if(!this.state.module) return;
57-
this.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(this.state.module.resource)));
58-
return true;
46+
});
47+
parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
48+
if(!this.state.module) return;
49+
const res = new BasicEvaluatedExpression();
50+
res.setString(getQuery(this.state.module.resource));
51+
res.setRange(expr.range);
52+
return res;
53+
});
54+
parser.plugin("expression __resourceQuery", function() {
55+
if(!this.state.module) return;
56+
this.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(this.state.module.resource)));
57+
return true;
58+
});
5959
});
6060
});
61-
});
62-
};
61+
}
62+
}
63+
64+
module.exports = ConstPlugin;

0 commit comments

Comments
 (0)