10
0

BannerPlugin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource } = require("webpack-sources");
  7. const Compilation = require("./Compilation");
  8. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  9. const Template = require("./Template");
  10. const createSchemaValidation = require("./util/create-schema-validation");
  11. /** @typedef {import("../declarations/plugins/BannerPlugin").BannerFunction} BannerFunction */
  12. /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */
  13. /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
  14. /** @typedef {import("./Compilation").PathData} PathData */
  15. /** @typedef {import("./Compiler")} Compiler */
  16. /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
  17. const validate = createSchemaValidation(
  18. /** @type {(function(typeof import("../schemas/plugins/BannerPlugin.json")): boolean)} */
  19. (require("../schemas/plugins/BannerPlugin.check.js")),
  20. () => require("../schemas/plugins/BannerPlugin.json"),
  21. {
  22. name: "Banner Plugin",
  23. baseDataPath: "options"
  24. }
  25. );
  26. /**
  27. * @param {string} str string to wrap
  28. * @returns {string} wrapped string
  29. */
  30. const wrapComment = str => {
  31. if (!str.includes("\n")) {
  32. return Template.toComment(str);
  33. }
  34. return `/*!\n * ${str
  35. .replace(/\*\//g, "* /")
  36. .split("\n")
  37. .join("\n * ")
  38. .replace(/\s+\n/g, "\n")
  39. .trimEnd()}\n */`;
  40. };
  41. class BannerPlugin {
  42. /**
  43. * @param {BannerPluginArgument} options options object
  44. */
  45. constructor(options) {
  46. if (typeof options === "string" || typeof options === "function") {
  47. options = {
  48. banner: options
  49. };
  50. }
  51. validate(options);
  52. this.options = options;
  53. const bannerOption = options.banner;
  54. if (typeof bannerOption === "function") {
  55. const getBanner = bannerOption;
  56. /** @type {BannerFunction} */
  57. this.banner = this.options.raw
  58. ? getBanner
  59. : /** @type {BannerFunction} */ data => wrapComment(getBanner(data));
  60. } else {
  61. const banner = this.options.raw
  62. ? bannerOption
  63. : wrapComment(bannerOption);
  64. /** @type {BannerFunction} */
  65. this.banner = () => banner;
  66. }
  67. }
  68. /**
  69. * Apply the plugin
  70. * @param {Compiler} compiler the compiler instance
  71. * @returns {void}
  72. */
  73. apply(compiler) {
  74. const options = this.options;
  75. const banner = this.banner;
  76. const matchObject = ModuleFilenameHelpers.matchObject.bind(
  77. undefined,
  78. options
  79. );
  80. const cache = new WeakMap();
  81. const stage =
  82. this.options.stage || Compilation.PROCESS_ASSETS_STAGE_ADDITIONS;
  83. compiler.hooks.compilation.tap("BannerPlugin", compilation => {
  84. compilation.hooks.processAssets.tap(
  85. {
  86. name: "BannerPlugin",
  87. stage
  88. },
  89. () => {
  90. for (const chunk of compilation.chunks) {
  91. if (options.entryOnly && !chunk.canBeInitial()) {
  92. continue;
  93. }
  94. for (const file of chunk.files) {
  95. if (!matchObject(file)) {
  96. continue;
  97. }
  98. /** @type {PathData} */
  99. const data = { chunk, filename: file };
  100. const comment = compilation.getPath(
  101. /** @type {TemplatePath} */
  102. (banner),
  103. data
  104. );
  105. compilation.updateAsset(file, old => {
  106. const cached = cache.get(old);
  107. if (!cached || cached.comment !== comment) {
  108. const source = options.footer
  109. ? new ConcatSource(old, "\n", comment)
  110. : new ConcatSource(comment, "\n", old);
  111. cache.set(old, { source, comment });
  112. return source;
  113. }
  114. return cached.source;
  115. });
  116. }
  117. }
  118. }
  119. );
  120. });
  121. }
  122. }
  123. module.exports = BannerPlugin;