(range)
| 1301 | * @returns {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} result |
| 1302 | */ |
| 1303 | const parseCommentOptions = (range) => { |
| 1304 | const found = getComments(range); |
| 1305 | if (found.length === 0) { |
| 1306 | return EMPTY_COMMENT_OPTIONS; |
| 1307 | } |
| 1308 | /** @type {Record<string, EXPECTED_ANY>} */ |
| 1309 | const options = {}; |
| 1310 | /** @type {(Error & { comment: Comment })[]} */ |
| 1311 | const errors = []; |
| 1312 | for (const c of found) { |
| 1313 | const { value } = c; |
| 1314 | if (value && webpackCommentRegExp.test(value)) { |
| 1315 | // Fast path for the common `webpackXxx: <bool|number|null>` pair, keeping it out of `vm.runInContext`. |
| 1316 | const fast = MAGIC_COMMENT_FAST_PATH.exec(value); |
| 1317 | if (fast !== null) { |
| 1318 | const key = fast[1]; |
| 1319 | const raw = fast[2]; |
| 1320 | options[key] = |
| 1321 | raw === "true" |
| 1322 | ? true |
| 1323 | : raw === "false" |
| 1324 | ? false |
| 1325 | : raw === "null" |
| 1326 | ? null |
| 1327 | : Number(raw); |
| 1328 | continue; |
| 1329 | } |
| 1330 | // try compile only if webpack options comment is present |
| 1331 | try { |
| 1332 | for (let [key, val] of Object.entries( |
| 1333 | vm.runInContext( |
| 1334 | `(function(){return {${value}};})()`, |
| 1335 | magicCommentContext |
| 1336 | ) |
| 1337 | )) { |
| 1338 | if (typeof val === "object" && val !== null) { |
| 1339 | val = |
| 1340 | val.constructor.name === "RegExp" |
| 1341 | ? new RegExp(val) |
| 1342 | : JSON.parse(JSON.stringify(val)); |
| 1343 | } |
| 1344 | options[key] = val; |
| 1345 | } |
| 1346 | } catch (err) { |
| 1347 | const newErr = new Error( |
| 1348 | String(/** @type {Error} */ (err).message) |
| 1349 | ); |
| 1350 | newErr.stack = String(/** @type {Error} */ (err).stack); |
| 1351 | Object.assign(newErr, { comment: c }); |
| 1352 | errors.push(/** @type {(Error & { comment: Comment })} */ (newErr)); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | return { options, errors }; |
| 1357 | }; |
| 1358 | |
| 1359 | // CSS modules stuff |
| 1360 |
no test coverage detected