You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

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