-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (51 loc) · 1.89 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
55
const path = require('path')
const fs = require('fs')
const Prerenderer = require('./prerenderer')
const PuppeteerRenderer = require('./puppeteer')
class PrerenderSPAPlugin {
constructor(options) {
this._options = {
renderer: new PuppeteerRenderer(options.renderOption ? { headless: true, skipRequest: true, ...options.renderOption } : { headless: true, skipRequest: true }),
...options,
server: {},
}
}
apply(compiler) {
const compilerFS = compiler.outputFileSystem
compiler.hooks.afterEmit.tapAsync('PrerenderSPAPlugin', (compilation, done) => {
const PrerendererInstance = new Prerenderer(this._options)
PrerendererInstance.initialize()
.then(() => {
return PrerendererInstance.renderRoutes((this._options.routes || ['/']))
}).then(renderedRoutes => {
renderedRoutes.forEach(renderedRoute => {
renderedRoute.outputPath = path.join(path.resolve((this._options.outDir || 'dist')), renderedRoute.route)
});
return renderedRoutes
}).then((renderedRoutes) => {
return Promise.all(renderedRoutes.map(renderedRoute => new Promise((resolve, reject)=>{
compilerFS.writeFile(renderedRoute.route === '/' ? `${renderedRoute.outputPath}${(this._options.html || 'index.html')}` : `${renderedRoute.outputPath}.html` , renderedRoute.html.trim(), err => {
if (err) reject(err)
else resolve()
})
})
)).catch(err => {
const msg = err.message || 'PrerenderSPAPlugin 发生错误'
PrerendererInstance.destroy()
compilation.errors.push(new Error(msg))
done()
})
}).then(() => {
PrerendererInstance.destroy()
done()
}).catch(err => {
const msg = err.message || 'PrerenderSPAPlugin 发生错误'
PrerendererInstance.destroy()
compilation.errors.push(new Error(msg))
done()
})
});
}
}
PrerenderSPAPlugin.PuppeteerRenderer = PuppeteerRenderer
module.exports = PrerenderSPAPlugin