@@ -16,6 +16,7 @@ Template.getFunctionContent = function(fn) {
1616} ;
1717
1818Template . toIdentifier = function ( str ) {
19+ if ( typeof str !== "string" ) return "" ;
1920 return str . replace ( / ^ [ ^ a - z A - Z $ _ ] / , "_" ) . replace ( / [ ^ a - z A - Z 0 - 9 $ _ ] / g, "_" ) ;
2021} ;
2122
@@ -47,24 +48,40 @@ Template.prototype.asString = function(str) {
4748 return str ;
4849} ;
4950
51+ Template . prototype . getModulesArrayBounds = function ( modules ) {
52+ if ( modules . some ( function ( module ) {
53+ return typeof module . id !== "number" ;
54+ } ) )
55+ return false ;
56+ var maxId = - Infinity ;
57+ var minId = Infinity ;
58+ modules . forEach ( function ( module ) {
59+ if ( maxId < module . id ) maxId = module . id ;
60+ if ( minId > module . id ) minId = module . id ;
61+ } ) ;
62+ if ( minId < 16 + ( "" + minId ) . length ) {
63+ // add minId x ',' instead of 'Array(minId).concat(...)'
64+ minId = 0 ;
65+ }
66+ var objectOverhead = modules . map ( function ( module ) {
67+ return ( module . id + "" ) . length + 2 ;
68+ } ) . reduce ( function ( a , b ) { return a + b ; } , - 1 ) ;
69+ var arrayOverhead = minId === 0 ? maxId : 16 + ( "" + minId ) . length + maxId ;
70+ return arrayOverhead < objectOverhead ? [ minId , maxId ] : false ;
71+ } ;
72+
5073Template . prototype . renderChunkModules = function ( chunk , moduleTemplate , dependencyTemplates , prefix ) {
5174 if ( ! prefix ) prefix = "" ;
5275 var source = new ConcatSource ( ) ;
5376 if ( chunk . modules . length === 0 ) {
5477 source . add ( "[]" ) ;
5578 return source ;
5679 }
57- var maxId = chunk . modules [ chunk . modules . length - 1 ] . id ;
58- var minId = chunk . modules [ 0 ] . id ;
59- if ( minId < 16 + ( "" + minId ) . length ) {
60- minId = 0 ;
61- }
62- var objectOverhead = chunk . modules . map ( function ( module ) {
63- return ( module . id + "" ) . length + 2 ;
64- } ) . reduce ( function ( a , b ) { return a + b ; } , - 1 ) ;
65- var arrayOverhead = minId === 0 ? maxId : 16 + ( "" + minId ) . length + maxId ;
66- if ( arrayOverhead < objectOverhead ) {
67- // Render a sparse array
80+ var bounds = this . getModulesArrayBounds ( chunk . modules ) ;
81+ if ( bounds ) {
82+ // Render a spare array
83+ var minId = bounds [ 0 ] ;
84+ var maxId = bounds [ 1 ] ;
6885 if ( minId !== 0 ) source . add ( "Array(" + minId + ").concat(" ) ;
6986 source . add ( "[\n" ) ;
7087 var modules = { } ;
@@ -87,7 +104,7 @@ Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependen
87104 source . add ( "{\n" ) ;
88105 chunk . modules . forEach ( function ( module , idx ) {
89106 if ( idx !== 0 ) source . add ( ",\n" ) ;
90- source . add ( "\n/***/ " + module . id + ":\n" ) ;
107+ source . add ( "\n/***/ " + JSON . stringify ( module . id ) + ":\n" ) ;
91108 source . add ( moduleTemplate . render ( module , dependencyTemplates , chunk ) ) ;
92109 } ) ;
93110 source . add ( "\n\n" + prefix + "}" ) ;
0 commit comments