BannerPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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").BannerPluginArgument} BannerPluginArgument */
  12. /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
  13. /** @typedef {import("./Compiler")} Compiler */
  14. const validate = createSchemaValidation(
  15. require("../schemas/plugins/BannerPlugin.check.js"),
  16. () => require("../schemas/plugins/BannerPlugin.json"),
  17. {
  18. name: "Banner Plugin",
  19. baseDataPath: "options"
  20. }
  21. );
  22. const wrapComment = str => {
  23. if (!str.includes("\n")) {
  24. return Template.toComment(str);
  25. }
  26. return `/*!\n * ${str
  27. .replace(/\*\//g, "* /")
  28. .split("\n")
  29. .join("\n * ")
  30. .replace(/\s+\n/g, "\n")
  31. .trimEnd()}\n */`;
  32. };
  33. class BannerPlugin {
  34. /**
  35. * @param {BannerPluginArgument} options options object
  36. */
  37. constructor(options) {
  38. if (typeof options === "string" || typeof options === "function") {
  39. options = {
  40. banner: options
  41. };
  42. }
  43. validate(options);
  44. this.options = options;
  45. const bannerOption = options.banner;
  46. if (typeof bannerOption === "function") {
  47. const getBanner = bannerOption;
  48. this.banner = this.options.raw
  49. ? getBanner
  50. : data => wrapComment(getBanner(data));
  51. } else {
  52. const banner = this.options.raw
  53. ? bannerOption
  54. : wrapComment(bannerOption);
  55. this.banner = () => banner;
  56. }
  57. }
  58. /**
  59. * Apply the plugin
  60. * @param {Compiler} compiler the compiler instance
  61. * @returns {void}
  62. */
  63. apply(compiler) {
  64. const options = this.options;
  65. const banner = this.banner;
  66. const matchObject = ModuleFilenameHelpers.matchObject.bind(
  67. undefined,
  68. options
  69. );
  70. const cache = new WeakMap();
  71. compiler.hooks.compilation.tap("BannerPlugin", compilation => {
  72. compilation.hooks.processAssets.tap(
  73. {
  74. name: "BannerPlugin",
  75. stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
  76. },
  77. () => {
  78. for (const chunk of compilation.chunks) {
  79. if (options.entryOnly && !chunk.canBeInitial()) {
  80. continue;
  81. }
  82. for (const file of chunk.files) {
  83. if (!matchObject(file)) {
  84. continue;
  85. }
  86. const data = {
  87. chunk,
  88. filename: file
  89. };
  90. const comment = compilation.getPath(banner, data);
  91. compilation.updateAsset(file, old => {
  92. let cached = cache.get(old);
  93. if (!cached || cached.comment !== comment) {
  94. const source = options.footer
  95. ? new ConcatSource(old, "\n", comment)
  96. : new ConcatSource(comment, "\n", old);
  97. cache.set(old, { source, comment });
  98. return source;
  99. }
  100. return cached.source;
  101. });
  102. }
  103. }
  104. }
  105. );
  106. });
  107. }
  108. }
  109. module.exports = BannerPlugin;