-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.js
308 lines (290 loc) · 9.84 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const { resolve, parse } = require('path')
const { readFile, writeFile } = require('fs').promises
const { existsSync } = require('fs')
const { ensureDir } = require('fs-extra')
const { createServer } = require('vite')
const matchit = require('matchit')
const Fastify = require('fastify')
const middie = require('middie')
const { fetch } = require('undici')
const fastifyPlugin = require('fastify-plugin')
const fastifyStatic = require('fastify-static')
const { build } = require('./build')
const { generateRoute } = require('./static')
const { processOptions } = require('./options')
async function fastifyVite (fastify, options) {
let viteReady
const isViteReady = new Promise((resolve) => {
viteReady = resolve
})
fastify.decorate('vite', {
ready () {
return isViteReady
},
})
// Run options through Vite to get all Vite defaults taking vite.config.js
// into account and ensuring options.root and options.vite.root are the same
try {
options = await processOptions(options)
} catch (err) {
console.error(err)
setImmediate(() => process.exit(1))
return
}
if (options.suppressExperimentalWarnings) {
// See https://github.com/nodejs/node/issues/30810
const { emitWarning } = process
process.emitWarning = (warning, ...args) => {
if (args[0] === 'ExperimentalWarning') {
return
}
if (args[0] && typeof args[0] === 'object' && args[0].type === 'ExperimentalWarning') {
return
}
return emitWarning(warning, ...args)
}
}
if (options.generate.enabled || options.generate.server.enabled) {
console.log('await build(options)')
await build(options)
options.dev = false
console.log('options.recalcDist()')
options.recalcDist()
}
// Provided by the chosen rendering adapter
const renderer = options.renderer
// We'll want access to this later
let handler
let vite
let routes = []
if (options.build) {
await build(options)
setImmediate(() => process.exit(0))
} else if (!options.eject) {
// Setup appropriate Vite route handler
if (options.dev) {
// For dev you get more detailed logging and hot reload
vite = await createServer({
...options.vite,
server: {
middlewareMode: 'ssr',
...options.vite.server,
},
})
await fastify.register(middie)
fastify.use(vite.middlewares)
const indexHtmlPath = resolve(options.root, 'index.html')
if (!existsSync(indexHtmlPath)) {
const baseIndexHtmlPath = resolve(renderer.path, 'base', 'index.html')
await writeFile(indexHtmlPath, await readFile(baseIndexHtmlPath, 'utf8'))
}
const getTemplate = async (url) => {
const indexHtml = await readFile(indexHtmlPath, 'utf8')
const transformedHtml = await vite.transformIndexHtml(url, indexHtml)
return await renderer.compileIndexHtml(transformedHtml)
}
const entry = await renderer.dev.getEntry(options, vite)
handler = renderer.dev.getHandler(fastify, options, entry.getRender, getTemplate, vite)
if (entry.routes) {
routes = await entry.routes()
}
} else if (!options.eject) {
// For production you get the distribution version of the render function
const { assetsDir } = options.vite.build
// We also register fastify-static to serve all static files
// in production (dev server takes of this)
// Note: this is just to ensure it works, for a real world
// production deployment, you'll want to capture those paths in
// Nginx or just serve them from a CDN instead
await fastify.register(fastifyStatic, {
root: resolve(options.distDir, `client/${assetsDir}`),
prefix: `/${assetsDir}`,
})
const template = await renderer.compileIndexHtml(options.distIndex)
const entry = await renderer.getEntry(options)
if (entry.routes) {
routes = await entry.routes()
}
handler = renderer.getHandler(fastify, options, entry.render, template)
}
}
viteReady()
// Sets fastify.vite.get() helper which uses
// a wrapper for setting a route with a data() handler
Object.assign(fastify.vite, {
handler,
options,
global: undefined,
// Not available when NODE_ENV=production
devServer: vite,
get (url, { data, ...routeOptions } = {}) {
return this.route(url, { data, method: 'GET', ...routeOptions })
},
post (url, { data, method, ...routeOptions } = {}) {
return this.route(url, { data, method: 'GET', ...routeOptions })
},
route (url, { getData, getPayload, method, ...routeOptions } = {}) {
const preHandler = routeOptions.preHandler || []
if (getData) {
preHandler.push(
async function (req, reply) {
req[options.hydration.data] = await getData.call(
this,
{
req,
params: req.params,
reply,
$api: this.api && this.api.client,
fastify: this,
fetch,
},
)
},
)
}
if (getPayload) {
preHandler.push(
async function (req, reply) {
req[options.hydration.payload] = await getPayload.call(
this,
{
req,
params: req.params,
reply,
$api: this.api && this.api.client,
fastify: this,
fetch,
},
)
},
)
fastify.get(`/-/payload${url}`, async function (req, reply) {
return getPayload.call(this, {
req,
params: req.params,
reply,
$api: this.api && this.api.client,
fastify: this,
fetch,
})
})
}
fastify.route({
method,
url,
handler,
...routeOptions,
preHandler,
})
},
})
for (const route of routes) {
fastify.vite.route(route.path, {
method: route.method || 'GET',
getData: route.getData,
getPayload: route.getPayload,
onRequest: route.onRequest,
preParsing: route.preParsing,
preValidation: route.preValidation,
preHandler: route.preHandler,
preSerialization: route.preSerialization,
onError: route.onError,
onSend: route.onSend,
onResponse: route.onResponse,
onTimeout: route.onTimeout,
})
}
fastify.vite.commands = async () => {
await fastify.ready()
if (fastify.vite.options.eject) {
const force = process.argv.includes('--force')
for (const blueprintFile of renderer.blueprint) {
if (force || !existsSync(resolve(options.root, blueprintFile))) {
const filePath = resolve(options.root, blueprintFile)
const fileDir = parse(filePath).dir
await ensureDir(fileDir)
await writeFile(
filePath,
await readFile(resolve(renderer.path, 'base', blueprintFile)),
)
console.log(`ℹ ejected ${filePath}.`)
}
}
process.exit()
}
if (fastify.vite.options.generate.enabled || fastify.vite.options.generate.server.enabled) {
const { generated } = fastify.vite.options.generate
const paths = []
if (typeof fastify.vite.options.generate.paths === 'function') {
await fastify.vite.options.generate.paths(fastify, (path) => paths.push(path))
} else if (Array.isArray(fastify.vite.options.generate.paths)) {
paths.push(...fastify.vite.options.generate.paths)
} else {
paths.push(
...routes
.filter(({ path }) => matchit.parse(path).every(segment => segment.type === 0))
.map(({ path }) => path),
)
}
const tasks = []
for (const path of paths) {
tasks.push(async () => {
const result = await generateRoute(fastify.inject({ url: path }), path, options)
if (result) {
generated(fastify, result, fastify.vite.options.distDir)
}
})
}
await Promise.all(tasks.map(task => task()))
if (fastify.vite.options.generate.done) {
await fastify.vite.options.generate.done(fastify)
}
if (!fastify.vite.options.generate.server.enabled) {
setImmediate(() => {
process.exit(0)
})
}
}
if (fastify.vite.options.generate.server.enabled) {
const { generated } = fastify.vite.options.generate
const { port } = fastify.vite.options.generate.server
const builder = Fastify()
builder.get('*', async (req, reply) => {
const path = req.raw.url
const result = await generateRoute(fastify.inject({ url: path }), path, options)
if (result) {
reply.send(`ℹ regenerated ${req.raw.url}`)
generated(fastify, result, fastify.vite.options.distDir)
}
})
// @Matteo
//
// FIXME Unresolved Promise used here just to prevent
// execution from advancing into the .listen() call
//
// Fastify Core Idea: render .listen() ineffective
// in case any of the registered onReady hooks returns false
await new Promise(() => {
builder.listen(port, (err, address) => {
if (err) {
console.error(err)
setImmediate(() => {
process.exit(1)
})
}
console.log(`ℹ generate server listening on ${address}`)
})
})
}
}
fastify.addHook('onReady', async () => {
// Pre-initialize request decorator for better performance
// This actually safely adds things to Request.prototype
fastify.decorateRequest(options.hydration.global, { getter: () => fastify.vite.global })
fastify.decorateRequest(options.hydration.data, null)
if (options.api) {
fastify.decorateRequest('api', fastify.api)
}
})
}
module.exports = fastifyPlugin(fastifyVite)