2
2
MIT License http://www.opensource.org/licenses/mit-license.php
3
3
Author Tobias Koppers @sokra
4
4
*/
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 */
6
9
7
10
const { ConcatSource } = require ( "webpack-sources" ) ;
11
+ const HotUpdateChunk = require ( "./HotUpdateChunk" ) ;
8
12
9
13
const START_LOWERCASE_ALPHABET_CODE = "a" . charCodeAt ( 0 ) ;
10
14
const START_UPPERCASE_ALPHABET_CODE = "A" . charCodeAt ( 0 ) ;
@@ -18,6 +22,20 @@ const COMMENT_END_REGEX = /\*\//g;
18
22
const PATH_NAME_NORMALIZE_REPLACE_REGEX = / [ ^ a - z A - Z 0 - 9 _ ! § $ ( ) = \- ^ ° ] + / g;
19
23
const MATCH_PADDED_HYPHENS_REPLACE_REGEX = / ^ - | - $ / g;
20
24
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
+ */
21
39
const stringifyIdSortPredicate = ( a , b ) => {
22
40
var aId = a . id + "" ;
23
41
var bId = b . id + "" ;
@@ -26,36 +44,63 @@ const stringifyIdSortPredicate = (a, b) => {
26
44
return 0 ;
27
45
} ;
28
46
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
+ */
29
53
const moduleIdIsNumber = module => {
30
54
return typeof module . id === "number" ;
31
55
} ;
32
56
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
+ */
34
63
static getFunctionContent ( fn ) {
35
64
return fn
36
65
. toString ( )
37
66
. replace ( FUNCTION_CONTENT_REGEX , "" )
38
67
. replace ( INDENT_MULTILINE_REGEX , "" )
39
68
. replace ( LINE_SEPARATOR_REGEX , "\n" ) ;
40
69
}
41
-
70
+ /**
71
+ * @param {string } str the string converted to identifier
72
+ * @return {string } created identifier
73
+ */
42
74
static toIdentifier ( str ) {
43
75
if ( typeof str !== "string" ) return "" ;
44
76
return str
45
77
. replace ( IDENTIFIER_NAME_REPLACE_REGEX , "_$1" )
46
78
. replace ( IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX , "_" ) ;
47
79
}
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
+ */
49
85
static toComment ( str ) {
50
86
if ( ! str ) return "" ;
51
87
return `/*! ${ str . replace ( COMMENT_END_REGEX , "* /" ) } */` ;
52
88
}
53
89
90
+ /**
91
+ *
92
+ * @param {string } str string to be converted to "normal comment"
93
+ * @return {string } returns a commented version of string
94
+ */
54
95
static toNormalComment ( str ) {
55
96
if ( ! str ) return "" ;
56
97
return `/* ${ str . replace ( COMMENT_END_REGEX , "* /" ) } */` ;
57
98
}
58
99
100
+ /**
101
+ * @param {string } str string path to be normalized
102
+ * @return {string } normalized bundle-safe path
103
+ */
59
104
static toPath ( str ) {
60
105
if ( typeof str !== "string" ) return "" ;
61
106
return str
@@ -64,6 +109,11 @@ module.exports = class Template {
64
109
}
65
110
66
111
// 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
+ */
67
117
static numberToIdentifer ( n ) {
68
118
// lower case
69
119
if ( n < DELTA_A_TO_Z )
@@ -81,6 +131,11 @@ module.exports = class Template {
81
131
) ;
82
132
}
83
133
134
+ /**
135
+ *
136
+ * @param {string | string[] } str string to convert to identity
137
+ * @return {string } converted identity
138
+ */
84
139
static indent ( str ) {
85
140
if ( Array . isArray ( str ) ) {
86
141
return str . map ( Template . indent ) . join ( "\n" ) ;
@@ -92,6 +147,12 @@ module.exports = class Template {
92
147
}
93
148
}
94
149
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
+ */
95
156
static prefix ( str , prefix ) {
96
157
if ( Array . isArray ( str ) ) {
97
158
str = str . join ( "\n" ) ;
@@ -102,13 +163,24 @@ module.exports = class Template {
102
163
return ind + str . replace ( / \n ( [ ^ \n ] ) / g, "\n" + prefix + "$1" ) ;
103
164
}
104
165
166
+ /**
167
+ *
168
+ * @param {string|string[] } str string or string collection
169
+ * @return {string } returns a single string from array
170
+ */
105
171
static asString ( str ) {
106
172
if ( Array . isArray ( str ) ) {
107
173
return str . join ( "\n" ) ;
108
174
}
109
175
return str ;
110
176
}
111
177
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
+ */
112
184
static getModulesArrayBounds ( modules ) {
113
185
if ( ! modules . every ( moduleIdIsNumber ) ) return false ;
114
186
var maxId = - Infinity ;
@@ -133,6 +205,15 @@ module.exports = class Template {
133
205
return arrayOverhead < objectOverhead ? [ minId , maxId ] : false ;
134
206
}
135
207
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
+ */
136
217
static renderChunkModules (
137
218
chunk ,
138
219
filterFn ,
@@ -143,7 +224,9 @@ module.exports = class Template {
143
224
if ( ! prefix ) prefix = "" ;
144
225
var source = new ConcatSource ( ) ;
145
226
const modules = chunk . getModules ( ) . filter ( filterFn ) ;
146
- var removedModules = chunk . removedModules ;
227
+ if ( chunk instanceof HotUpdateChunk ) {
228
+ var removedModules = chunk . removedModules ;
229
+ }
147
230
if (
148
231
modules . length === 0 &&
149
232
( ! removedModules || removedModules . length === 0 )
@@ -202,4 +285,6 @@ module.exports = class Template {
202
285
}
203
286
return source ;
204
287
}
205
- } ;
288
+ }
289
+
290
+ module . exports = Template ;
0 commit comments