@@ -20,6 +20,13 @@ class DevServerPlugin {
2020 * @typedef {(string[] | string | Object<string | string[],string>) } Entry
2121 */
2222
23+ /**
24+ * Additional entry to add to specific chunk
25+ * @typedef {Object } AdditionalChunkEntry
26+ * @property {Entry } entry
27+ * @property {string[] } [chunks]
28+ */
29+
2330 /**
2431 * Apply the plugin
2532 * @param {Object } compiler the compiler instance
@@ -66,7 +73,7 @@ class DevServerPlugin {
6673 /**
6774 * prependEntry Method for webpack 4
6875 * @param {Entry } originalEntry
69- * @param {Entry } additionalEntries
76+ * @param {AdditionalChunkEntry[] } additionalEntries
7077 * @returns {Entry }
7178 */
7279 const prependEntry = ( originalEntry , additionalEntries ) => {
@@ -83,8 +90,13 @@ class DevServerPlugin {
8390
8491 Object . keys ( originalEntry ) . forEach ( ( key ) => {
8592 // entry[key] should be a string here
93+ const chunkAdditionalEntries = additionalEntries . filter (
94+ ( additionalEntry ) =>
95+ ! additionalEntry . chunks || additionalEntry . chunks . includes ( key )
96+ ) ;
97+
8698 const entryDescription = originalEntry [ key ] ;
87- clone [ key ] = prependEntry ( entryDescription , additionalEntries ) ;
99+ clone [ key ] = prependEntry ( entryDescription , chunkAdditionalEntries ) ;
88100 } ) ;
89101
90102 return clone ;
@@ -93,13 +105,15 @@ class DevServerPlugin {
93105 // in this case, entry is a string or an array.
94106 // make sure that we do not add duplicates.
95107 /** @type {Entry } */
96- const entriesClone = additionalEntries . slice ( 0 ) ;
108+ const newEntries = additionalEntries . map (
109+ ( additionalEntry ) => additionalEntry . entry
110+ ) ;
97111 [ ] . concat ( originalEntry ) . forEach ( ( newEntry ) => {
98- if ( ! entriesClone . includes ( newEntry ) ) {
99- entriesClone . push ( newEntry ) ;
112+ if ( ! newEntries . includes ( newEntry ) ) {
113+ newEntries . push ( newEntry ) ;
100114 }
101115 } ) ;
102- return entriesClone ;
116+ return newEntries ;
103117 } ;
104118
105119 /**
@@ -112,14 +126,15 @@ class DevServerPlugin {
112126
113127 /**
114128 *
115- * @param {Boolean | checkInjectOptionsParam } option - inject(Hot|Client) it is Boolean | fn => Boolean
129+ * @param {Boolean | string[] | checkInjectOptionsParam } option - inject(Hot|Client) it is Boolean | fn => Boolean
116130 * @param {Object } _config
117131 * @param {Boolean } defaultValue
118- * @return {Boolean }
132+ * @return {Boolean | string[] }
119133 */
120134 // eslint-disable-next-line no-shadow
121135 const checkInject = ( option , _config , defaultValue ) => {
122136 if ( typeof option === 'boolean' ) return option ;
137+ if ( Array . isArray ( option ) ) return option ;
123138 if ( typeof option === 'function' ) return option ( _config ) ;
124139 return defaultValue ;
125140 } ;
@@ -138,37 +153,86 @@ class DevServerPlugin {
138153 undefined ,
139154 null ,
140155 ] . includes ( compilerOptions . target ) ;
141- /** @type {Entry } */
142- const additionalEntries = checkInject (
156+ /** @type {AdditionalChunkEntry[] } */
157+ const additionalEntries = [ ] ;
158+
159+ const checkInjectClientResult = checkInject (
143160 options . injectClient ,
144161 compilerOptions ,
145162 isWebTarget
146- )
147- ? [ clientEntry ]
148- : [ ] ;
163+ ) ;
164+ if ( checkInjectClientResult ) {
165+ additionalEntries . push ( {
166+ entry : clientEntry ,
167+ chunks : Array . isArray ( checkInjectClientResult )
168+ ? checkInjectClientResult
169+ : null ,
170+ } ) ;
171+ }
149172
150- if ( hotEntry && checkInject ( options . injectHot , compilerOptions , true ) ) {
151- additionalEntries . push ( hotEntry ) ;
173+ if ( hotEntry ) {
174+ const checkInjectHotResult = checkInject (
175+ options . injectHot ,
176+ compilerOptions ,
177+ true
178+ ) ;
179+ if ( checkInjectHotResult ) {
180+ additionalEntries . push ( {
181+ entry : hotEntry ,
182+ chunks : Array . isArray ( checkInjectHotResult )
183+ ? checkInjectHotResult
184+ : null ,
185+ } ) ;
186+ }
152187 }
153188
154189 // use a hook to add entries if available
155190 if ( EntryPlugin ) {
156191 compiler . hooks . make . tapPromise ( 'DevServerPlugin' , ( compilation ) =>
157192 Promise . all (
158- additionalEntries . map (
159- ( entry ) =>
160- new Promise ( ( resolve , reject ) => {
161- compilation . addEntry (
162- compiler . context ,
163- EntryPlugin . createDependency ( entry , { } ) ,
164- { } , // global entry
165- ( err ) => {
166- if ( err ) return reject ( err ) ;
167- resolve ( ) ;
168- }
193+ additionalEntries . map ( ( additionalChunkEntry ) => {
194+ // add entry to existing chunks
195+ if (
196+ additionalChunkEntry . chunks &&
197+ Array . isArray ( additionalChunkEntry . chunks )
198+ ) {
199+ let promise = Promise . resolve ( ) ;
200+ additionalChunkEntry . chunks . forEach ( ( chunkName ) => {
201+ promise = promise . then (
202+ ( ) =>
203+ new Promise ( ( resolve , reject ) => {
204+ compilation . addEntry (
205+ compiler . context ,
206+ EntryPlugin . createDependency (
207+ additionalChunkEntry . entry ,
208+ { }
209+ ) ,
210+ chunkName ,
211+ ( err ) => {
212+ if ( err ) return reject ( err ) ;
213+ resolve ( ) ;
214+ }
215+ ) ;
216+ } )
169217 ) ;
170- } )
171- )
218+ } ) ;
219+
220+ return promise ;
221+ }
222+
223+ // add new entry
224+ return new Promise ( ( resolve , reject ) => {
225+ compilation . addEntry (
226+ compiler . context ,
227+ EntryPlugin . createDependency ( additionalChunkEntry . entry , { } ) ,
228+ { } , // global entry
229+ ( err ) => {
230+ if ( err ) return reject ( err ) ;
231+ resolve ( ) ;
232+ }
233+ ) ;
234+ } ) ;
235+ } )
172236 )
173237 ) ;
174238 } else {
0 commit comments