JsonpLibraryPlugin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
  8. /** @typedef {import("webpack-sources").Source} Source */
  9. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  10. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  11. /** @typedef {import("../Chunk")} Chunk */
  12. /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
  13. /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
  14. /** @typedef {import("../util/Hash")} Hash */
  15. /**
  16. * Defines the shared type used by this module.
  17. * @template T
  18. * @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T>
  19. */
  20. /**
  21. * Defines the jsonp library plugin options type used by this module.
  22. * @typedef {object} JsonpLibraryPluginOptions
  23. * @property {LibraryType} type
  24. */
  25. /**
  26. * Defines the jsonp library plugin parsed type used by this module.
  27. * @typedef {object} JsonpLibraryPluginParsed
  28. * @property {string} name
  29. */
  30. /**
  31. * Represents the jsonp library plugin runtime component.
  32. * @typedef {JsonpLibraryPluginParsed} T
  33. * @extends {AbstractLibraryPlugin<JsonpLibraryPluginParsed>}
  34. */
  35. class JsonpLibraryPlugin extends AbstractLibraryPlugin {
  36. /**
  37. * Creates an instance of JsonpLibraryPlugin.
  38. * @param {JsonpLibraryPluginOptions} options the plugin options
  39. */
  40. constructor(options) {
  41. super({
  42. pluginName: "JsonpLibraryPlugin",
  43. type: options.type
  44. });
  45. }
  46. /**
  47. * Returns preprocess as needed by overriding.
  48. * @param {LibraryOptions} library normalized library option
  49. * @returns {T} preprocess as needed by overriding
  50. */
  51. parseOptions(library) {
  52. const { name } = library;
  53. if (typeof name !== "string") {
  54. throw new Error(
  55. `Jsonp library name must be a simple string. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  56. );
  57. }
  58. const _name = /** @type {string} */ (name);
  59. return {
  60. name: _name
  61. };
  62. }
  63. /**
  64. * Returns source with library export.
  65. * @param {Source} source source
  66. * @param {RenderContext} renderContext render context
  67. * @param {LibraryContext<T>} libraryContext context
  68. * @returns {Source} source with library export
  69. */
  70. render(source, { chunk }, { options, compilation }) {
  71. const name = compilation.getPath(options.name, {
  72. chunk
  73. });
  74. return new ConcatSource(`${name}(`, source, ")");
  75. }
  76. /**
  77. * Processes the provided chunk.
  78. * @param {Chunk} chunk the chunk
  79. * @param {Hash} hash hash
  80. * @param {ChunkHashContext} chunkHashContext chunk hash context
  81. * @param {LibraryContext<T>} libraryContext context
  82. * @returns {void}
  83. */
  84. chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
  85. hash.update("JsonpLibraryPlugin");
  86. hash.update(compilation.getPath(options.name, { chunk }));
  87. }
  88. }
  89. module.exports = JsonpLibraryPlugin;