MCPcopy
hub / github.com/webpack/webpack / applyDefine

Method applyDefine

lib/DefinePlugin.js:589–704  ·  lib/DefinePlugin.js::DefinePlugin.applyDefine
(key, code)

Source from the content-addressed store, hash-verified

587 * @returns {void}
588 */
589 const applyDefine = (key, code) => {
590 const originalKey = key;
591 const isTypeof = TYPEOF_OPERATOR_REGEXP.test(key);
592 if (isTypeof) key = key.replace(TYPEOF_OPERATOR_REGEXP, class="st">"");
593 let recurse = false;
594 let recurseTypeof = false;
595 if (!isTypeof) {
596 parser.hooks.canRename.for(key).tap(PLUGIN_NAME, () => {
597 addValueDependency(originalKey);
598 return true;
599 });
600 parser.hooks.evaluateIdentifier
601 .for(key)
602 .tap(PLUGIN_NAME, (expr) => {
603 /**
604 * this is needed in case there is a recursion in the DefinePlugin
605 * to prevent an endless recursion
606 * e.g.: new DefinePlugin({
607 * class="st">"a": class="st">"b",
608 * class="st">"b": class="st">"a"
609 * });
610 */
611 if (recurse) return;
612 addValueDependency(originalKey);
613 recurse = true;
614 const res = parser.evaluate(
615 toCode(
616 code,
617 parser,
618 compilation.valueCacheVersions,
619 key,
620 runtimeTemplate,
621 logger,
622 null
623 )
624 );
625 recurse = false;
626 res.setRange(/** @type {Range} */ (expr.range));
627 return res;
628 });
629 parser.hooks.expression.for(key).tap(PLUGIN_NAME, (expr) => {
630 addValueDependency(originalKey);
631 let strCode = toCode(
632 code,
633 parser,
634 compilation.valueCacheVersions,
635 originalKey,
636 runtimeTemplate,
637 logger,
638 !parser.isAsiPosition(/** @type {Range} */ (expr.range)[0]),
639 null
640 );
641
642 if (parser.scope.inShorthand) {
643 strCode = `${parser.scope.inShorthand}:${strCode}`;
644 }
645
646 if (WEBPACK_REQUIRE_FUNCTION_REGEXP.test(strCode)) {

Callers

nothing calls this directly

Calls 8

toCodeFunction · 0.85
tapMethod · 0.80
evaluateMethod · 0.80
setRangeMethod · 0.80
isAsiPositionMethod · 0.80
isStringMethod · 0.80
testMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected