Skip to content

Commit 0379421

Browse files
committed
perf(): render static html via piping streams
1 parent eece2e3 commit 0379421

File tree

1 file changed

+21
-7
lines changed
  • packages/@vuepress/core/lib/node/build

1 file changed

+21
-7
lines changed

packages/@vuepress/core/lib/node/build/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,35 @@ module.exports = class Build extends EventEmitter {
145145
version
146146
}
147147

148-
let html
149148
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)
151154
} catch (e) {
152155
console.error(logger.error(chalk.red(`Error rendering ${pagePath}:`), false))
153156
throw e
154157
}
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
160158
}
161159
}
162160

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+
163177
/**
164178
* Compile a webpack application and return stats json.
165179
*

0 commit comments

Comments
 (0)