@@ -16,6 +16,7 @@ const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
16
16
const LoaderTargetPlugin = require ( 'webpack/lib/LoaderTargetPlugin' ) ;
17
17
const LibraryTemplatePlugin = require ( 'webpack/lib/LibraryTemplatePlugin' ) ;
18
18
const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
19
+ const Compilation = require ( 'webpack' ) . Compilation ;
19
20
20
21
/**
21
22
* The HtmlWebpackChildCompiler is a helper to allow reusing one childCompiler
@@ -100,17 +101,38 @@ class HtmlWebpackChildCompiler {
100
101
new LibraryTemplatePlugin ( 'HTML_WEBPACK_PLUGIN_RESULT' , 'var' ) . apply ( childCompiler ) ;
101
102
new LoaderTargetPlugin ( 'node' ) . apply ( childCompiler ) ;
102
103
104
+ // Generate output file names
105
+ const temporaryTemplateNames = this . templates . map ( ( template , index ) => `__child-HtmlWebpackPlugin_${ index } -${ template } ` ) ;
106
+
103
107
// Add all templates
104
108
this . templates . forEach ( ( template , index ) => {
105
- new SingleEntryPlugin ( childCompiler . context , template , `HtmlWebpackPlugin_${ index } ` ) . apply ( childCompiler ) ;
109
+ new SingleEntryPlugin ( childCompiler . context , template , `HtmlWebpackPlugin_${ index } - ${ template } ` ) . apply ( childCompiler ) ;
106
110
} ) ;
107
111
108
112
this . compilationStartedTimestamp = new Date ( ) . getTime ( ) ;
109
113
this . compilationPromise = new Promise ( ( resolve , reject ) => {
114
+ const extractedAssets = [ ] ;
115
+ childCompiler . hooks . compilation . tap ( 'HtmlWebpackPlugin' , ( compilation ) => {
116
+ compilation . hooks . processAssets . tap (
117
+ {
118
+ name : 'HtmlWebpackPlugin' ,
119
+ stage : Compilation . PROCESS_ASSETS_STAGE_ADDITIONS
120
+ } ,
121
+ ( assets ) => {
122
+ temporaryTemplateNames . forEach ( ( temporaryTemplateName ) => {
123
+ if ( assets [ temporaryTemplateName ] ) {
124
+ extractedAssets . push ( assets [ temporaryTemplateName ] ) ;
125
+ compilation . deleteAsset ( temporaryTemplateName ) ;
126
+ }
127
+ } ) ;
128
+ }
129
+ ) ;
130
+ } ) ;
131
+
110
132
childCompiler . runAsChild ( ( err , entries , childCompilation ) => {
111
133
// Extract templates
112
134
const compiledTemplates = entries
113
- ? extractHelperFilesFromCompilation ( mainCompilation , childCompilation , outputOptions . filename , entries )
135
+ ? extractedAssets . map ( ( asset ) => asset . source ( ) )
114
136
: [ ] ;
115
137
// Extract file dependencies
116
138
if ( entries ) {
@@ -159,36 +181,6 @@ class HtmlWebpackChildCompiler {
159
181
}
160
182
}
161
183
162
- /**
163
- * The webpack child compilation will create files as a side effect.
164
- * This function will extract them and clean them up so they won't be written to disk.
165
- *
166
- * Returns the source code of the compiled templates as string
167
- *
168
- * @returns Array<string>
169
- */
170
- function extractHelperFilesFromCompilation ( mainCompilation , childCompilation , filename , childEntryChunks ) {
171
- const helperAssetNames = childEntryChunks . map ( ( entryChunk , index ) => {
172
- const entryConfig = {
173
- hash : childCompilation . hash ,
174
- chunk : entryChunk ,
175
- name : `HtmlWebpackPlugin_${ index } `
176
- } ;
177
-
178
- return mainCompilation . getAssetPath ( filename , entryConfig ) ;
179
- } ) ;
180
-
181
- helperAssetNames . forEach ( ( helperFileName ) => {
182
- delete mainCompilation . assets [ helperFileName ] ;
183
- } ) ;
184
-
185
- const helperContents = helperAssetNames . map ( ( helperFileName ) => {
186
- return childCompilation . assets [ helperFileName ] . source ( ) ;
187
- } ) ;
188
-
189
- return helperContents ;
190
- }
191
-
192
184
module . exports = {
193
185
HtmlWebpackChildCompiler
194
186
} ;
0 commit comments