diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/webpack/lib/ConstPlugin.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/webpack/lib/ConstPlugin.js')
-rw-r--r-- | node_modules/webpack/lib/ConstPlugin.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/node_modules/webpack/lib/ConstPlugin.js b/node_modules/webpack/lib/ConstPlugin.js new file mode 100644 index 000000000..8534a6fe8 --- /dev/null +++ b/node_modules/webpack/lib/ConstPlugin.js @@ -0,0 +1,60 @@ +/*
+ MIT License http://www.opensource.org/licenses/mit-license.php
+ Author Tobias Koppers @sokra
+*/
+"use strict";
+const ConstDependency = require("./dependencies/ConstDependency");
+const NullFactory = require("./NullFactory");
+const ParserHelpers = require("./ParserHelpers");
+
+const getQuery = (request) => {
+ const i = request.indexOf("?");
+ return request.indexOf("?") < 0 ? "" : request.substr(i);
+};
+
+class ConstPlugin {
+ apply(compiler) {
+ compiler.plugin("compilation", (compilation, params) => {
+ compilation.dependencyFactories.set(ConstDependency, new NullFactory());
+ compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
+
+ params.normalModuleFactory.plugin("parser", parser => {
+ parser.plugin("statement if", function(statement) {
+ const param = this.evaluateExpression(statement.test);
+ const bool = param.asBool();
+ if(typeof bool === "boolean") {
+ if(statement.test.type !== "Literal") {
+ const dep = new ConstDependency(`${bool}`, param.range);
+ dep.loc = statement.loc;
+ this.state.current.addDependency(dep);
+ }
+ return bool;
+ }
+ });
+ parser.plugin("expression ?:", function(expression) {
+ const param = this.evaluateExpression(expression.test);
+ const bool = param.asBool();
+ if(typeof bool === "boolean") {
+ if(expression.test.type !== "Literal") {
+ const dep = new ConstDependency(` ${bool}`, param.range);
+ dep.loc = expression.loc;
+ this.state.current.addDependency(dep);
+ }
+ return bool;
+ }
+ });
+ parser.plugin("evaluate Identifier __resourceQuery", function(expr) {
+ if(!this.state.module) return;
+ return ParserHelpers.evaluateToString(getQuery(this.state.module.resource))(expr);
+ });
+ parser.plugin("expression __resourceQuery", function() {
+ if(!this.state.module) return;
+ this.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(this.state.module.resource)));
+ return true;
+ });
+ });
+ });
+ }
+}
+
+module.exports = ConstPlugin;
|