22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5- "use strict" ;
5+ /** @typedef {import("./Module") } Module */
6+ /** @typedef {import("./Chunk") } Chunk */
7+ /** @typedef {import("./ModuleTemplate") } ModuleTemplate */
8+ /** @typedef {import("webpack-sources").ConcatSource } ConcatSource */
69
710const { ConcatSource } = require ( "webpack-sources" ) ;
11+ const HotUpdateChunk = require ( "./HotUpdateChunk" ) ;
812
913const START_LOWERCASE_ALPHABET_CODE = "a" . charCodeAt ( 0 ) ;
1014const START_UPPERCASE_ALPHABET_CODE = "A" . charCodeAt ( 0 ) ;
@@ -18,6 +22,20 @@ const COMMENT_END_REGEX = /\*\//g;
1822const PATH_NAME_NORMALIZE_REPLACE_REGEX = / [ ^ a - z A - Z 0 - 9 _ ! § $ ( ) = \- ^ ° ] + / g;
1923const MATCH_PADDED_HYPHENS_REPLACE_REGEX = / ^ - | - $ / g;
2024
25+ /**
26+ * @typedef {Object } HasId
27+ * @property {number } id
28+ * */
29+
30+ /**
31+ * @typedef {(m: Module, idx: number) => boolean } ModuleFilterPredicate
32+ */
33+
34+ /**
35+ * @param {HasId } a first id object to be sorted
36+ * @param {HasId } b second id object to be sorted against
37+ * @return {-1|0|1 } the sort value
38+ */
2139const stringifyIdSortPredicate = ( a , b ) => {
2240 var aId = a . id + "" ;
2341 var bId = b . id + "" ;
@@ -26,36 +44,63 @@ const stringifyIdSortPredicate = (a, b) => {
2644 return 0 ;
2745} ;
2846
47+ //TODO: Change type to Module when import works
48+ //https://github.com/Microsoft/TypeScript/issues/23375
49+ /**
50+ * @param {HasId } module the module to compare against
51+ * @return {boolean } return true if module.id is equal to type "number"
52+ */
2953const moduleIdIsNumber = module => {
3054 return typeof module . id === "number" ;
3155} ;
3256
33- module . exports = class Template {
57+ class Template {
58+ /**
59+ *
60+ * @param {Function } fn - a runtime function (.runtime.js) "template"
61+ * @return {string } the updated and normalized function string
62+ */
3463 static getFunctionContent ( fn ) {
3564 return fn
3665 . toString ( )
3766 . replace ( FUNCTION_CONTENT_REGEX , "" )
3867 . replace ( INDENT_MULTILINE_REGEX , "" )
3968 . replace ( LINE_SEPARATOR_REGEX , "\n" ) ;
4069 }
41-
70+ /**
71+ * @param {string } str the string converted to identifier
72+ * @return {string } created identifier
73+ */
4274 static toIdentifier ( str ) {
4375 if ( typeof str !== "string" ) return "" ;
4476 return str
4577 . replace ( IDENTIFIER_NAME_REPLACE_REGEX , "_$1" )
4678 . replace ( IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX , "_" ) ;
4779 }
48-
80+ /**
81+ *
82+ * @param {string } str string to be converted to commented in bundle code
83+ * @return {string } returns a commented version of string
84+ */
4985 static toComment ( str ) {
5086 if ( ! str ) return "" ;
5187 return `/*! ${ str . replace ( COMMENT_END_REGEX , "* /" ) } */` ;
5288 }
5389
90+ /**
91+ *
92+ * @param {string } str string to be converted to "normal comment"
93+ * @return {string } returns a commented version of string
94+ */
5495 static toNormalComment ( str ) {
5596 if ( ! str ) return "" ;
5697 return `/* ${ str . replace ( COMMENT_END_REGEX , "* /" ) } */` ;
5798 }
5899
100+ /**
101+ * @param {string } str string path to be normalized
102+ * @return {string } normalized bundle-safe path
103+ */
59104 static toPath ( str ) {
60105 if ( typeof str !== "string" ) return "" ;
61106 return str
@@ -64,6 +109,11 @@ module.exports = class Template {
64109 }
65110
66111 // map number to a single character a-z, A-Z or <_ + number> if number is too big
112+ /**
113+ *
114+ * @param {number } n number to convert to ident
115+ * @return {string } returns single character ident
116+ */
67117 static numberToIdentifer ( n ) {
68118 // lower case
69119 if ( n < DELTA_A_TO_Z )
@@ -81,6 +131,11 @@ module.exports = class Template {
81131 ) ;
82132 }
83133
134+ /**
135+ *
136+ * @param {string | string[] } str string to convert to identity
137+ * @return {string } converted identity
138+ */
84139 static indent ( str ) {
85140 if ( Array . isArray ( str ) ) {
86141 return str . map ( Template . indent ) . join ( "\n" ) ;
@@ -92,6 +147,12 @@ module.exports = class Template {
92147 }
93148 }
94149
150+ /**
151+ *
152+ * @param {string|string[] } str string to create prefix for
153+ * @param {string } prefix prefix to compose
154+ * @return {string } returns new prefix string
155+ */
95156 static prefix ( str , prefix ) {
96157 if ( Array . isArray ( str ) ) {
97158 str = str . join ( "\n" ) ;
@@ -102,13 +163,24 @@ module.exports = class Template {
102163 return ind + str . replace ( / \n ( [ ^ \n ] ) / g, "\n" + prefix + "$1" ) ;
103164 }
104165
166+ /**
167+ *
168+ * @param {string|string[] } str string or string collection
169+ * @return {string } returns a single string from array
170+ */
105171 static asString ( str ) {
106172 if ( Array . isArray ( str ) ) {
107173 return str . join ( "\n" ) ;
108174 }
109175 return str ;
110176 }
111177
178+ /**
179+ *
180+ * @param {HasId[] } modules - a collection of modules to get array bounds for
181+ * @return {[number, number] | false } returns the upper and lower array bounds
182+ * or false if not every module has a number based id
183+ */
112184 static getModulesArrayBounds ( modules ) {
113185 if ( ! modules . every ( moduleIdIsNumber ) ) return false ;
114186 var maxId = - Infinity ;
@@ -133,6 +205,15 @@ module.exports = class Template {
133205 return arrayOverhead < objectOverhead ? [ minId , maxId ] : false ;
134206 }
135207
208+ /**
209+ *
210+ * @param {Chunk } chunk chunk whose modules will be rendered
211+ * @param {ModuleFilterPredicate } filterFn function used to filter modules from chunk to render
212+ * @param {ModuleTemplate } moduleTemplate ModuleTemplate instance used to render modules
213+ * @param {any | any[] } dependencyTemplates templates needed for each module to render dependencies
214+ * @param {string= } prefix applying prefix strings
215+ * @return {ConcatSource } rendered chunk modules in a Source object
216+ */
136217 static renderChunkModules (
137218 chunk ,
138219 filterFn ,
@@ -143,7 +224,9 @@ module.exports = class Template {
143224 if ( ! prefix ) prefix = "" ;
144225 var source = new ConcatSource ( ) ;
145226 const modules = chunk . getModules ( ) . filter ( filterFn ) ;
146- var removedModules = chunk . removedModules ;
227+ if ( chunk instanceof HotUpdateChunk ) {
228+ var removedModules = chunk . removedModules ;
229+ }
147230 if (
148231 modules . length === 0 &&
149232 ( ! removedModules || removedModules . length === 0 )
@@ -202,4 +285,6 @@ module.exports = class Template {
202285 }
203286 return source ;
204287 }
205- } ;
288+ }
289+
290+ module . exports = Template ;
0 commit comments