|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | +*/ |
| 5 | +var AbstractPlugin = require("../AbstractPlugin"); |
| 6 | +var ConstDependency = require("./ConstDependency"); |
| 7 | +var LabeledModuleDependency = require("./LabeledModuleDependency"); |
| 8 | +var LabeledExportsDependency = require("./LabeledExportsDependency"); |
| 9 | + |
| 10 | +module.exports = AbstractPlugin.create({ |
| 11 | + "label require": function(stmt) { |
| 12 | + if(stmt.body.type != "ExpressionStatement") return; |
| 13 | + switch(stmt.body.expression.type) { |
| 14 | + case "Literal": |
| 15 | + var param = this.evaluateExpression(stmt.body.expression); |
| 16 | + return this.applyPluginsBailResult("label require:item", stmt, param); |
| 17 | + case "SequenceExpression": |
| 18 | + stmt.body.expressions.forEach(function(expression) { |
| 19 | + var param = this.evaluateExpression(stmt.body.expression); |
| 20 | + return this.applyPluginsBailResult("label require:item", stmt, param); |
| 21 | + }, this); |
| 22 | + return true; |
| 23 | + } |
| 24 | + }, |
| 25 | + "label require:item": function(stmt, param) { |
| 26 | + if(param.isString()) { |
| 27 | + var dep = new LabeledModuleDependency(param.string, stmt.range); |
| 28 | + dep.loc = stmt.loc; |
| 29 | + dep.optional = !!this.scope.inTry; |
| 30 | + this.state.current.addDependency(dep); |
| 31 | + return true; |
| 32 | + } |
| 33 | + }, |
| 34 | + "label exports": function(stmt) { |
| 35 | + switch(stmt.body.type) { |
| 36 | + case "VariableDeclaration": |
| 37 | + stmt.body.declarations.forEach(function(decl) { |
| 38 | + if(!decl.init) return; |
| 39 | + var dep = new LabeledExportsDependency(decl.id.name, decl.init.range[0]); |
| 40 | + dep.loc = stmt.loc; |
| 41 | + this.state.current.addDependency(dep); |
| 42 | + if(!this.state.module.meta.exports) this.state.module.meta.exports = []; |
| 43 | + this.state.module.meta.exports.push(decl.id.name); |
| 44 | + }, this); |
| 45 | + return true; |
| 46 | + case "FunctionDeclaration": |
| 47 | + var name = stmt.body.id.name; |
| 48 | + var dep = new LabeledExportsDependency(name, stmt.body.range[0]); |
| 49 | + dep.loc = stmt.loc; |
| 50 | + this.state.current.addDependency(dep); |
| 51 | + if(!this.state.module.meta.exports) this.state.module.meta.exports = []; |
| 52 | + this.state.module.meta.exports.push(name); |
| 53 | + return true; |
| 54 | + case "ExpressionStatement": |
| 55 | + if(stmt.body.expression.type == "Identifier") { |
| 56 | + var name = stmt.body.expression.name; |
| 57 | + var dep = new LabeledExportsDependency(name, stmt.body.expression.range[0]); |
| 58 | + dep.loc = stmt.loc; |
| 59 | + this.state.current.addDependency(dep); |
| 60 | + if(!this.state.module.meta.exports) this.state.module.meta.exports = []; |
| 61 | + this.state.module.meta.exports.push(name); |
| 62 | + return true; |
| 63 | + } else if(stmt.body.expression.type == "SequenceExpression") { |
| 64 | + stmt.body.expression.expressions.forEach(function(e) { |
| 65 | + if(e.type != "Identifier") return; |
| 66 | + var name = e.name; |
| 67 | + var dep = new LabeledExportsDependency(name, e.range[0]); |
| 68 | + dep.loc = stmt.loc; |
| 69 | + this.state.current.addDependency(dep); |
| 70 | + if(!this.state.module.meta.exports) this.state.module.meta.exports = []; |
| 71 | + this.state.module.meta.exports.push(name); |
| 72 | + }, this); |
| 73 | + return true; |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | +}); |
| 78 | + |
0 commit comments