JsonpExportMainTemplatePlugin.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. class JsonpExportMainTemplatePlugin {
  8. constructor(name) {
  9. this.name = name;
  10. }
  11. apply(compilation) {
  12. const { mainTemplate, chunkTemplate } = compilation;
  13. const onRenderWithEntry = (source, chunk, hash) => {
  14. const name = mainTemplate.getAssetPath(this.name || "", {
  15. hash,
  16. chunk
  17. });
  18. return new ConcatSource(`${name}(`, source, ");");
  19. };
  20. for (const template of [mainTemplate, chunkTemplate]) {
  21. template.hooks.renderWithEntry.tap(
  22. "JsonpExportMainTemplatePlugin",
  23. onRenderWithEntry
  24. );
  25. }
  26. mainTemplate.hooks.globalHashPaths.tap(
  27. "JsonpExportMainTemplatePlugin",
  28. paths => {
  29. if (this.name) paths.push(this.name);
  30. return paths;
  31. }
  32. );
  33. mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => {
  34. hash.update("jsonp export");
  35. hash.update(`${this.name}`);
  36. });
  37. }
  38. }
  39. module.exports = JsonpExportMainTemplatePlugin;