EntryPlugin.js 1.9 KB

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