DllEntryPlugin.js 1.7 KB

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