MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/optimize/ConstExportsPlugin.js:48–207  ·  view source on GitHub ↗

* @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

46 * @returns {void}
47 */
48 apply(compiler) {
49 compiler.hooks.compilation.tap(
50 PLUGIN_NAME,
51 (compilation, { normalModuleFactory }) => {
52 /**
53 * @param {JavascriptParser} parser the parser
54 * @returns {void}
55 */
56 const handleInlineExports = (parser) => {
57 parser.hooks.program.tap(PLUGIN_NAME, () => {
58 const buildInfo =
59 /** @type {JavascriptModuleBuildInfo | undefined} */
60 (parser.state.module.buildInfo);
61 if (buildInfo) buildInfo.inlineExports = true;
62 });
63
64 // Propagate inlined constant through evaluator so chained constants and uses see the literal
65 parser.hooks.evaluateIdentifier
66 .for(CONST_BINDING_TAG)
67 .tap(PLUGIN_NAME, (expr) => {
68 const tagData =
69 /** @type {{ value?: InlinedValue } | undefined} */
70 (parser.currentTagData);
71 if (!tagData || !tagData.value) return;
72 const { value } = tagData;
73 const eval_ = new BasicEvaluatedExpression().setRange(
74 /** @type {[number, number]} */ (expr.range)
75 );
76 switch (value.kind) {
77 case "null":
78 return eval_.setNull();
79 case "undefined":
80 return eval_.setUndefined();
81 case "boolean":
82 return eval_.setBoolean(/** @type {boolean} */ (value.value));
83 case "number":
84 return eval_.setNumber(/** @type {number} */ (value.value));
85 case "string":
86 return eval_.setString(/** @type {string} */ (value.value));
87 }
88 });
89 };
90
91 /**
92 * @param {JavascriptParser} parser the parser
93 * @returns {void}
94 */
95 const handleConstValue = (parser) => {
96 // Only const is tracked; function/class names can be reassigned in sloppy mode.
97 // Re-exports always use getters: cross-module bindings may be mutable,
98 // and SideEffectsFlagPlugin can rewire connections skipping the template.
99 parser.hooks.preDeclarator.tap(
100 PLUGIN_NAME,
101 (declarator, statement) => {
102 // Detect top-level `const` declarations:
103 // - Tag ALL const bindings with CONST_BINDING_TAG (for const detection via tag system)
104 // - Carry inlined primitive value in tag data when eligible (for inline optimization)
105 if (statement.kind !== "const") return;

Callers

nothing calls this directly

Calls 11

enableInlineExportsFunction · 0.85
tapMethod · 0.80
getExportsInfoMethod · 0.80
canInlineMethod · 0.80
hasUsedNameMethod · 0.80
setUsedNameMethod · 0.80
markInlinedExportsMethod · 0.80
pushMethod · 0.45
hasMethod · 0.45
addMethod · 0.45
getUsedMethod · 0.45

Tested by

no test coverage detected