You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
  7. const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
  8. const MultiModuleFactory = require("./MultiModuleFactory");
  9. /** @typedef {import("./Compiler")} Compiler */
  10. class MultiEntryPlugin {
  11. /**
  12. * The MultiEntryPlugin is invoked whenever this.options.entry value is an array of paths
  13. * @param {string} context context path
  14. * @param {string[]} entries array of entry paths
  15. * @param {string} name entry key name
  16. */
  17. constructor(context, entries, name) {
  18. this.context = context;
  19. this.entries = entries;
  20. this.name = name;
  21. }
  22. /**
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.compilation.tap(
  28. "MultiEntryPlugin",
  29. (compilation, { normalModuleFactory }) => {
  30. const multiModuleFactory = new MultiModuleFactory();
  31. compilation.dependencyFactories.set(
  32. MultiEntryDependency,
  33. multiModuleFactory
  34. );
  35. compilation.dependencyFactories.set(
  36. SingleEntryDependency,
  37. normalModuleFactory
  38. );
  39. }
  40. );
  41. compiler.hooks.make.tapAsync(
  42. "MultiEntryPlugin",
  43. (compilation, callback) => {
  44. const { context, entries, name } = this;
  45. const dep = MultiEntryPlugin.createDependency(entries, name);
  46. compilation.addEntry(context, dep, name, callback);
  47. }
  48. );
  49. }
  50. /**
  51. * @param {string[]} entries each entry path string
  52. * @param {string} name name of the entry
  53. * @returns {MultiEntryDependency} returns a constructed Dependency
  54. */
  55. static createDependency(entries, name) {
  56. return new MultiEntryDependency(
  57. entries.map((e, idx) => {
  58. const dep = new SingleEntryDependency(e);
  59. // Because entrypoints are not dependencies found in an
  60. // existing module, we give it a synthetic id
  61. dep.loc = {
  62. name,
  63. index: idx
  64. };
  65. return dep;
  66. }),
  67. name
  68. );
  69. }
  70. }
  71. module.exports = MultiEntryPlugin;