source: trip-planner-front/node_modules/webpack/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[6a3a178]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RequireEnsureDependenciesBlock = require("./RequireEnsureDependenciesBlock");
9const RequireEnsureDependency = require("./RequireEnsureDependency");
10const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
11const getFunctionExpression = require("./getFunctionExpression");
12
13module.exports = class RequireEnsureDependenciesBlockParserPlugin {
14 apply(parser) {
15 parser.hooks.call
16 .for("require.ensure")
17 .tap("RequireEnsureDependenciesBlockParserPlugin", expr => {
18 let chunkName = null;
19 let errorExpressionArg = null;
20 let errorExpression = null;
21 switch (expr.arguments.length) {
22 case 4: {
23 const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
24 if (!chunkNameExpr.isString()) return;
25 chunkName = chunkNameExpr.string;
26 }
27 // falls through
28 case 3: {
29 errorExpressionArg = expr.arguments[2];
30 errorExpression = getFunctionExpression(errorExpressionArg);
31
32 if (!errorExpression && !chunkName) {
33 const chunkNameExpr = parser.evaluateExpression(
34 expr.arguments[2]
35 );
36 if (!chunkNameExpr.isString()) return;
37 chunkName = chunkNameExpr.string;
38 }
39 }
40 // falls through
41 case 2: {
42 const dependenciesExpr = parser.evaluateExpression(
43 expr.arguments[0]
44 );
45 const dependenciesItems = dependenciesExpr.isArray()
46 ? dependenciesExpr.items
47 : [dependenciesExpr];
48 const successExpressionArg = expr.arguments[1];
49 const successExpression =
50 getFunctionExpression(successExpressionArg);
51
52 if (successExpression) {
53 parser.walkExpressions(successExpression.expressions);
54 }
55 if (errorExpression) {
56 parser.walkExpressions(errorExpression.expressions);
57 }
58
59 const depBlock = new RequireEnsureDependenciesBlock(
60 chunkName,
61 expr.loc
62 );
63 const errorCallbackExists =
64 expr.arguments.length === 4 ||
65 (!chunkName && expr.arguments.length === 3);
66 const dep = new RequireEnsureDependency(
67 expr.range,
68 expr.arguments[1].range,
69 errorCallbackExists && expr.arguments[2].range
70 );
71 dep.loc = expr.loc;
72 depBlock.addDependency(dep);
73 const old = parser.state.current;
74 parser.state.current = depBlock;
75 try {
76 let failed = false;
77 parser.inScope([], () => {
78 for (const ee of dependenciesItems) {
79 if (ee.isString()) {
80 const ensureDependency = new RequireEnsureItemDependency(
81 ee.string
82 );
83 ensureDependency.loc = ee.loc || expr.loc;
84 depBlock.addDependency(ensureDependency);
85 } else {
86 failed = true;
87 }
88 }
89 });
90 if (failed) {
91 return;
92 }
93 if (successExpression) {
94 if (successExpression.fn.body.type === "BlockStatement") {
95 parser.walkStatement(successExpression.fn.body);
96 } else {
97 parser.walkExpression(successExpression.fn.body);
98 }
99 }
100 old.addBlock(depBlock);
101 } finally {
102 parser.state.current = old;
103 }
104 if (!successExpression) {
105 parser.walkExpression(successExpressionArg);
106 }
107 if (errorExpression) {
108 if (errorExpression.fn.body.type === "BlockStatement") {
109 parser.walkStatement(errorExpression.fn.body);
110 } else {
111 parser.walkExpression(errorExpression.fn.body);
112 }
113 } else if (errorExpressionArg) {
114 parser.walkExpression(errorExpressionArg);
115 }
116 return true;
117 }
118 }
119 });
120 }
121};
Note: See TracBrowser for help on using the repository browser.