webpack/lib/ConstPlugin.js

49 lines
1.8 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 ConstDependency = require("./dependencies/ConstDependency");
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
2013-01-31 01:49:25 +08:00
var NullFactory = require("./NullFactory");
function ConstPlugin() {
}
module.exports = ConstPlugin;
ConstPlugin.prototype.apply = function(compiler) {
compiler.plugin("compilation", function(compilation, params) {
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);
var bool = param.asBool();
if(typeof bool === "boolean") {
if(statement.test.type !== "Literal")
this.state.current.addDependency(new ConstDependency(bool + "", param.range));
return bool;
}
});
compiler.parser.plugin("expression ?:", function(expression) {
var param = this.evaluateExpression(expression.test);
var bool = param.asBool();
if(typeof bool === "boolean") {
if(expression.test.type !== "Literal")
this.state.current.addDependency(new ConstDependency(bool + "", param.range));
return bool;
2013-01-31 01:49:25 +08:00
}
});
compiler.parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
if(!this.state.module) return;
var res = new BasicEvaluatedExpression();
res.setString(this.state.module.splitQuery(this.state.module.resource)[1]);
res.setRange(expr.range);
return res;
});
compiler.parser.plugin("expression __resourceQuery", function(expr) {
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]));
return true;
});
2013-01-31 01:49:25 +08:00
};