* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 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 |