LibraryTemplatePlugin.js 1.6 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 EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
  7. /** @typedef {import("../declarations/WebpackOptions").AuxiliaryComment} AuxiliaryComment */
  8. /** @typedef {import("../declarations/WebpackOptions").LibraryExport} LibraryExport */
  9. /** @typedef {import("../declarations/WebpackOptions").LibraryName} LibraryName */
  10. /** @typedef {import("../declarations/WebpackOptions").LibraryType} LibraryType */
  11. /** @typedef {import("../declarations/WebpackOptions").UmdNamedDefine} UmdNamedDefine */
  12. /** @typedef {import("./Compiler")} Compiler */
  13. // TODO webpack 6 remove
  14. class LibraryTemplatePlugin {
  15. /**
  16. * Creates an instance of LibraryTemplatePlugin.
  17. * @param {LibraryName} name name of library
  18. * @param {LibraryType} target type of library
  19. * @param {UmdNamedDefine} umdNamedDefine setting this to true will name the UMD module
  20. * @param {AuxiliaryComment} auxiliaryComment comment in the UMD wrapper
  21. * @param {LibraryExport} exportProperty which export should be exposed as library
  22. */
  23. constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
  24. this.library = {
  25. type: target || "var",
  26. name,
  27. umdNamedDefine,
  28. auxiliaryComment,
  29. export: exportProperty
  30. };
  31. }
  32. /**
  33. * Applies the plugin by registering its hooks on the compiler.
  34. * @param {Compiler} compiler the compiler instance
  35. * @returns {void}
  36. */
  37. apply(compiler) {
  38. const { output } = compiler.options;
  39. output.library = this.library;
  40. new EnableLibraryPlugin(this.library.type).apply(compiler);
  41. }
  42. }
  43. module.exports = LibraryTemplatePlugin;