DynamicEntryPlugin.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Naoyuki Kanezawa @nkzawa
  4. */
  5. "use strict";
  6. const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
  7. const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
  8. const MultiModuleFactory = require("./MultiModuleFactory");
  9. const MultiEntryPlugin = require("./MultiEntryPlugin");
  10. const SingleEntryPlugin = require("./SingleEntryPlugin");
  11. /** @typedef {import("../declarations/WebpackOptions").EntryDynamic} EntryDynamic */
  12. /** @typedef {import("../declarations/WebpackOptions").EntryStatic} EntryStatic */
  13. /** @typedef {import("./Compiler")} Compiler */
  14. class DynamicEntryPlugin {
  15. /**
  16. * @param {string} context the context path
  17. * @param {EntryDynamic} entry the entry value
  18. */
  19. constructor(context, entry) {
  20. this.context = context;
  21. this.entry = entry;
  22. }
  23. /**
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.compilation.tap(
  29. "DynamicEntryPlugin",
  30. (compilation, { normalModuleFactory }) => {
  31. const multiModuleFactory = new MultiModuleFactory();
  32. compilation.dependencyFactories.set(
  33. MultiEntryDependency,
  34. multiModuleFactory
  35. );
  36. compilation.dependencyFactories.set(
  37. SingleEntryDependency,
  38. normalModuleFactory
  39. );
  40. }
  41. );
  42. compiler.hooks.make.tapAsync(
  43. "DynamicEntryPlugin",
  44. (compilation, callback) => {
  45. /**
  46. * @param {string|string[]} entry entry value or array of entry values
  47. * @param {string} name name of entry
  48. * @returns {Promise<EntryStatic>} returns the promise resolving the Compilation#addEntry function
  49. */
  50. const addEntry = (entry, name) => {
  51. const dep = DynamicEntryPlugin.createDependency(entry, name);
  52. return new Promise((resolve, reject) => {
  53. compilation.addEntry(this.context, dep, name, err => {
  54. if (err) return reject(err);
  55. resolve();
  56. });
  57. });
  58. };
  59. Promise.resolve(this.entry()).then(entry => {
  60. if (typeof entry === "string" || Array.isArray(entry)) {
  61. addEntry(entry, "main").then(() => callback(), callback);
  62. } else if (typeof entry === "object") {
  63. Promise.all(
  64. Object.keys(entry).map(name => {
  65. return addEntry(entry[name], name);
  66. })
  67. ).then(() => callback(), callback);
  68. }
  69. });
  70. }
  71. );
  72. }
  73. }
  74. module.exports = DynamicEntryPlugin;
  75. /**
  76. * @param {string|string[]} entry entry value or array of entry paths
  77. * @param {string} name name of entry
  78. * @returns {SingleEntryDependency|MultiEntryDependency} returns dep
  79. */
  80. DynamicEntryPlugin.createDependency = (entry, name) => {
  81. if (Array.isArray(entry)) {
  82. return MultiEntryPlugin.createDependency(entry, name);
  83. } else {
  84. return SingleEntryPlugin.createDependency(entry, name);
  85. }
  86. };