a zip code crypto-currency system good for red ONLY

BannerPlugin.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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").ConcatSource;
  7. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  8. const wrapComment = (str) => {
  9. if(!str.includes("\n")) return `/*! ${str} */`;
  10. return `/*!\n * ${str.split("\n").join("\n * ")}\n */`;
  11. };
  12. class BannerPlugin {
  13. constructor(options) {
  14. if(arguments.length > 1)
  15. throw new Error("BannerPlugin only takes one argument (pass an options object)");
  16. if(typeof options === "string")
  17. options = {
  18. banner: options
  19. };
  20. this.options = options || {};
  21. this.banner = this.options.raw ? options.banner : wrapComment(options.banner);
  22. }
  23. apply(compiler) {
  24. const options = this.options;
  25. const banner = this.banner;
  26. compiler.plugin("compilation", (compilation) => {
  27. compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
  28. chunks.forEach((chunk) => {
  29. if(options.entryOnly && !chunk.isInitial()) return;
  30. chunk.files
  31. .filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
  32. .forEach((file) => {
  33. let basename;
  34. let query = "";
  35. let filename = file;
  36. const hash = compilation.hash;
  37. const querySplit = filename.indexOf("?");
  38. if(querySplit >= 0) {
  39. query = filename.substr(querySplit);
  40. filename = filename.substr(0, querySplit);
  41. }
  42. if(filename.indexOf("/") < 0) {
  43. basename = filename;
  44. } else {
  45. basename = filename.substr(filename.lastIndexOf("/") + 1);
  46. }
  47. const comment = compilation.getPath(banner, {
  48. hash,
  49. chunk,
  50. filename,
  51. basename,
  52. query,
  53. });
  54. return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
  55. });
  56. });
  57. callback();
  58. });
  59. });
  60. }
  61. }
  62. module.exports = BannerPlugin;