1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const ConcatSource = require("webpack-sources").ConcatSource;
|
8 |
|
9 | class JsonpChunkTemplatePlugin {
|
10 | apply(chunkTemplate) {
|
11 | chunkTemplate.hooks.render.tap(
|
12 | "JsonpChunkTemplatePlugin",
|
13 | (modules, chunk) => {
|
14 | const jsonpFunction = chunkTemplate.outputOptions.jsonpFunction;
|
15 | const globalObject = chunkTemplate.outputOptions.globalObject;
|
16 | const source = new ConcatSource();
|
17 | source.add(
|
18 | `(${globalObject}[${JSON.stringify(jsonpFunction)}] = ${
|
19 | globalObject
|
20 | }[${JSON.stringify(jsonpFunction)}] || []).push([${JSON.stringify(
|
21 | chunk.ids
|
22 | )},`
|
23 | );
|
24 | source.add(modules);
|
25 | const entries = [chunk.entryModule].filter(Boolean).map(m =>
|
26 | [m.id].concat(
|
27 | Array.from(chunk.groupsIterable)[0]
|
28 | .chunks.filter(c => c !== chunk)
|
29 | .map(c => c.id)
|
30 | )
|
31 | );
|
32 | if (entries.length > 0) {
|
33 | source.add(`,${JSON.stringify(entries)}`);
|
34 | }
|
35 | source.add("])");
|
36 | return source;
|
37 | }
|
38 | );
|
39 | chunkTemplate.hooks.hash.tap("JsonpChunkTemplatePlugin", hash => {
|
40 | hash.update("JsonpChunkTemplatePlugin");
|
41 | hash.update("4");
|
42 | hash.update(`${chunkTemplate.outputOptions.jsonpFunction}`);
|
43 | hash.update(`${chunkTemplate.outputOptions.globalObject}`);
|
44 | });
|
45 | }
|
46 | }
|
47 | module.exports = JsonpChunkTemplatePlugin;
|
48 |
|