source: imaps-frontend/node_modules/webpack/lib/dependencies/ImportMetaContextDependencyParserPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8const WebpackError = require("../WebpackError");
9const {
10 evaluateToIdentifier
11} = require("../javascript/JavascriptParserHelpers");
12const ImportMetaContextDependency = require("./ImportMetaContextDependency");
13
14/** @typedef {import("estree").Expression} Expression */
15/** @typedef {import("estree").ObjectExpression} ObjectExpression */
16/** @typedef {import("estree").Property} Property */
17/** @typedef {import("estree").SourceLocation} SourceLocation */
18/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
19/** @typedef {import("../javascript/JavascriptParser").Range} Range */
20/** @typedef {import("../ContextModule").ContextModuleOptions} ContextModuleOptions */
21/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
22/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
23/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
24/** @typedef {Pick<ContextModuleOptions, 'mode'|'recursive'|'regExp'|'include'|'exclude'|'chunkName'>&{groupOptions: RawChunkGroupOptions, exports?: ContextModuleOptions["referencedExports"]}} ImportMetaContextOptions */
25
26/**
27 * @param {TODO} prop property
28 * @param {string} expect except message
29 * @returns {WebpackError} error
30 */
31function createPropertyParseError(prop, expect) {
32 return createError(
33 `Parsing import.meta.webpackContext options failed. Unknown value for property ${JSON.stringify(
34 prop.key.name
35 )}, expected type ${expect}.`,
36 prop.value.loc
37 );
38}
39
40/**
41 * @param {string} msg message
42 * @param {DependencyLocation} loc location
43 * @returns {WebpackError} error
44 */
45function createError(msg, loc) {
46 const error = new WebpackError(msg);
47 error.name = "ImportMetaContextError";
48 error.loc = loc;
49 return error;
50}
51
52module.exports = class ImportMetaContextDependencyParserPlugin {
53 /**
54 * @param {JavascriptParser} parser the parser
55 * @returns {void}
56 */
57 apply(parser) {
58 parser.hooks.evaluateIdentifier
59 .for("import.meta.webpackContext")
60 .tap("ImportMetaContextDependencyParserPlugin", expr =>
61 evaluateToIdentifier(
62 "import.meta.webpackContext",
63 "import.meta",
64 () => ["webpackContext"],
65 true
66 )(expr)
67 );
68 parser.hooks.call
69 .for("import.meta.webpackContext")
70 .tap("ImportMetaContextDependencyParserPlugin", expr => {
71 if (expr.arguments.length < 1 || expr.arguments.length > 2) return;
72 const [directoryNode, optionsNode] = expr.arguments;
73 if (optionsNode && optionsNode.type !== "ObjectExpression") return;
74 const requestExpr = parser.evaluateExpression(
75 /** @type {Expression} */ (directoryNode)
76 );
77 if (!requestExpr.isString()) return;
78 const request = /** @type {string} */ (requestExpr.string);
79 const errors = [];
80 let regExp = /^\.\/.*$/;
81 let recursive = true;
82 /** @type {ContextModuleOptions["mode"]} */
83 let mode = "sync";
84 /** @type {ContextModuleOptions["include"]} */
85 let include;
86 /** @type {ContextModuleOptions["exclude"]} */
87 let exclude;
88 /** @type {RawChunkGroupOptions} */
89 const groupOptions = {};
90 /** @type {ContextModuleOptions["chunkName"]} */
91 let chunkName;
92 /** @type {ContextModuleOptions["referencedExports"]} */
93 let exports;
94 if (optionsNode) {
95 for (const prop of /** @type {ObjectExpression} */ (optionsNode)
96 .properties) {
97 if (prop.type !== "Property" || prop.key.type !== "Identifier") {
98 errors.push(
99 createError(
100 "Parsing import.meta.webpackContext options failed.",
101 /** @type {DependencyLocation} */ (optionsNode.loc)
102 )
103 );
104 break;
105 }
106 switch (prop.key.name) {
107 case "regExp": {
108 const regExpExpr = parser.evaluateExpression(
109 /** @type {Expression} */ (prop.value)
110 );
111 if (!regExpExpr.isRegExp()) {
112 errors.push(createPropertyParseError(prop, "RegExp"));
113 } else {
114 regExp = /** @type {RegExp} */ (regExpExpr.regExp);
115 }
116 break;
117 }
118 case "include": {
119 const regExpExpr = parser.evaluateExpression(
120 /** @type {Expression} */ (prop.value)
121 );
122 if (!regExpExpr.isRegExp()) {
123 errors.push(createPropertyParseError(prop, "RegExp"));
124 } else {
125 include = regExpExpr.regExp;
126 }
127 break;
128 }
129 case "exclude": {
130 const regExpExpr = parser.evaluateExpression(
131 /** @type {Expression} */ (prop.value)
132 );
133 if (!regExpExpr.isRegExp()) {
134 errors.push(createPropertyParseError(prop, "RegExp"));
135 } else {
136 exclude = regExpExpr.regExp;
137 }
138 break;
139 }
140 case "mode": {
141 const modeExpr = parser.evaluateExpression(
142 /** @type {Expression} */ (prop.value)
143 );
144 if (!modeExpr.isString()) {
145 errors.push(createPropertyParseError(prop, "string"));
146 } else {
147 mode = /** @type {ContextModuleOptions["mode"]} */ (
148 modeExpr.string
149 );
150 }
151 break;
152 }
153 case "chunkName": {
154 const expr = parser.evaluateExpression(
155 /** @type {Expression} */ (prop.value)
156 );
157 if (!expr.isString()) {
158 errors.push(createPropertyParseError(prop, "string"));
159 } else {
160 chunkName = expr.string;
161 }
162 break;
163 }
164 case "exports": {
165 const expr = parser.evaluateExpression(
166 /** @type {Expression} */ (prop.value)
167 );
168 if (expr.isString()) {
169 exports = [[/** @type {string} */ (expr.string)]];
170 } else if (expr.isArray()) {
171 const items =
172 /** @type {BasicEvaluatedExpression[]} */
173 (expr.items);
174 if (
175 items.every(i => {
176 if (!i.isArray()) return false;
177 const innerItems =
178 /** @type {BasicEvaluatedExpression[]} */ (i.items);
179 return innerItems.every(i => i.isString());
180 })
181 ) {
182 exports = [];
183 for (const i1 of items) {
184 /** @type {string[]} */
185 const export_ = [];
186 for (const i2 of /** @type {BasicEvaluatedExpression[]} */ (
187 i1.items
188 )) {
189 export_.push(/** @type {string} */ (i2.string));
190 }
191 exports.push(export_);
192 }
193 } else {
194 errors.push(
195 createPropertyParseError(prop, "string|string[][]")
196 );
197 }
198 } else {
199 errors.push(
200 createPropertyParseError(prop, "string|string[][]")
201 );
202 }
203 break;
204 }
205 case "prefetch": {
206 const expr = parser.evaluateExpression(
207 /** @type {Expression} */ (prop.value)
208 );
209 if (expr.isBoolean()) {
210 groupOptions.prefetchOrder = 0;
211 } else if (expr.isNumber()) {
212 groupOptions.prefetchOrder = expr.number;
213 } else {
214 errors.push(createPropertyParseError(prop, "boolean|number"));
215 }
216 break;
217 }
218 case "preload": {
219 const expr = parser.evaluateExpression(
220 /** @type {Expression} */ (prop.value)
221 );
222 if (expr.isBoolean()) {
223 groupOptions.preloadOrder = 0;
224 } else if (expr.isNumber()) {
225 groupOptions.preloadOrder = expr.number;
226 } else {
227 errors.push(createPropertyParseError(prop, "boolean|number"));
228 }
229 break;
230 }
231 case "fetchPriority": {
232 const expr = parser.evaluateExpression(
233 /** @type {Expression} */ (prop.value)
234 );
235 if (
236 expr.isString() &&
237 ["high", "low", "auto"].includes(
238 /** @type {string} */ (expr.string)
239 )
240 ) {
241 groupOptions.fetchPriority =
242 /** @type {RawChunkGroupOptions["fetchPriority"]} */ (
243 expr.string
244 );
245 } else {
246 errors.push(
247 createPropertyParseError(prop, '"high"|"low"|"auto"')
248 );
249 }
250 break;
251 }
252 case "recursive": {
253 const recursiveExpr = parser.evaluateExpression(
254 /** @type {Expression} */ (prop.value)
255 );
256 if (!recursiveExpr.isBoolean()) {
257 errors.push(createPropertyParseError(prop, "boolean"));
258 } else {
259 recursive = /** @type {boolean} */ (recursiveExpr.bool);
260 }
261 break;
262 }
263 default:
264 errors.push(
265 createError(
266 `Parsing import.meta.webpackContext options failed. Unknown property ${JSON.stringify(
267 prop.key.name
268 )}.`,
269 /** @type {DependencyLocation} */ (optionsNode.loc)
270 )
271 );
272 }
273 }
274 }
275 if (errors.length) {
276 for (const error of errors) parser.state.current.addError(error);
277 return;
278 }
279
280 const dep = new ImportMetaContextDependency(
281 {
282 request,
283 include,
284 exclude,
285 recursive,
286 regExp,
287 groupOptions,
288 chunkName,
289 referencedExports: exports,
290 mode,
291 category: "esm"
292 },
293 /** @type {Range} */ (expr.range)
294 );
295 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
296 dep.optional = Boolean(parser.scope.inTry);
297 parser.state.current.addDependency(dep);
298 return true;
299 });
300 }
301};
Note: See TracBrowser for help on using the repository browser.