DllPlugin.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const validateOptions = require("schema-utils");
  10. const schema = require("../schemas/plugins/DllPlugin.json");
  11. /** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
  12. class DllPlugin {
  13. /**
  14. * @param {DllPluginOptions} options options object
  15. */
  16. constructor(options) {
  17. validateOptions(schema, options, "Dll Plugin");
  18. this.options = options;
  19. }
  20. apply(compiler) {
  21. compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => {
  22. const itemToPlugin = (item, name) => {
  23. if (Array.isArray(item)) {
  24. return new DllEntryPlugin(context, item, name);
  25. }
  26. throw new Error("DllPlugin: supply an Array as entry");
  27. };
  28. if (typeof entry === "object" && !Array.isArray(entry)) {
  29. Object.keys(entry).forEach(name => {
  30. itemToPlugin(entry[name], name).apply(compiler);
  31. });
  32. } else {
  33. itemToPlugin(entry, "main").apply(compiler);
  34. }
  35. return true;
  36. });
  37. new LibManifestPlugin(this.options).apply(compiler);
  38. if (!this.options.entryOnly) {
  39. new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler);
  40. }
  41. }
  42. }
  43. module.exports = DllPlugin;