DynamicEntryPlugin.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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").EntryDynamicNormalized} EntryDynamic */
  10. /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
  11. /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStatic */
  12. /** @typedef {import("./Compiler")} Compiler */
  13. class DynamicEntryPlugin {
  14. /**
  15. * @param {string} context the context path
  16. * @param {EntryDynamic} entry the entry value
  17. */
  18. constructor(context, entry) {
  19. this.context = context;
  20. this.entry = entry;
  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. "DynamicEntryPlugin",
  30. (compilation, { normalModuleFactory }) => {
  31. compilation.dependencyFactories.set(
  32. EntryDependency,
  33. normalModuleFactory
  34. );
  35. }
  36. );
  37. compiler.hooks.make.tapPromise(
  38. "DynamicEntryPlugin",
  39. (compilation, callback) =>
  40. Promise.resolve(this.entry())
  41. .then(entry => {
  42. const promises = [];
  43. for (const name of Object.keys(entry)) {
  44. const desc = entry[name];
  45. const options = EntryOptionPlugin.entryDescriptionToOptions(
  46. compiler,
  47. name,
  48. desc
  49. );
  50. for (const entry of desc.import) {
  51. promises.push(
  52. new Promise((resolve, reject) => {
  53. compilation.addEntry(
  54. this.context,
  55. EntryPlugin.createDependency(entry, options),
  56. options,
  57. err => {
  58. if (err) return reject(err);
  59. resolve();
  60. }
  61. );
  62. })
  63. );
  64. }
  65. }
  66. return Promise.all(promises);
  67. })
  68. .then(x => {})
  69. );
  70. }
  71. }
  72. module.exports = DynamicEntryPlugin;