JsonpExportMainTemplatePlugin.js 915 B

12345678910111213141516171819202122232425262728293031323334353637
  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. class JsonpExportMainTemplatePlugin {
  8. constructor(name) {
  9. this.name = name;
  10. }
  11. apply(compilation) {
  12. const mainTemplate = compilation.mainTemplate;
  13. compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
  14. const name = mainTemplate.applyPluginsWaterfall("asset-path", this.name || "", {
  15. hash: hash,
  16. chunk: chunk
  17. });
  18. return new ConcatSource(`${name}(`, source, ");");
  19. });
  20. mainTemplate.plugin("global-hash-paths", paths => {
  21. if(this.name) paths.push(this.name);
  22. return paths;
  23. });
  24. mainTemplate.plugin("hash", hash => {
  25. hash.update("jsonp export");
  26. hash.update(`${this.name}`);
  27. });
  28. }
  29. }
  30. module.exports = JsonpExportMainTemplatePlugin;