forked from chrisvfritz/prerender-spa-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (51 loc) · 1.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var FS = require('fs')
var Path = require('path')
var mkdirp = require('mkdirp')
var compileToHTML = require('./lib/compile-to-html')
function SimpleHtmlPrecompiler (staticDir, paths, options) {
this.staticDir = staticDir
this.paths = paths
this.options = options || {}
}
SimpleHtmlPrecompiler.prototype.apply = function (compiler) {
var self = this
compiler.plugin('after-emit', function (compilation, done) {
Promise.all(
self.paths.map(function (outputPath) {
return new Promise(function (resolve, reject) {
compileToHTML(self.staticDir, outputPath, self.options, function (prerenderedHTML) {
if (self.options.postProcessHtml) {
prerenderedHTML = self.options.postProcessHtml({
html: prerenderedHTML,
route: outputPath
})
}
var folder = Path.join(self.options.outputDir || self.staticDir, outputPath)
mkdirp(folder, function (error) {
if (error) {
return reject('Folder could not be created: ' + folder + '\n' + error)
}
var file = Path.join(folder, 'index.html')
FS.writeFile(
file,
prerenderedHTML,
function (error) {
if (error) {
return reject('Could not write file: ' + file + '\n' + error)
}
resolve()
}
)
})
})
})
})
)
.then(function () { done() })
.catch(function (error) {
// setTimeout prevents the Promise from swallowing the throw
setTimeout(function () { throw error })
})
})
}
module.exports = SimpleHtmlPrecompiler