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

Method apply

lib/FlagDependencyUsagePlugin.js:51–431  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

49 * @returns {void}
50 */
51 apply(compiler) {
52 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
53 const moduleGraph = compilation.moduleGraph;
54 compilation.hooks.optimizeDependencies.tap(
55 { name: PLUGIN_NAME, stage: STAGE_DEFAULT },
56 (modules) => {
57 if (compilation.moduleMemCaches) {
58 throw new Error(
59 "optimization.usedExports can't be used with cacheUnaffected as export usage is a global effect"
60 );
61 }
62
63 const logger = compilation.getLogger(PLUGIN_LOGGER_NAME);
64 /** @type {Map<ExportsInfo, Module>} */
65 const exportInfoToModuleMap = new Map();
66
67 /** @type {TupleQueue<Module, RuntimeSpec>} */
68 const queue = new TupleQueue();
69
70 /**
71 * Process referenced module.
72 * @param {Module} module module to process
73 * @param {ReferencedExports} usedExports list of used exports
74 * @param {RuntimeSpec} runtime part of which runtime
75 * @param {boolean} forceSideEffects always apply side effects
76 * @returns {void}
77 */
78 const processReferencedModule = (
79 module,
80 usedExports,
81 runtime,
82 forceSideEffects
83 ) => {
84 const exportsInfo = moduleGraph.getExportsInfo(module);
85 if (usedExports === EXPORTS_OBJECT_REFERENCED_MANGLEABLE) {
86 // The whole namespace object escapes via a reference that codegen
87 // can materialize as a decoupled namespace object. When all exports
88 // are statically known we keep them mangleable instead of marking
89 // them used-in-unknown-way.
90 if (
91 this.mangleEscapingNamespaces &&
92 module.buildMeta &&
93 module.buildMeta.exportsType === "namespace" &&
94 exportsInfo.otherExportsInfo.provided === false
95 ) {
96 let changed = exportsInfo.setAllKnownExportsUsed(runtime);
97 // The whole namespace object is observed as a value, so the
98 // module must stay a real ES module namespace at runtime
99 // (keep __esModule / the namespace object, i.e. `r()`).
100 if (
101 exportsInfo
102 .getExportInfo("__esModule")
103 .setUsed(UsageState.Used, runtime)
104 ) {
105 changed = true;
106 }
107 // Exports must keep a real binding (not be inlined) so member
108 // access on the namespace has ES namespace semantics, e.g.

Callers

nothing calls this directly

Calls 10

dequeueMethod · 0.95
getEntryRuntimeFunction · 0.85
mergeRuntimeOwnedFunction · 0.85
tapMethod · 0.80
timeMethod · 0.80
getExportsInfoMethod · 0.80
timeEndMethod · 0.80
getLoggerMethod · 0.65
setMethod · 0.45
setHasUseInfoMethod · 0.45

Tested by

no test coverage detected