JsonpLibraryPlugin.js 2.8 KB

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