@@ -145,21 +145,35 @@ module.exports = class Build extends EventEmitter {
145
145
version
146
146
}
147
147
148
- let html
149
148
try {
150
- html = await this . renderer . renderToString ( context )
149
+ const readable = await this . renderer . renderToStream ( context )
150
+ const filename = pagePath . replace ( / \/ $ / , '/index.html' ) . replace ( / ^ \/ / , '' )
151
+ const filePath = path . resolve ( this . outDir , filename )
152
+ await fs . ensureDir ( path . dirname ( filePath ) )
153
+ return pipe ( filePath , readable )
151
154
} catch ( e ) {
152
155
console . error ( logger . error ( chalk . red ( `Error rendering ${ pagePath } :` ) , false ) )
153
156
throw e
154
157
}
155
- const filename = pagePath . replace ( / \/ $ / , '/index.html' ) . replace ( / ^ \/ / , '' )
156
- const filePath = path . resolve ( this . outDir , filename )
157
- await fs . ensureDir ( path . dirname ( filePath ) )
158
- await fs . writeFile ( filePath , html )
159
- return filePath
160
158
}
161
159
}
162
160
161
+ /**
162
+ * Pipes rendered static HTML to a file
163
+ *
164
+ * @param {string } filePath
165
+ * @param {Stream.Readable } readable
166
+ * @returns {Promise<void> }
167
+ */
168
+ function pipe ( filePath , readable ) {
169
+ return new Promise ( ( resolve , reject ) => {
170
+ const outStream = fs . createWriteStream ( filePath ) ;
171
+ readable . pipe ( outStream ) ;
172
+ outStream . on ( 'finish' , resolve ( filePath ) ) ;
173
+ readable . on ( 'error' , reject ) ;
174
+ } ) ;
175
+ }
176
+
163
177
/**
164
178
* Compile a webpack application and return stats json.
165
179
*
0 commit comments