EntryPlugin.js 1.7 KB

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