AMDRequireDependenciesBlockParserPlugin.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
  8. const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
  9. const AMDRequireContextDependency = require("./AMDRequireContextDependency");
  10. const AMDRequireDependenciesBlock = require("./AMDRequireDependenciesBlock");
  11. const AMDRequireDependency = require("./AMDRequireDependency");
  12. const AMDRequireItemDependency = require("./AMDRequireItemDependency");
  13. const ConstDependency = require("./ConstDependency");
  14. const ContextDependencyHelpers = require("./ContextDependencyHelpers");
  15. const LocalModuleDependency = require("./LocalModuleDependency");
  16. const { getLocalModule } = require("./LocalModulesHelpers");
  17. const UnsupportedDependency = require("./UnsupportedDependency");
  18. const getFunctionExpression = require("./getFunctionExpression");
  19. class AMDRequireDependenciesBlockParserPlugin {
  20. constructor(options) {
  21. this.options = options;
  22. }
  23. processFunctionArgument(parser, expression) {
  24. let bindThis = true;
  25. const fnData = getFunctionExpression(expression);
  26. if (fnData) {
  27. parser.inScope(
  28. fnData.fn.params.filter(i => {
  29. return !["require", "module", "exports"].includes(i.name);
  30. }),
  31. () => {
  32. if (fnData.fn.body.type === "BlockStatement") {
  33. parser.walkStatement(fnData.fn.body);
  34. } else {
  35. parser.walkExpression(fnData.fn.body);
  36. }
  37. }
  38. );
  39. parser.walkExpressions(fnData.expressions);
  40. if (fnData.needThis === false) {
  41. bindThis = false;
  42. }
  43. } else {
  44. parser.walkExpression(expression);
  45. }
  46. return bindThis;
  47. }
  48. apply(parser) {
  49. parser.hooks.call
  50. .for("require")
  51. .tap(
  52. "AMDRequireDependenciesBlockParserPlugin",
  53. this.processCallRequire.bind(this, parser)
  54. );
  55. }
  56. processArray(parser, expr, param) {
  57. if (param.isArray()) {
  58. for (const p of param.items) {
  59. const result = this.processItem(parser, expr, p);
  60. if (result === undefined) {
  61. this.processContext(parser, expr, p);
  62. }
  63. }
  64. return true;
  65. } else if (param.isConstArray()) {
  66. const deps = [];
  67. for (const request of param.array) {
  68. let dep, localModule;
  69. if (request === "require") {
  70. dep = "__webpack_require__";
  71. } else if (["exports", "module"].includes(request)) {
  72. dep = request;
  73. } else if ((localModule = getLocalModule(parser.state, request))) {
  74. localModule.flagUsed();
  75. dep = new LocalModuleDependency(localModule, undefined, false);
  76. dep.loc = expr.loc;
  77. parser.state.module.addPresentationalDependency(dep);
  78. } else {
  79. dep = this.newRequireItemDependency(request);
  80. dep.loc = expr.loc;
  81. dep.optional = !!parser.scope.inTry;
  82. parser.state.current.addDependency(dep);
  83. }
  84. deps.push(dep);
  85. }
  86. const dep = this.newRequireArrayDependency(deps, param.range);
  87. dep.loc = expr.loc;
  88. dep.optional = !!parser.scope.inTry;
  89. parser.state.module.addPresentationalDependency(dep);
  90. return true;
  91. }
  92. }
  93. processItem(parser, expr, param) {
  94. if (param.isConditional()) {
  95. for (const p of param.options) {
  96. const result = this.processItem(parser, expr, p);
  97. if (result === undefined) {
  98. this.processContext(parser, expr, p);
  99. }
  100. }
  101. return true;
  102. } else if (param.isString()) {
  103. let dep, localModule;
  104. if (param.string === "require") {
  105. dep = new ConstDependency("__webpack_require__", param.string, [
  106. RuntimeGlobals.require
  107. ]);
  108. } else if (param.string === "module") {
  109. dep = new ConstDependency(
  110. parser.state.module.buildInfo.moduleArgument,
  111. param.range,
  112. [RuntimeGlobals.module]
  113. );
  114. } else if (param.string === "exports") {
  115. dep = new ConstDependency(
  116. parser.state.module.buildInfo.exportsArgument,
  117. param.range,
  118. [RuntimeGlobals.exports]
  119. );
  120. } else if ((localModule = getLocalModule(parser.state, param.string))) {
  121. localModule.flagUsed();
  122. dep = new LocalModuleDependency(localModule, param.range, false);
  123. } else {
  124. dep = this.newRequireItemDependency(param.string, param.range);
  125. dep.loc = expr.loc;
  126. dep.optional = !!parser.scope.inTry;
  127. parser.state.current.addDependency(dep);
  128. return true;
  129. }
  130. dep.loc = expr.loc;
  131. parser.state.module.addPresentationalDependency(dep);
  132. return true;
  133. }
  134. }
  135. processContext(parser, expr, param) {
  136. const dep = ContextDependencyHelpers.create(
  137. AMDRequireContextDependency,
  138. param.range,
  139. param,
  140. expr,
  141. this.options,
  142. {
  143. category: "amd"
  144. },
  145. parser
  146. );
  147. if (!dep) return;
  148. dep.loc = expr.loc;
  149. dep.optional = !!parser.scope.inTry;
  150. parser.state.current.addDependency(dep);
  151. return true;
  152. }
  153. processArrayForRequestString(param) {
  154. if (param.isArray()) {
  155. const result = param.items.map(item =>
  156. this.processItemForRequestString(item)
  157. );
  158. if (result.every(Boolean)) return result.join(" ");
  159. } else if (param.isConstArray()) {
  160. return param.array.join(" ");
  161. }
  162. }
  163. processItemForRequestString(param) {
  164. if (param.isConditional()) {
  165. const result = param.options.map(item =>
  166. this.processItemForRequestString(item)
  167. );
  168. if (result.every(Boolean)) return result.join("|");
  169. } else if (param.isString()) {
  170. return param.string;
  171. }
  172. }
  173. processCallRequire(parser, expr) {
  174. let param;
  175. let depBlock;
  176. let dep;
  177. let result;
  178. const old = parser.state.current;
  179. if (expr.arguments.length >= 1) {
  180. param = parser.evaluateExpression(expr.arguments[0]);
  181. depBlock = this.newRequireDependenciesBlock(
  182. expr.loc,
  183. this.processArrayForRequestString(param)
  184. );
  185. dep = this.newRequireDependency(
  186. expr.range,
  187. param.range,
  188. expr.arguments.length > 1 ? expr.arguments[1].range : null,
  189. expr.arguments.length > 2 ? expr.arguments[2].range : null
  190. );
  191. dep.loc = expr.loc;
  192. depBlock.addDependency(dep);
  193. parser.state.current = depBlock;
  194. }
  195. if (expr.arguments.length === 1) {
  196. parser.inScope([], () => {
  197. result = this.processArray(parser, expr, param);
  198. });
  199. parser.state.current = old;
  200. if (!result) return;
  201. parser.state.current.addBlock(depBlock);
  202. return true;
  203. }
  204. if (expr.arguments.length === 2 || expr.arguments.length === 3) {
  205. try {
  206. parser.inScope([], () => {
  207. result = this.processArray(parser, expr, param);
  208. });
  209. if (!result) {
  210. const dep = new UnsupportedDependency("unsupported", expr.range);
  211. old.addPresentationalDependency(dep);
  212. if (parser.state.module) {
  213. parser.state.module.addError(
  214. new UnsupportedFeatureWarning(
  215. "Cannot statically analyse 'require(…, …)' in line " +
  216. expr.loc.start.line,
  217. expr.loc
  218. )
  219. );
  220. }
  221. depBlock = null;
  222. return true;
  223. }
  224. dep.functionBindThis = this.processFunctionArgument(
  225. parser,
  226. expr.arguments[1]
  227. );
  228. if (expr.arguments.length === 3) {
  229. dep.errorCallbackBindThis = this.processFunctionArgument(
  230. parser,
  231. expr.arguments[2]
  232. );
  233. }
  234. } finally {
  235. parser.state.current = old;
  236. if (depBlock) parser.state.current.addBlock(depBlock);
  237. }
  238. return true;
  239. }
  240. }
  241. newRequireDependenciesBlock(loc, request) {
  242. return new AMDRequireDependenciesBlock(loc, request);
  243. }
  244. newRequireDependency(
  245. outerRange,
  246. arrayRange,
  247. functionRange,
  248. errorCallbackRange
  249. ) {
  250. return new AMDRequireDependency(
  251. outerRange,
  252. arrayRange,
  253. functionRange,
  254. errorCallbackRange
  255. );
  256. }
  257. newRequireItemDependency(request, range) {
  258. return new AMDRequireItemDependency(request, range);
  259. }
  260. newRequireArrayDependency(depsArray, range) {
  261. return new AMDRequireArrayDependency(depsArray, range);
  262. }
  263. }
  264. module.exports = AMDRequireDependenciesBlockParserPlugin;