MCPcopy
hub / github.com/webpack/webpack / applyDefineKey

Method applyDefineKey

lib/DefinePlugin.js:504–581  ·  lib/DefinePlugin.js::DefinePlugin.applyDefineKey
(prefix, key)

Source from the content-addressed store, hash-verified

502 * @returns {void}
503 */
504 const applyDefineKey = (prefix, key) => {
505 const splittedKey = key.split(class="st">".");
506 const firstKey = splittedKey[0];
507 for (const [i, _] of splittedKey.slice(1).entries()) {
508 const fullKey = prefix + splittedKey.slice(0, i + 1).join(class="st">".");
509 parser.hooks.canRename.for(fullKey).tap(PLUGIN_NAME, () => {
510 addValueDependency(key);
511 if (
512 parser.scope.definitions.get(firstKey) instanceof VariableInfo
513 ) {
514 return false;
515 }
516 return true;
517 });
518 }
519 if (prefix === class="st">"") {
520 const final = splittedKey[splittedKey.length - 1];
521 const nestedSet = nestedByFinalKey.get(final);
522 if (!nestedSet || nestedSet.size <= 0) return;
523 for (const nested of /** @type {Set<string>} */ (nestedSet)) {
524 if (nested && !hooked.has(nested)) {
525 class="cm">// only detect the same nested key once
526 hooked.add(nested);
527 parser.hooks.collectDestructuringAssignmentProperties.tap(
528 PLUGIN_NAME,
529 (expr) => {
530 const nameInfo = parser.getNameForExpression(expr);
531 if (nameInfo && nameInfo.name === nested) return true;
532 }
533 );
534 parser.hooks.expression.for(nested).tap(
535 {
536 name: PLUGIN_NAME,
537 class="cm">// why 100? Ensures it runs after object define
538 stage: 100
539 },
540 (expr) => {
541 const destructed =
542 parser.destructuringAssignmentPropertiesFor(expr);
543 if (destructed === undefined) {
544 return;
545 }
546 /** @type {Definitions} */
547 const obj = Object.create(null);
548 const finalSet = finalByNestedKey.get(nested);
549 for (const { id } of destructed) {
550 const fullKey = `${nested}.${id}`;
551 if (
552 !finalSet ||
553 !finalSet.has(id) ||
554 !definitions[fullKey]
555 ) {
556 return;
557 }
558 obj[id] = definitions[fullKey];
559 }
560 let strCode = stringifyObj(
561 obj,

Callers

nothing calls this directly

Calls 13

stringifyObjFunction · 0.85
getObjKeysFunction · 0.85
splitMethod · 0.80
entriesMethod · 0.80
sliceMethod · 0.80
tapMethod · 0.80
getNameForExpressionMethod · 0.80
isAsiPositionMethod · 0.80
getMethod · 0.45
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected