22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5+ var nextIdent = 0 ;
6+
57function CommonsChunkPlugin ( options ) {
68 if ( arguments . length > 1 )
79 throw new Error ( "CommonsChunkPlugin only takes one argument (pass an options object)" ) ;
@@ -17,7 +19,9 @@ function CommonsChunkPlugin(options) {
1719 if ( options . children ) this . selectedChunks = false ;
1820 this . async = options . async ;
1921 this . minSize = options . minSize ;
22+ this . ident = __filename + ( nextIdent ++ ) ;
2023}
24+
2125module . exports = CommonsChunkPlugin ;
2226CommonsChunkPlugin . prototype . apply = function ( compiler ) {
2327 var chunkNames = this . chunkNames ;
@@ -26,26 +30,29 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
2630 var selectedChunks = this . selectedChunks ;
2731 var async = this . async ;
2832 var minSize = this . minSize ;
33+ var ident = this . ident ;
2934 compiler . plugin ( "this-compilation" , function ( compilation ) {
3035 compilation . plugin ( [ "optimize-chunks" , "optimize-extracted-chunks" ] , function ( chunks ) {
36+ // only optimize once
37+ if ( compilation [ ident ] ) return ;
38+ compilation [ ident ] = true ;
39+
3140 var commonChunks ;
3241 if ( ! chunkNames && ( selectedChunks === false || async ) ) {
3342 commonChunks = chunks ;
34- } else if ( Array . isArray ( chunkNames ) ) {
35- commonChunks = chunkNames . map ( function ( chunkName ) {
36- return chunks . filter ( function ( chunk ) {
43+ } else if ( Array . isArray ( chunkNames ) || typeof chunkNames === "string" ) {
44+ commonChunks = [ ] . concat ( chunkNames ) . map ( function ( chunkName ) {
45+ var chunk = chunks . filter ( function ( chunk ) {
3746 return chunk . name === chunkName ;
3847 } ) [ 0 ] ;
39- } ) ;
48+ if ( ! chunk ) {
49+ chunk = this . addChunk ( chunkName ) ;
50+ chunk . initial = chunk . entry = true ;
51+ }
52+ return chunk ;
53+ } , this ) ;
4054 } else {
41- commonChunks = chunks . filter ( function ( chunk ) {
42- return chunk . name === chunkNames ;
43- } ) ;
44- }
45- if ( commonChunks . length === 0 ) {
46- var chunk = this . addChunk ( chunkNames ) ;
47- chunk . initial = chunk . entry = true ;
48- commonChunks = [ chunk ] ;
55+ throw new Error ( "Invalid chunkNames argument" ) ;
4956 }
5057 commonChunks . forEach ( function processCommonChunk ( commonChunk , idx ) {
5158 var commonModulesCount = [ ] ;
@@ -155,6 +162,7 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
155162 if ( filenameTemplate )
156163 commonChunk . filenameTemplate = filenameTemplate ;
157164 } , this ) ;
165+ return true ;
158166 } ) ;
159167 } ) ;
160168} ;
0 commit comments