DllEntryPlugin.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DllModuleFactory = require("./DllModuleFactory");
  7. const DllEntryDependency = require("./dependencies/DllEntryDependency");
  8. const EntryDependency = require("./dependencies/EntryDependency");
  9. /** @typedef {import("./Compiler")} Compiler */
  10. class DllEntryPlugin {
  11. /**
  12. * @param {string} context context
  13. * @param {string[]} entries entry names
  14. * @param {TODO} options options
  15. */
  16. constructor(context, entries, options) {
  17. this.context = context;
  18. this.entries = entries;
  19. this.options = options;
  20. }
  21. /**
  22. * Apply the plugin
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.compilation.tap(
  28. "DllEntryPlugin",
  29. (compilation, { normalModuleFactory }) => {
  30. const dllModuleFactory = new DllModuleFactory();
  31. compilation.dependencyFactories.set(
  32. DllEntryDependency,
  33. dllModuleFactory
  34. );
  35. compilation.dependencyFactories.set(
  36. EntryDependency,
  37. normalModuleFactory
  38. );
  39. }
  40. );
  41. compiler.hooks.make.tapAsync("DllEntryPlugin", (compilation, callback) => {
  42. compilation.addEntry(
  43. this.context,
  44. new DllEntryDependency(
  45. this.entries.map((e, idx) => {
  46. const dep = new EntryDependency(e);
  47. dep.loc = {
  48. name: this.options.name,
  49. index: idx
  50. };
  51. return dep;
  52. }),
  53. this.options.name
  54. ),
  55. this.options,
  56. error => {
  57. if (error) return callback(error);
  58. callback();
  59. }
  60. );
  61. });
  62. }
  63. }
  64. module.exports = DllEntryPlugin;