You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

4 years ago
  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 LibManifestPlugin = require("./LibManifestPlugin");
  8. const FlagInitialModulesAsUsedPlugin = require("./FlagInitialModulesAsUsedPlugin");
  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 FlagInitialModulesAsUsedPlugin("DllPlugin").apply(compiler);
  40. }
  41. }
  42. }
  43. module.exports = DllPlugin;