* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 528 | * @returns {void} |
| 529 | */ |
| 530 | apply(compiler) { |
| 531 | /** @type {BackendApi} */ |
| 532 | let backend; |
| 533 | compiler.hooks.beforeCompile.tapAsync(PLUGIN_NAME, (params, callback) => { |
| 534 | if (backend !== undefined) return callback(); |
| 535 | const promise = this.backend(compiler, (err, result) => { |
| 536 | if (err) return callback(err); |
| 537 | backend = /** @type {BackendApi} */ (result); |
| 538 | callback(); |
| 539 | }); |
| 540 | if (promise && promise.then) { |
| 541 | promise.then((b) => { |
| 542 | backend = b; |
| 543 | callback(); |
| 544 | }, callback); |
| 545 | } |
| 546 | }); |
| 547 | compiler.hooks.thisCompilation.tap( |
| 548 | PLUGIN_NAME, |
| 549 | (compilation, { normalModuleFactory }) => { |
| 550 | normalModuleFactory.hooks.module.tap( |
| 551 | PLUGIN_NAME, |
| 552 | (module, createData, resolveData) => { |
| 553 | if ( |
| 554 | resolveData.dependencies.every((dep) => |
| 555 | HMR_DEPENDENCY_TYPES.has(dep.type) |
| 556 | ) |
| 557 | ) { |
| 558 | // for HMR only resolving, try to determine if the HMR accept/decline refers to |
| 559 | // an import() or not |
| 560 | const hmrDep = resolveData.dependencies[0]; |
| 561 | const originModule = |
| 562 | /** @type {Module} */ |
| 563 | (compilation.moduleGraph.getParentModule(hmrDep)); |
| 564 | const isReferringToDynamicImport = originModule.blocks.some( |
| 565 | (block) => |
| 566 | block.dependencies.some( |
| 567 | (dep) => |
| 568 | dep.type === "import()" && |
| 569 | /** @type {HarmonyImportDependency} */ (dep).request === |
| 570 | hmrDep.request |
| 571 | ) |
| 572 | ); |
| 573 | if (!isReferringToDynamicImport) return module; |
| 574 | } else if ( |
| 575 | !resolveData.dependencies.every( |
| 576 | (dep) => |
| 577 | HMR_DEPENDENCY_TYPES.has(dep.type) || |
| 578 | (this.imports && |
| 579 | (dep.type === "import()" || |
| 580 | dep.type === "import() context element")) || |
| 581 | (this.entries && dep.type === "entry") |
| 582 | ) |
| 583 | ) { |
| 584 | return module; |
| 585 | } |
| 586 | if ( |
| 587 | /webpack[/\\]hot[/\\]|webpack-dev-server[/\\]client|webpack-hot-middleware[/\\]client/.test( |