DelegatedPlugin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DelegatedSourceDependency = require("../dependencies/DelegatedSourceDependency");
  7. const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
  8. /** @import Compiler from "../Compiler" */
  9. /** @import { Options } from "./DelegatedModuleFactoryPlugin" */
  10. const PLUGIN_NAME = "DelegatedPlugin";
  11. class DelegatedPlugin {
  12. /**
  13. * Creates an instance of DelegatedPlugin.
  14. * @param {Options} options options
  15. */
  16. constructor(options) {
  17. /** @type {Options} */
  18. this.options = options;
  19. }
  20. /**
  21. * Applies the plugin by registering its hooks on the compiler.
  22. * @param {Compiler} compiler the compiler instance
  23. * @returns {void}
  24. */
  25. apply(compiler) {
  26. compiler.hooks.compilation.tap(
  27. PLUGIN_NAME,
  28. (compilation, { normalModuleFactory }) => {
  29. compilation.dependencyFactories.set(
  30. DelegatedSourceDependency,
  31. normalModuleFactory
  32. );
  33. }
  34. );
  35. compiler.hooks.compile.tap(PLUGIN_NAME, ({ normalModuleFactory }) => {
  36. new DelegatedModuleFactoryPlugin({
  37. associatedObjectForCache: compiler.root,
  38. ...this.options
  39. }).apply(normalModuleFactory);
  40. });
  41. }
  42. }
  43. module.exports = DelegatedPlugin;