PrefetchPlugin.js 1.0 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 PrefetchDependency = require("./dependencies/PrefetchDependency");
  7. /** @typedef {import("./Compiler")} Compiler */
  8. class PrefetchPlugin {
  9. constructor(context, request) {
  10. if (request) {
  11. this.context = context;
  12. this.request = request;
  13. } else {
  14. this.context = null;
  15. this.request = context;
  16. }
  17. }
  18. /**
  19. * Apply the plugin
  20. * @param {Compiler} compiler the compiler instance
  21. * @returns {void}
  22. */
  23. apply(compiler) {
  24. compiler.hooks.compilation.tap(
  25. "PrefetchPlugin",
  26. (compilation, { normalModuleFactory }) => {
  27. compilation.dependencyFactories.set(
  28. PrefetchDependency,
  29. normalModuleFactory
  30. );
  31. }
  32. );
  33. compiler.hooks.make.tapAsync("PrefetchPlugin", (compilation, callback) => {
  34. compilation.addModuleChain(
  35. this.context || compiler.context,
  36. new PrefetchDependency(this.request),
  37. err => {
  38. callback(err);
  39. }
  40. );
  41. });
  42. }
  43. }
  44. module.exports = PrefetchPlugin;