PrefetchPlugin.js 837 B

12345678910111213141516171819202122232425262728293031323334353637
  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. class PrefetchPlugin {
  8. constructor(context, request) {
  9. if (!request) {
  10. this.request = context;
  11. } else {
  12. this.context = context;
  13. this.request = request;
  14. }
  15. }
  16. apply(compiler) {
  17. compiler.hooks.compilation.tap(
  18. "PrefetchPlugin",
  19. (compilation, { normalModuleFactory }) => {
  20. compilation.dependencyFactories.set(
  21. PrefetchDependency,
  22. normalModuleFactory
  23. );
  24. }
  25. );
  26. compiler.hooks.make.tapAsync("PrefetchPlugin", (compilation, callback) => {
  27. compilation.prefetch(
  28. this.context || compiler.context,
  29. new PrefetchDependency(this.request),
  30. callback
  31. );
  32. });
  33. }
  34. }
  35. module.exports = PrefetchPlugin;