DelegatedPlugin.js 1.2 KB

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