EntryPlugin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 = {
  55. name:
  56. typeof options === "object"
  57. ? /** @type {string} */ (options.name)
  58. : options
  59. };
  60. return dep;
  61. }
  62. }
  63. module.exports = EntryPlugin;