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