-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-ssg.ts
350 lines (306 loc) · 9.92 KB
/
build-ssg.ts
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Pre-render the app into static HTML.
// run `pnpm ssg:build` and then `dist` can be served as a static site.
import fs from 'node:fs'
import { createRequire } from 'node:module'
import fsp from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import fg from 'fast-glob'
import { mergeConfig, resolveConfig, build as viteBuild } from 'vite'
import colors from 'picocolors'
import { format, generateStaticParams } from './build-ssg.config'
// prevent non-ready SSR dependencies from throwing errors
//@ts-expect-error
globalThis.__VUE_PROD_DEVTOOLS__ = false
//@ts-expect-error
globalThis.__VUE_I18N_FULL_INSTALL__ = false
//@ts-expect-error
globalThis.__VUE_I18N_LEGACY_API__ = false
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const resolve = (p: string) => path.resolve(__dirname, p)
const ROUTE_PARAM_REGEX = /(\[.*?\])/g
// const routesToPrerender = fs.readdirSync(resolve('src/pages')).map((file) => {
// const name = file.replace(/\.vue$/, '').toLowerCase()
// return name === 'index' ? `/` : `/${name}`
// })
async function build() {
const staticParams = generateStaticParams()
const viteConfig = {}
const mode = process.env.MODE || process.env.NODE_ENV || 'production'
const config = await resolveConfig(viteConfig, 'build', mode)
const cwd = process.cwd()
const root = config.root || cwd
const outDir = config.build.outDir || 'dist'
const out = path.isAbsolute(outDir) ? outDir : path.join(root, outDir)
if (fs.existsSync(out)) {
await fsp.rm(out, { recursive: true })
}
const outStatic = out
const outServer = path.join(out, '.server')
config.logger.info(colors.green('[SSG] Build for client...'))
await viteBuild(
mergeConfig(viteConfig, {
build: {
ssrManifest: true,
outDir: outStatic,
rollupOptions: {
input: {
app: path.join(root, './index.html'),
},
},
},
mode: config.mode,
})
)
// server
config.logger.info(colors.green('[SSG] Build for server...'))
await viteBuild(
mergeConfig(viteConfig, {
build: {
ssr: 'src/entry-server.ts',
outDir: outServer,
minify: false,
cssCodeSplit: false,
rollupOptions: {
output:
format === 'esm'
? {
entryFileNames: '[name].mjs',
format: 'esm',
}
: {
entryFileNames: '[name].cjs',
format: 'cjs',
},
},
},
mode: config.mode,
})
)
const manifest = JSON.parse(
await fsp.readFile(path.join(outStatic, './ssr-manifest.json'), 'utf-8')
)
const template = await fsp.readFile(path.join(outStatic, './index.html'), 'utf-8')
const prefix = format === 'esm' && process.platform === 'win32' ? 'file://' : ''
const ext = format === 'esm' ? '.mjs' : '.cjs'
const serverEntry = path.join(prefix, outServer, 'entry-server' + ext)
const _require = createRequire(import.meta.url)
const { init, render }: any =
format === 'esm' ? await import(serverEntry) : _require(serverEntry)
// determine routes to pre-render from src/pages
const routesToPrerender = (
await fg([path.resolve(cwd, 'src/pages/**/*.vue').replace(/\\/g, '/')])
)
.filter((path) => !path.includes('src/pages/[...all].vue')) // ignore root catch-all route
.map((file) => {
const name = file
.replace(/\.vue$/, '')
.replace(cwd.replace(/\\/g, '/'), '')
.replace(/\/+/g, '/')
.replace('/src/pages/', '')
.toLowerCase()
return '/' + name.replace(/index$/, '')
})
// pre-render each route...
for (const index in routesToPrerender) {
const url =
routesToPrerender[index] === '/' ? '/' : routesToPrerender[index].replace(/\/$/, '') // remove trailing slash
const logCount = `${1 + parseInt(index, 10)}/${routesToPrerender.length}`
if (url.includes('[')) {
const routeStaticParamsFn =
url in staticParams ? staticParams[url as keyof typeof staticParams] : undefined
if (!routeStaticParamsFn) {
config.logger.warn(
`dynamic route (${logCount}) ${colors.yellow(
url
)} - missing static config - update ${colors.cyan(
'./build-ssg.config.ts'
)} to generate static params for this route.`
)
continue
}
// extract route params from url (e.g. /[id] or /[[slug]] or /[...all])
const params = (url.match(ROUTE_PARAM_REGEX) || []).map((p) => {
const required = !p.includes('[[')
const array = p.includes('...')
const name = p.replaceAll(/\[/g, '').replaceAll(/\]/g, '').replaceAll(/\./g, '')
return {
required,
array,
name,
param: p,
}
})
const routeStaticParams = await staticParams[url as keyof typeof staticParams]()
if (!routeStaticParams || !Array.isArray(routeStaticParams)) {
config.logger.warn(
`dynamic route (${logCount}) ${colors.yellow(
url
)} - static params must be an array`
)
continue
}
// check if static params are valid
const invalidParams = routeStaticParams.filter((param) => {
return params.some((p) => {
if (p.required && !(p.name in param)) {
config.logger.warn(
`dynamic route (${logCount}) ${colors.yellow(
url
)} - missing required param ${colors.cyan(p.name)}`
)
return true
}
if (p.array && p.name in param) {
const value = param[p.name as keyof typeof param]
const valid = Array.isArray(value)
if (!valid) {
config.logger.warn(
`dynamic route (${logCount}) ${colors.yellow(url)} - param ${colors.cyan(
p.name
)} must be an array, got string "${colors.cyan(value)}"`
)
return true
}
} else if (!p.array && p.name in param) {
const value = param[p.name as keyof typeof param]
const valid = !Array.isArray(value)
if (!valid) {
config.logger.warn(
`dynamic route (${logCount}) ${colors.yellow(url)} - param ${colors.cyan(
p.name
)} must be string, got array ${colors.cyan(`[${value.join(', ')}]`)}`
)
return true
}
}
})
})
if (invalidParams.length) {
continue
}
// render each static param
for (const subindex in routeStaticParams) {
const logSubCount = `${1 + parseInt(subindex, 10)}/${routeStaticParams.length}`
const param = routeStaticParams[subindex]
const paramUrl = params.reduce((url, p) => {
if (p.name in param) {
const value = param[p.name as keyof typeof param]
if (Array.isArray(value)) {
return url.replace(p.param, value.join('/'))
} else {
return url.replace(p.param, value)
}
} else {
return url.replace(p.param, '')
}
}, url)
await renderPage({
url: paramUrl,
init,
render,
template,
manifest,
outStatic,
config,
logCount: `${logCount} - ${logSubCount}`,
cwd,
})
}
continue
}
await renderPage({
url,
init,
render,
template,
manifest,
outStatic,
config,
logCount,
cwd,
})
}
// done, delete ssr manifest
await fsp.rm(path.join(outServer), { recursive: true, force: true })
// when `vite-plugin-pwa` is presented, use it to regenerate SW after rendering
const pwaPlugin = config.plugins.find((plugin) => plugin.name === 'vite-plugin-pwa')
?.api
if (pwaPlugin && !pwaPlugin.disabled && pwaPlugin.generateSW) {
config.logger.info(colors.green('[SSG] Regenerate PWA...'))
await pwaPlugin.generateSW()
// update sw.js to replace /index.html with nothing so that it can be served from /
const swPath = path.join(outStatic, 'sw.js')
const swContent = await fsp.readFile(swPath, 'utf-8')
await fsp.writeFile(swPath, swContent.replace(/\/index\.html/g, ''), 'utf-8')
}
config.logger.info(
[
`Pre-rendering done. You can now serve the ${colors.cyan(
out.replace(cwd, '.')
)} directory with a static file server.`,
`Example:`,
` ${colors.green('pnpm ssg:serve')}`,
].join('\n')
)
process.exit(0)
}
async function renderPage({
url,
init,
render,
template,
manifest,
outStatic,
config,
logCount,
cwd,
}: any) {
init()
const {
found,
appHtml,
headTags,
htmlAttrs,
bodyAttrs,
bodyTagsOpen,
bodyTags,
preloadLinks,
initialState,
} = await render(url, manifest)
if (!found) {
return
}
const html = template
.replace(`<html>`, `<html${htmlAttrs}>`)
.replace(`<head>`, `<head><meta charset="UTF-8" />${headTags}`)
.replace(`</head>`, `${preloadLinks}</head>`)
.replace(`<body>`, `<body${bodyAttrs}>${bodyTagsOpen}`)
.replace(`</body>`, `${bodyTags}</body>`)
.replace(
/<div id="app"([\s\w\-"'=[\]]*)><\/div>/,
`<div id="app" data-server-rendered="true"$1>${appHtml}</div><script>window.__vuero__=${initialState}</script>`
)
const file = `${url.endsWith('/') ? `${url}` : `${url}/`}index.html`
const filePath = path.join(outStatic, file)
const dirname = path.dirname(filePath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
fs.writeFileSync(resolve(filePath), html)
config.logger.info(
colors.dim(
`pre-rendered (${logCount}) ${colors.green(url)} - ${colors.cyan(
filePath.replace(cwd, '.')
)}`
)
)
}
;(async () => {
try {
await build()
} catch (e) {
console.error(e)
process.exit(1)
}
})()