DllPlugin.js 2.0 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 DllEntryPlugin = require("./DllEntryPlugin");
  7. const FlagAllModulesAsUsedPlugin = require("./FlagAllModulesAsUsedPlugin");
  8. const LibManifestPlugin = require("./LibManifestPlugin");
  9. /** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
  10. /** @typedef {import("./Compiler")} Compiler */
  11. /** @typedef {import("./DllEntryPlugin").Entries} Entries */
  12. /** @typedef {import("./DllEntryPlugin").Options} Options */
  13. const PLUGIN_NAME = "DllPlugin";
  14. class DllPlugin {
  15. /**
  16. * Creates an instance of DllPlugin.
  17. * @param {DllPluginOptions} options options object
  18. */
  19. constructor(options) {
  20. /** @type {DllPluginOptions} */
  21. this.options = options;
  22. }
  23. /**
  24. * Applies the plugin by registering its hooks on the compiler.
  25. * @param {Compiler} compiler the compiler instance
  26. * @returns {void}
  27. */
  28. apply(compiler) {
  29. compiler.hooks.validate.tap(PLUGIN_NAME, () => {
  30. compiler.validate(
  31. () => require("../schemas/plugins/DllPlugin.json"),
  32. this.options,
  33. {
  34. name: "Dll Plugin",
  35. baseDataPath: "options"
  36. },
  37. (options) => require("../schemas/plugins/DllPlugin.check")(options)
  38. );
  39. });
  40. const entryOnly = this.options.entryOnly !== false;
  41. compiler.hooks.entryOption.tap(PLUGIN_NAME, (context, entry) => {
  42. if (typeof entry !== "function") {
  43. for (const name of Object.keys(entry)) {
  44. /** @type {Options} */
  45. const options = { name };
  46. new DllEntryPlugin(
  47. context,
  48. /** @type {Entries} */
  49. (entry[name].import),
  50. options
  51. ).apply(compiler);
  52. }
  53. } else {
  54. throw new Error(
  55. `${PLUGIN_NAME} doesn't support dynamic entry (function) yet`
  56. );
  57. }
  58. return true;
  59. });
  60. new LibManifestPlugin({ ...this.options, entryOnly }).apply(compiler);
  61. if (!entryOnly) {
  62. new FlagAllModulesAsUsedPlugin(PLUGIN_NAME).apply(compiler);
  63. }
  64. }
  65. }
  66. module.exports = DllPlugin;