JsonpLibraryPlugin.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. const _name = /** @type {string} */ (name);
  51. return {
  52. name: _name
  53. };
  54. }
  55. /**
  56. * @param {Source} source source
  57. * @param {RenderContext} renderContext render context
  58. * @param {LibraryContext<T>} libraryContext context
  59. * @returns {Source} source with library export
  60. */
  61. render(source, { chunk }, { options, compilation }) {
  62. const name = compilation.getPath(options.name, {
  63. chunk
  64. });
  65. return new ConcatSource(`${name}(`, source, ")");
  66. }
  67. /**
  68. * @param {Chunk} chunk the chunk
  69. * @param {Hash} hash hash
  70. * @param {ChunkHashContext} chunkHashContext chunk hash context
  71. * @param {LibraryContext<T>} libraryContext context
  72. * @returns {void}
  73. */
  74. chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
  75. hash.update("JsonpLibraryPlugin");
  76. hash.update(compilation.getPath(options.name, { chunk }));
  77. }
  78. }
  79. module.exports = JsonpLibraryPlugin;