11
11
12
12
/** @typedef {import("webpack").Chunk } Chunk */
13
13
/** @typedef {import("webpack").sources.Source } Source */
14
+ /** @typedef {{hash: string, entry: Chunk, content: string, assets: {[name: string]: { source: Source, info: import("webpack").AssetInfo }}} } ChildCompilationTemplateResult */
14
15
15
16
let instanceId = 0 ;
16
17
/**
@@ -30,17 +31,11 @@ class HtmlWebpackChildCompiler {
30
31
* The template array will allow us to keep track which input generated which output
31
32
*/
32
33
this . templates = templates ;
33
- /**
34
- * @type {Promise<{[templatePath: string]: { content: string, hash: string, entry: Chunk }}> }
35
- */
34
+ /** @type {Promise<{[templatePath: string]: ChildCompilationTemplateResult}> } */
36
35
this . compilationPromise ; // eslint-disable-line
37
- /**
38
- * @type {number }
39
- */
36
+ /** @type {number | undefined } */
40
37
this . compilationStartedTimestamp ; // eslint-disable-line
41
- /**
42
- * @type {number }
43
- */
38
+ /** @type {number | undefined } */
44
39
this . compilationEndedTimestamp ; // eslint-disable-line
45
40
/**
46
41
* All file dependencies of the child compiler
@@ -51,6 +46,7 @@ class HtmlWebpackChildCompiler {
51
46
52
47
/**
53
48
* Returns true if the childCompiler is currently compiling
49
+ *
54
50
* @returns {boolean }
55
51
*/
56
52
isCompiling ( ) {
@@ -59,6 +55,8 @@ class HtmlWebpackChildCompiler {
59
55
60
56
/**
61
57
* Returns true if the childCompiler is done compiling
58
+ *
59
+ * @returns {boolean }
62
60
*/
63
61
didCompile ( ) {
64
62
return this . compilationEndedTimestamp !== undefined ;
@@ -69,7 +67,7 @@ class HtmlWebpackChildCompiler {
69
67
* once it is started no more templates can be added
70
68
*
71
69
* @param {import('webpack').Compilation } mainCompilation
72
- * @returns {Promise<{[templatePath: string]: { content: string, hash: string, entry: Chunk } }> }
70
+ * @returns {Promise<{[templatePath: string]: ChildCompilationTemplateResult }> }
73
71
*/
74
72
compileTemplates ( mainCompilation ) {
75
73
const webpack = mainCompilation . compiler . webpack ;
@@ -125,13 +123,17 @@ class HtmlWebpackChildCompiler {
125
123
// The following config enables relative URL support for the child compiler
126
124
childCompiler . options . module = { ...childCompiler . options . module } ;
127
125
childCompiler . options . module . parser = { ...childCompiler . options . module . parser } ;
128
- childCompiler . options . module . parser . javascript = { ...childCompiler . options . module . parser . javascript ,
129
- url : 'relative' } ;
126
+ childCompiler . options . module . parser . javascript = {
127
+ ...childCompiler . options . module . parser . javascript ,
128
+ url : 'relative'
129
+ } ;
130
130
131
131
this . compilationStartedTimestamp = new Date ( ) . getTime ( ) ;
132
+ /** @type {Promise<{[templatePath: string]: ChildCompilationTemplateResult}> } */
132
133
this . compilationPromise = new Promise ( ( resolve , reject ) => {
133
134
/** @type {Source[] } */
134
135
const extractedAssets = [ ] ;
136
+
135
137
childCompiler . hooks . thisCompilation . tap ( 'HtmlWebpackPlugin' , ( compilation ) => {
136
138
compilation . hooks . processAssets . tap (
137
139
{
@@ -142,6 +144,7 @@ class HtmlWebpackChildCompiler {
142
144
temporaryTemplateNames . forEach ( ( temporaryTemplateName ) => {
143
145
if ( assets [ temporaryTemplateName ] ) {
144
146
extractedAssets . push ( assets [ temporaryTemplateName ] ) ;
147
+
145
148
compilation . deleteAsset ( temporaryTemplateName ) ;
146
149
}
147
150
} ) ;
@@ -151,13 +154,16 @@ class HtmlWebpackChildCompiler {
151
154
152
155
childCompiler . runAsChild ( ( err , entries , childCompilation ) => {
153
156
// Extract templates
157
+ // TODO fine a better way to store entries and results, to avoid duplicate chunks and assets
154
158
const compiledTemplates = entries
155
159
? extractedAssets . map ( ( asset ) => asset . source ( ) )
156
160
: [ ] ;
161
+
157
162
// Extract file dependencies
158
163
if ( entries && childCompilation ) {
159
164
this . fileDependencies = { fileDependencies : Array . from ( childCompilation . fileDependencies ) , contextDependencies : Array . from ( childCompilation . contextDependencies ) , missingDependencies : Array . from ( childCompilation . missingDependencies ) } ;
160
165
}
166
+
161
167
// Reject the promise if the childCompilation contains error
162
168
if ( childCompilation && childCompilation . errors && childCompilation . errors . length ) {
163
169
const errorDetails = childCompilation . errors . map ( error => {
@@ -167,34 +173,50 @@ class HtmlWebpackChildCompiler {
167
173
}
168
174
return message ;
169
175
} ) . join ( '\n' ) ;
176
+
170
177
reject ( new Error ( 'Child compilation failed:\n' + errorDetails ) ) ;
178
+
171
179
return ;
172
180
}
181
+
173
182
// Reject if the error object contains errors
174
183
if ( err ) {
175
184
reject ( err ) ;
176
185
return ;
177
186
}
187
+
178
188
if ( ! childCompilation || ! entries ) {
179
189
reject ( new Error ( 'Empty child compilation' ) ) ;
180
190
return ;
181
191
}
192
+
182
193
/**
183
- * @type {{[templatePath: string]: { content: string, hash: string, entry: Chunk } } }
194
+ * @type {{[templatePath: string]: ChildCompilationTemplateResult } }
184
195
*/
185
196
const result = { } ;
197
+
198
+ /** @type {{[name: string]: { source: Source, info: import("webpack").AssetInfo }} } */
199
+ const assets = { } ;
200
+
201
+ for ( const asset of childCompilation . getAssets ( ) ) {
202
+ assets [ asset . name ] = { source : asset . source , info : asset . info } ;
203
+ }
204
+
186
205
compiledTemplates . forEach ( ( templateSource , entryIndex ) => {
187
206
// The compiledTemplates are generated from the entries added in
188
207
// the addTemplate function.
189
- // Therefore the array index of this.templates should be the as entryIndex.
208
+ // Therefore, the array index of this.templates should be the as entryIndex.
190
209
result [ this . templates [ entryIndex ] ] = {
191
210
// TODO, can we have Buffer here?
192
211
content : /** @type {string } */ ( templateSource ) ,
193
212
hash : childCompilation . hash || 'XXXX' ,
194
- entry : entries [ entryIndex ]
213
+ entry : entries [ entryIndex ] ,
214
+ assets
195
215
} ;
196
216
} ) ;
217
+
197
218
this . compilationEndedTimestamp = new Date ( ) . getTime ( ) ;
219
+
198
220
resolve ( result ) ;
199
221
} ) ;
200
222
} ) ;
0 commit comments