PrefetchPlugin.js 1.1 KB

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