DllEntryPlugin.js 1.8 KB

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