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

Method apply

lib/DefinePlugin.js:391–944  ·  lib/DefinePlugin.js::DefinePlugin.apply

* 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

389 * @returns {void}
390 */
391 apply(compiler) {
392 compiler.hooks.compilation.tap(
393 PLUGIN_NAME,
394 (compilation, { normalModuleFactory }) => {
395 const definitions = this.definitions;
396 const hooks = DefinePlugin.getCompilationHooks(compilation);
397
398 hooks.definitions.tap(PLUGIN_NAME, (previousDefinitions) => ({
399 ...previousDefinitions,
400 ...definitions
401 }));
402
403 /**
404 * @type {Map<string, Set<string>>}
405 */
406 const finalByNestedKey = new Map();
407 /**
408 * @type {Map<string, Set<string>>}
409 */
410 const nestedByFinalKey = new Map();
411
412 const logger = compilation.getLogger(class="st">"webpack.DefinePlugin");
413 compilation.dependencyTemplates.set(
414 ConstDependency,
415 new ConstDependency.Template()
416 );
417 const { runtimeTemplate } = compilation;
418
419 const mainHash = createHash(compilation.outputOptions.hashFunction);
420 mainHash.update(
421 /** @type {string} */
422 (compilation.valueCacheVersions.get(VALUE_DEP_MAIN)) || class="st">""
423 );
424
425 /**
426 * Handles the hook callback for this code path.
427 * @param {JavascriptParser} parser Parser
428 * @returns {void}
429 */
430 const handler = (parser) => {
431 /** @type {Set<string>} */
432 const hooked = new Set();
433 const mainValue =
434 /** @type {ValueCacheVersion} */
435 (compilation.valueCacheVersions.get(VALUE_DEP_MAIN));
436 parser.hooks.program.tap(PLUGIN_NAME, () => {
437 const buildInfo = /** @type {NormalModuleBuildInfo} */ (
438 parser.state.module.buildInfo
439 );
440 if (!buildInfo.valueDependencies) {
441 buildInfo.valueDependencies = new Map();
442 }
443 buildInfo.valueDependencies.set(VALUE_DEP_MAIN, mainValue);
444 });
445
446 /**
447 * Adds value dependency.
448 * @param {string} key key

Callers 2

applyMethod · 0.95
applyMethod · 0.95

Calls 9

createHashFunction · 0.85
tapMethod · 0.80
sliceMethod · 0.80
getLoggerMethod · 0.65
getCompilationHooksMethod · 0.45
setMethod · 0.45
updateMethod · 0.45
getMethod · 0.45
digestMethod · 0.45

Tested by

no test coverage detected