JsonpLibraryPlugin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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("../Compiler")} Compiler */
  14. /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
  15. /** @typedef {import("../util/Hash")} Hash */
  16. /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
  17. /**
  18. * @typedef {object} JsonpLibraryPluginOptions
  19. * @property {LibraryType} type
  20. */
  21. /**
  22. * @typedef {object} JsonpLibraryPluginParsed
  23. * @property {string} name
  24. */
  25. /**
  26. * @typedef {JsonpLibraryPluginParsed} T
  27. * @extends {AbstractLibraryPlugin<JsonpLibraryPluginParsed>}
  28. */
  29. class JsonpLibraryPlugin extends AbstractLibraryPlugin {
  30. /**
  31. * @param {JsonpLibraryPluginOptions} options the plugin options
  32. */
  33. constructor(options) {
  34. super({
  35. pluginName: "JsonpLibraryPlugin",
  36. type: options.type
  37. });
  38. }
  39. /**
  40. * @param {LibraryOptions} library normalized library option
  41. * @returns {T | false} preprocess as needed by overriding
  42. */
  43. parseOptions(library) {
  44. const { name } = library;
  45. if (typeof name !== "string") {
  46. throw new Error(
  47. `Jsonp library name must be a simple string. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  48. );
  49. }
  50. return {
  51. name: /** @type {string} */ (name)
  52. };
  53. }
  54. /**
  55. * @param {Source} source source
  56. * @param {RenderContext} renderContext render context
  57. * @param {LibraryContext<T>} libraryContext context
  58. * @returns {Source} source with library export
  59. */
  60. render(source, { chunk }, { options, compilation }) {
  61. const name = compilation.getPath(options.name, {
  62. chunk
  63. });
  64. return new ConcatSource(`${name}(`, source, ")");
  65. }
  66. /**
  67. * @param {Chunk} chunk the chunk
  68. * @param {Hash} hash hash
  69. * @param {ChunkHashContext} chunkHashContext chunk hash context
  70. * @param {LibraryContext<T>} libraryContext context
  71. * @returns {void}
  72. */
  73. chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
  74. hash.update("JsonpLibraryPlugin");
  75. hash.update(compilation.getPath(options.name, { chunk }));
  76. }
  77. }
  78. module.exports = JsonpLibraryPlugin;