2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var ConstDependency = require("./dependencies/ConstDependency");
|
2013-02-17 05:23:22 +08:00
|
|
|
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
var NullFactory = require("./NullFactory");
|
|
|
|
|
2015-07-08 20:39:02 +08:00
|
|
|
function ConstPlugin() {}
|
2013-01-31 01:49:25 +08:00
|
|
|
module.exports = ConstPlugin;
|
|
|
|
|
|
|
|
ConstPlugin.prototype.apply = function(compiler) {
|
2015-04-24 05:55:50 +08:00
|
|
|
compiler.plugin("compilation", function(compilation) {
|
2013-01-31 01:49:25 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
|
|
|
});
|
|
|
|
compiler.parser.plugin("statement if", function(statement) {
|
|
|
|
var param = this.evaluateExpression(statement.test);
|
2013-09-13 17:17:57 +08:00
|
|
|
var bool = param.asBool();
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof bool === "boolean") {
|
|
|
|
if(statement.test.type !== "Literal") {
|
2015-05-11 00:31:58 +08:00
|
|
|
var dep = new ConstDependency(bool + "", param.range);
|
|
|
|
dep.loc = statement.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
}
|
2013-09-13 17:17:57 +08:00
|
|
|
return bool;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
compiler.parser.plugin("expression ?:", function(expression) {
|
|
|
|
var param = this.evaluateExpression(expression.test);
|
|
|
|
var bool = param.asBool();
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof bool === "boolean") {
|
|
|
|
if(expression.test.type !== "Literal") {
|
2015-07-23 05:40:46 +08:00
|
|
|
var dep = new ConstDependency(" " + bool + "", param.range);
|
2015-05-11 00:31:58 +08:00
|
|
|
dep.loc = expression.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
}
|
2013-09-13 17:17:57 +08:00
|
|
|
return bool;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
});
|
2013-02-17 05:23:22 +08:00
|
|
|
compiler.parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(!this.state.module) return;
|
2013-02-17 05:23:22 +08:00
|
|
|
var res = new BasicEvaluatedExpression();
|
|
|
|
res.setString(this.state.module.splitQuery(this.state.module.resource)[1]);
|
|
|
|
res.setRange(expr.range);
|
|
|
|
return res;
|
|
|
|
});
|
2015-04-24 05:55:50 +08:00
|
|
|
compiler.parser.plugin("expression __resourceQuery", function() {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(!this.state.module) return;
|
2013-02-17 17:30:47 +08:00
|
|
|
this.state.current.addVariable("__resourceQuery", JSON.stringify(this.state.module.splitQuery(this.state.module.resource)[1]));
|
2013-02-17 05:23:22 +08:00
|
|
|
return true;
|
|
|
|
});
|
2015-07-08 20:39:02 +08:00
|
|
|
};
|