/
index.js
executable file
·60 lines (48 loc) · 1.55 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
56
57
58
59
60
const { resolve } = require('path')
const { Nuxt, Builder } = require('nuxt')
const NuxtPlugin = {
name: 'nuxt',
pkg: require('../package.json'),
async register (server, config) {
// If config is not provided try nuxt.config.js
if (!config || Object.keys(config).length === 0) {
config = 'nuxt.config.js'
}
// Resolve config location if is provided as string
if(typeof config === 'string') {
try {
const path = resolve(process.cwd(), config)
config = require(path)
} catch (e) {
// DO NOTHING
}
}
// Create nuxt instance using options
const nuxt = new Nuxt(config)
server.expose('nuxt', nuxt)
// Nuxt handler
server.route({
method: 'GET',
path: '/{path*}',
config: {
id: 'NuxtController.render',
auth: false,
},
handler (request, h) {
const {req, res} = request.raw
nuxt.render(req, res)
// https://hapijs.com/api#h.abandon
return h.abandon
}
})
// Dev
if (nuxt.options.dev && nuxt.options.startOnly !== false) {
// Build nuxt
console.log('Building nuxt ...')
const builder = new Builder(nuxt);
server.expose('builder', builder);
builder.build().catch(console.error)
}
}
}
module.exports = NuxtPlugin