DllEntryPlugin.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
  11. /** @typedef {string[]} Entries */
  12. /** @typedef {EntryOptions & { name: string }} Options */
  13. const PLUGIN_NAME = "DllEntryPlugin";
  14. class DllEntryPlugin {
  15. /**
  16. * Creates an instance of DllEntryPlugin.
  17. * @param {string} context context
  18. * @param {Entries} entries entry names
  19. * @param {Options} options options
  20. */
  21. constructor(context, entries, options) {
  22. this.context = context;
  23. this.entries = entries;
  24. this.options = options;
  25. }
  26. /**
  27. * Applies the plugin by registering its hooks on the compiler.
  28. * @param {Compiler} compiler the compiler instance
  29. * @returns {void}
  30. */
  31. apply(compiler) {
  32. compiler.hooks.compilation.tap(
  33. PLUGIN_NAME,
  34. (compilation, { normalModuleFactory }) => {
  35. const dllModuleFactory = new DllModuleFactory();
  36. compilation.dependencyFactories.set(
  37. DllEntryDependency,
  38. dllModuleFactory
  39. );
  40. compilation.dependencyFactories.set(
  41. EntryDependency,
  42. normalModuleFactory
  43. );
  44. }
  45. );
  46. compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
  47. compilation.addEntry(
  48. this.context,
  49. new DllEntryDependency(
  50. this.entries.map((e, idx) => {
  51. const dep = new EntryDependency(e);
  52. dep.loc = {
  53. name: this.options.name,
  54. index: idx
  55. };
  56. return dep;
  57. }),
  58. this.options.name
  59. ),
  60. this.options,
  61. (error) => {
  62. if (error) return callback(error);
  63. callback();
  64. }
  65. );
  66. });
  67. }
  68. }
  69. module.exports = DllEntryPlugin;