SingleEntryPlugin.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
  7. /** @typedef {import("./Compiler")} Compiler */
  8. class SingleEntryPlugin {
  9. /**
  10. * An entry plugin which will handle
  11. * creation of the SingleEntryDependency
  12. *
  13. * @param {string} context context path
  14. * @param {string} entry entry path
  15. * @param {string} name entry key name
  16. */
  17. constructor(context, entry, name) {
  18. this.context = context;
  19. this.entry = entry;
  20. this.name = name;
  21. }
  22. /**
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.compilation.tap(
  28. "SingleEntryPlugin",
  29. (compilation, { normalModuleFactory }) => {
  30. compilation.dependencyFactories.set(
  31. SingleEntryDependency,
  32. normalModuleFactory
  33. );
  34. }
  35. );
  36. compiler.hooks.make.tapAsync(
  37. "SingleEntryPlugin",
  38. (compilation, callback) => {
  39. const { entry, name, context } = this;
  40. const dep = SingleEntryPlugin.createDependency(entry, name);
  41. compilation.addEntry(context, dep, name, callback);
  42. }
  43. );
  44. }
  45. /**
  46. * @param {string} entry entry request
  47. * @param {string} name entry name
  48. * @returns {SingleEntryDependency} the dependency
  49. */
  50. static createDependency(entry, name) {
  51. const dep = new SingleEntryDependency(entry);
  52. dep.loc = { name };
  53. return dep;
  54. }
  55. }
  56. module.exports = SingleEntryPlugin;