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