forked from manudurgoni/prerender-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (55 loc) · 1.59 KB
/
index.js
File metadata and controls
63 lines (55 loc) · 1.59 KB
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
55
56
57
58
59
60
61
62
63
const mkdirp = require('mkdirp')
const fse = require('fs-extra')
const serverBuilder = require('./util/server')
const browserBuilder = require('./util/browser')
const pageProcessor = require('./util/page')
const optionsProcessor = require('./util/options')
function HtmlPrerenderer(options) {
this.options = optionsProcessor.process(options)
}
async function process(compilation, done) {
const { browserOptions } = this.options || {}
this.options.server = await serverBuilder.build(this.options.source)
this.options.browser = await browserBuilder.build(browserOptions)
this.options.url = `http://localhost:${this.options.server.address().port}`
// copy source folder
if (this.options.source !== this.options.target) {
try {
mkdirp.sync(this.options.target)
await fse.copy(this.options.source, this.options.target)
} catch (err) {
throw err
}
}
Promise.all(
this.options.routes.map(
(route) =>
new Promise(async (resolve, reject) => {
pageProcessor.process(route, this.options, (err) => {
if (err) reject(err)
resolve()
})
}),
),
)
.catch((err) => {
setTimeout(() => {
console.log(err)
throw err
}, 0)
})
.then(() => {
this.options.browser.close()
this.options.server.close()
if (done) done()
})
}
// eslint-disable-next-line
HtmlPrerenderer.prototype.apply = async function (compiler) {
if (compiler) {
compiler.plugin('after-emit', process.bind(this))
} else {
process.bind(this)()
}
}
module.exports = HtmlPrerenderer