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