DllEntryPlugin.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DllEntryDependency = require("../dependencies/DllEntryDependency");
  7. const EntryDependency = require("../dependencies/EntryDependency");
  8. const DllModuleFactory = require("./DllModuleFactory");
  9. /** @import Compiler from "../Compiler" */
  10. /** @import { EntryOptions } from "../Entrypoint" */
  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. /** @type {string} */
  23. this.context = context;
  24. /** @type {Entries} */
  25. this.entries = entries;
  26. /** @type {Options} */
  27. this.options = options;
  28. }
  29. /**
  30. * Applies the plugin by registering its hooks on the compiler.
  31. * @param {Compiler} compiler the compiler instance
  32. * @returns {void}
  33. */
  34. apply(compiler) {
  35. compiler.hooks.compilation.tap(
  36. PLUGIN_NAME,
  37. (compilation, { normalModuleFactory }) => {
  38. const dllModuleFactory = new DllModuleFactory();
  39. compilation.dependencyFactories.set(
  40. DllEntryDependency,
  41. dllModuleFactory
  42. );
  43. compilation.dependencyFactories.set(
  44. EntryDependency,
  45. normalModuleFactory
  46. );
  47. }
  48. );
  49. compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
  50. compilation.addEntry(
  51. this.context,
  52. new DllEntryDependency(
  53. this.entries.map((e, idx) => {
  54. const dep = new EntryDependency(e);
  55. dep.loc = {
  56. name: this.options.name,
  57. index: idx
  58. };
  59. return dep;
  60. }),
  61. this.options.name
  62. ),
  63. this.options,
  64. (error) => {
  65. if (error) return callback(error);
  66. callback();
  67. }
  68. );
  69. });
  70. }
  71. }
  72. module.exports = DllEntryPlugin;