10
0

DynamicEntryPlugin.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Naoyuki Kanezawa @nkzawa
  4. */
  5. "use strict";
  6. const EntryOptionPlugin = require("./EntryOptionPlugin");
  7. const EntryPlugin = require("./EntryPlugin");
  8. const EntryDependency = require("./dependencies/EntryDependency");
  9. /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
  10. /** @typedef {import("../declarations/WebpackOptions").EntryDynamicNormalized} EntryDynamic */
  11. /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
  12. /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} 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. * Apply the plugin
  25. * @param {Compiler} compiler the compiler instance
  26. * @returns {void}
  27. */
  28. apply(compiler) {
  29. compiler.hooks.compilation.tap(
  30. "DynamicEntryPlugin",
  31. (compilation, { normalModuleFactory }) => {
  32. compilation.dependencyFactories.set(
  33. EntryDependency,
  34. normalModuleFactory
  35. );
  36. }
  37. );
  38. compiler.hooks.make.tapPromise("DynamicEntryPlugin", compilation =>
  39. Promise.resolve(this.entry())
  40. .then(entry => {
  41. const promises = [];
  42. for (const name of Object.keys(entry)) {
  43. const desc = entry[name];
  44. const options = EntryOptionPlugin.entryDescriptionToOptions(
  45. compiler,
  46. name,
  47. desc
  48. );
  49. for (const entry of /** @type {NonNullable<EntryDescriptionNormalized["import"]>} */ (
  50. desc.import
  51. )) {
  52. promises.push(
  53. new Promise(
  54. /**
  55. * @param {(value?: any) => void} resolve resolve
  56. * @param {(reason?: Error) => void} reject reject
  57. */
  58. (resolve, reject) => {
  59. compilation.addEntry(
  60. this.context,
  61. EntryPlugin.createDependency(entry, options),
  62. options,
  63. err => {
  64. if (err) return reject(err);
  65. resolve();
  66. }
  67. );
  68. }
  69. )
  70. );
  71. }
  72. }
  73. return Promise.all(promises);
  74. })
  75. .then(x => {})
  76. );
  77. }
  78. }
  79. module.exports = DynamicEntryPlugin;