14
14
const NodeTemplatePlugin = require ( 'webpack/lib/node/NodeTemplatePlugin' ) ;
15
15
const NodeTargetPlugin = require ( 'webpack/lib/node/NodeTargetPlugin' ) ;
16
16
const LoaderTargetPlugin = require ( 'webpack/lib/LoaderTargetPlugin' ) ;
17
- const LibraryTemplatePlugin = require ( 'webpack/lib/LibraryTemplatePlugin' ) ;
18
17
const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
19
- const Compilation = require ( 'webpack' ) . Compilation ;
20
18
21
19
let instanceId = 0 ;
22
20
/**
@@ -74,35 +72,43 @@ class HtmlWebpackChildCompiler {
74
72
* This function will start the template compilation
75
73
* once it is started no more templates can be added
76
74
*
77
- * @param {WebpackCompilation } mainCompilation
75
+ * @param {import('Webpack').Compilation } mainCompilation
78
76
* @returns {Promise<{[templatePath: string]: { content: string, hash: string, entry: WebpackChunk }}> }
79
77
*/
80
78
compileTemplates ( mainCompilation ) {
79
+ const webpack = mainCompilation . compiler . webpack ;
80
+ const Compilation = webpack . Compilation ;
81
+
81
82
// To prevent multiple compilations for the same template
82
83
// the compilation is cached in a promise.
83
84
// If it already exists return
84
85
if ( this . compilationPromise ) {
85
86
return this . compilationPromise ;
86
87
}
87
88
88
- // The entry file is just an empty helper as the dynamic template
89
- // require is added in "loader.js"
90
89
const outputOptions = {
91
90
filename : '__child-[name]' ,
92
- publicPath : mainCompilation . outputOptions . publicPath
91
+ publicPath : mainCompilation . outputOptions . publicPath ,
92
+ library : {
93
+ type : 'var' ,
94
+ name : 'HTML_WEBPACK_PLUGIN_RESULT'
95
+ } ,
96
+ /** @type {'text/javascript' } */
97
+ scriptType : ( /** @type {'text/javascript' } */ 'text/javascript' ) ,
98
+ iife : false
93
99
} ;
94
100
const compilerName = 'HtmlWebpackCompiler' ;
95
101
// Create an additional child compiler which takes the template
96
102
// and turns it into an Node.JS html factory.
97
103
// This allows us to use loaders during the compilation
98
- const childCompiler = mainCompilation . createChildCompiler ( compilerName , outputOptions ) ;
104
+ const childCompiler = mainCompilation . createChildCompiler ( compilerName , outputOptions , [
105
+ // Compile the template to nodejs javascript
106
+ new NodeTemplatePlugin ( outputOptions ) ,
107
+ new NodeTargetPlugin ( ) ,
108
+ new LoaderTargetPlugin ( 'node' )
109
+ ] ) ;
99
110
// The file path context which webpack uses to resolve all relative files to
100
111
childCompiler . context = mainCompilation . compiler . context ;
101
- // Compile the template to nodejs javascript
102
- new NodeTemplatePlugin ( outputOptions ) . apply ( childCompiler ) ;
103
- new NodeTargetPlugin ( ) . apply ( childCompiler ) ;
104
- new LibraryTemplatePlugin ( 'HTML_WEBPACK_PLUGIN_RESULT' , 'var' ) . apply ( childCompiler ) ;
105
- new LoaderTargetPlugin ( 'node' ) . apply ( childCompiler ) ;
106
112
107
113
// Generate output file names
108
114
const temporaryTemplateNames = this . templates . map ( ( template , index ) => `__child-HtmlWebpackPlugin_${ index } -${ this . id } ` ) ;
@@ -138,14 +144,14 @@ class HtmlWebpackChildCompiler {
138
144
? extractedAssets . map ( ( asset ) => asset . source ( ) )
139
145
: [ ] ;
140
146
// Extract file dependencies
141
- if ( entries ) {
147
+ if ( entries && childCompilation ) {
142
148
this . fileDependencies = { fileDependencies : Array . from ( childCompilation . fileDependencies ) , contextDependencies : Array . from ( childCompilation . contextDependencies ) , missingDependencies : Array . from ( childCompilation . missingDependencies ) } ;
143
149
}
144
150
// Reject the promise if the childCompilation contains error
145
151
if ( childCompilation && childCompilation . errors && childCompilation . errors . length ) {
146
152
const errorDetails = childCompilation . errors . map ( error => {
147
153
let message = error . message ;
148
- if ( error . error ) {
154
+ if ( ' error' in error ) {
149
155
message += ':\n' + error . error ;
150
156
}
151
157
if ( error . stack ) {
@@ -161,6 +167,10 @@ class HtmlWebpackChildCompiler {
161
167
reject ( err ) ;
162
168
return ;
163
169
}
170
+ if ( ! childCompilation || ! entries ) {
171
+ reject ( new Error ( 'Empty child compilation' ) ) ;
172
+ return ;
173
+ }
164
174
/**
165
175
* @type {{[templatePath: string]: { content: string, hash: string, entry: WebpackChunk }} }
166
176
*/
0 commit comments