1
+ import configure from '../../configure'
2
+
3
+ const { ConcatSource } = require ( "webpack-sources" ) ;
4
+
5
+ const getEntryInfo = chunk => {
6
+ return [ chunk . entryModule ] . filter ( Boolean ) . map ( m =>
7
+ [ m . id ] . concat (
8
+ Array . from ( chunk . groupsIterable ) [ 0 ]
9
+ // @ts -ignore
10
+ . chunks . filter ( c => c !== chunk )
11
+ . map ( c => c . id )
12
+ )
13
+ ) ;
14
+ } ;
15
+
16
+
17
+ export default class MpJsonpChunkTemplatePlugin {
18
+ /**
19
+ * @param {ChunkTemplate } chunkTemplate the chunk template
20
+ * @returns {void }
21
+ */
22
+ apply ( chunkTemplate ) {
23
+ chunkTemplate . hooks . render . tap (
24
+ "JsonpChunkTemplatePlugin" ,
25
+ ( modules , chunk ) => {
26
+ const jsonpFunction = chunkTemplate . outputOptions . jsonpFunction ;
27
+
28
+ const source = new ConcatSource ( ) ;
29
+ const prefetchChunks = chunk . getChildIdsByOrders ( ) . prefetch ;
30
+ source . add (
31
+ `(${ configure . mpGlobalObject } [${ JSON . stringify (
32
+ jsonpFunction
33
+ ) } ] = ${ configure . mpGlobalObject } [${ JSON . stringify (
34
+ jsonpFunction
35
+ ) } ] || []).push([${ JSON . stringify ( chunk . ids ) } ,`
36
+ ) ;
37
+ source . add ( modules ) ;
38
+ const entries = getEntryInfo ( chunk ) ;
39
+ if ( entries . length > 0 ) {
40
+ source . add ( `,${ JSON . stringify ( entries ) } ` ) ;
41
+ } else if ( prefetchChunks && prefetchChunks . length ) {
42
+ source . add ( `,0` ) ;
43
+ }
44
+
45
+ if ( prefetchChunks && prefetchChunks . length ) {
46
+ source . add ( `,${ JSON . stringify ( prefetchChunks ) } ` ) ;
47
+ }
48
+ source . add ( "])" ) ;
49
+ return source ;
50
+ }
51
+ ) ;
52
+ chunkTemplate . hooks . hash . tap ( "JsonpChunkTemplatePlugin" , hash => {
53
+ hash . update ( "JsonpChunkTemplatePlugin" ) ;
54
+ hash . update ( "4" ) ;
55
+ hash . update ( `${ chunkTemplate . outputOptions . jsonpFunction } ` ) ;
56
+ hash . update ( `${ configure . mpGlobalObject } ` ) ;
57
+ } ) ;
58
+ chunkTemplate . hooks . hashForChunk . tap (
59
+ "JsonpChunkTemplatePlugin" ,
60
+ ( hash , chunk ) => {
61
+ hash . update ( JSON . stringify ( getEntryInfo ( chunk ) ) ) ;
62
+ hash . update ( JSON . stringify ( chunk . getChildIdsByOrders ( ) . prefetch ) || "" ) ;
63
+ }
64
+ ) ;
65
+ }
66
+ }
0 commit comments