PrefetchPlugin.js 795B

1234567891011121314151617181920212223242526272829303132
  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.plugin("compilation", (compilation, params) => {
  18. const normalModuleFactory = params.normalModuleFactory;
  19. compilation.dependencyFactories.set(PrefetchDependency, normalModuleFactory);
  20. });
  21. compiler.plugin("make", (compilation, callback) => {
  22. compilation.prefetch(this.context || compiler.context, new PrefetchDependency(this.request), callback);
  23. });
  24. }
  25. }
  26. module.exports = PrefetchPlugin;