EntryPlugin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 creation of the EntryDependency
  12. * @param {string} context context path
  13. * @param {string} entry entry path
  14. * @param {EntryOptions | string=} options entry options (passing a string is deprecated)
  15. */
  16. constructor(context, entry, options) {
  17. this.context = context;
  18. this.entry = entry;
  19. this.options = options || "";
  20. }
  21. /**
  22. * Apply the plugin
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.compilation.tap(
  28. "EntryPlugin",
  29. (compilation, { normalModuleFactory }) => {
  30. compilation.dependencyFactories.set(
  31. EntryDependency,
  32. normalModuleFactory
  33. );
  34. }
  35. );
  36. const { entry, options, context } = this;
  37. const dep = EntryPlugin.createDependency(entry, options);
  38. compiler.hooks.make.tapAsync("EntryPlugin", (compilation, callback) => {
  39. compilation.addEntry(context, dep, options, err => {
  40. callback(err);
  41. });
  42. });
  43. }
  44. /**
  45. * @param {string} entry entry request
  46. * @param {EntryOptions | string} options entry options (passing string is deprecated)
  47. * @returns {EntryDependency} the dependency
  48. */
  49. static createDependency(entry, options) {
  50. const dep = new EntryDependency(entry);
  51. // TODO webpack 6 remove string option
  52. dep.loc = {
  53. name:
  54. typeof options === "object"
  55. ? /** @type {string} */ (options.name)
  56. : options
  57. };
  58. return dep;
  59. }
  60. }
  61. module.exports = EntryPlugin;