|
| 1 | +/* |
| 2 | + * (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +/* |
| 17 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 18 | + Author Tobias Koppers @sokra |
| 19 | +*/ |
| 20 | +"use strict"; |
| 21 | + |
| 22 | +const RequireHeaderDependency = require("webpack/lib/dependencies/RequireHeaderDependency"); |
| 23 | +const LocalModuleDependency = require("webpack/lib/dependencies/LocalModuleDependency"); |
| 24 | +const LocalModulesHelpers = require("webpack/lib/dependencies/LocalModulesHelpers"); |
| 25 | + |
| 26 | +module.exports = class CJSRequireDependencyParserPlugin { |
| 27 | + apply(parser) { |
| 28 | + parser.plugin("call cjsRequire", (expr) => { |
| 29 | + if(expr.arguments.length !== 1) return; |
| 30 | + let localModule; |
| 31 | + const param = parser.evaluateExpression(expr.arguments[0]); |
| 32 | + if(param.isString() && (localModule = LocalModulesHelpers.getLocalModule(parser.state, param.string))) { |
| 33 | + const dep = new LocalModuleDependency(localModule, expr.range); |
| 34 | + dep.loc = expr.loc; |
| 35 | + parser.state.current.addDependency(dep); |
| 36 | + return true; |
| 37 | + } else { |
| 38 | + const result = parser.applyPluginsBailResult("call require:commonjs:item", expr, param); |
| 39 | + if(result === undefined) { |
| 40 | + parser.applyPluginsBailResult("call require:commonjs:context", expr, param); |
| 41 | + } else { |
| 42 | + const dep = new RequireHeaderDependency(expr.callee.range); |
| 43 | + dep.loc = expr.loc; |
| 44 | + parser.state.current.addDependency(dep); |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | +}; |
0 commit comments