2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-09 02:07:44 +08:00
|
|
|
"use strict";
|
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
const NullFactory = require("./NullFactory");
|
2017-01-09 17:30:12 +08:00
|
|
|
const ParserHelpers = require("./ParserHelpers");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-01-09 02:07:44 +08:00
|
|
|
const getQuery = (request) => {
|
|
|
|
const i = request.indexOf("?");
|
|
|
|
return request.indexOf("?") < 0 ? "" : request.substr(i);
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2015-12-30 23:39:10 +08:00
|
|
|
|
2017-01-09 02:07:44 +08:00
|
|
|
class ConstPlugin {
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.plugin("compilation", (compilation, params) => {
|
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2017-11-12 01:48:29 +08:00
|
|
|
params.normalModuleFactory.plugin(["parser javascript/auto", "parser javascript/dynamic", "parser javascript/esm"], parser => {
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.plugin("statement if", statement => {
|
|
|
|
const param = parser.evaluateExpression(statement.test);
|
2017-01-09 02:07:44 +08:00
|
|
|
const bool = param.asBool();
|
|
|
|
if(typeof bool === "boolean") {
|
|
|
|
if(statement.test.type !== "Literal") {
|
|
|
|
const dep = new ConstDependency(`${bool}`, param.range);
|
|
|
|
dep.loc = statement.loc;
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2017-01-09 02:07:44 +08:00
|
|
|
}
|
|
|
|
return bool;
|
2016-09-14 18:04:42 +08:00
|
|
|
}
|
2017-01-09 02:07:44 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.plugin("expression ?:", expression => {
|
|
|
|
const param = parser.evaluateExpression(expression.test);
|
2017-01-09 02:07:44 +08:00
|
|
|
const bool = param.asBool();
|
|
|
|
if(typeof bool === "boolean") {
|
|
|
|
if(expression.test.type !== "Literal") {
|
|
|
|
const dep = new ConstDependency(` ${bool}`, param.range);
|
|
|
|
dep.loc = expression.loc;
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2017-01-09 02:07:44 +08:00
|
|
|
}
|
|
|
|
return bool;
|
2016-09-14 18:04:42 +08:00
|
|
|
}
|
2017-01-09 02:07:44 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.plugin("evaluate Identifier __resourceQuery", expr => {
|
|
|
|
if(!parser.state.module) return;
|
|
|
|
return ParserHelpers.evaluateToString(getQuery(parser.state.module.resource))(expr);
|
2017-01-09 02:07:44 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
parser.plugin("expression __resourceQuery", () => {
|
|
|
|
if(!parser.state.module) return;
|
|
|
|
parser.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(parser.state.module.resource)));
|
2017-01-09 02:07:44 +08:00
|
|
|
return true;
|
|
|
|
});
|
2016-09-14 18:04:42 +08:00
|
|
|
});
|
|
|
|
});
|
2017-01-09 02:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ConstPlugin;
|