-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathbuild.ts
319 lines (271 loc) · 11 KB
/
build.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
/* eslint-disable no-console */
import { dirname, isAbsolute, join, parse } from 'path'
import { createRequire } from 'module'
import PQueue from 'p-queue'
import { blue, cyan, dim, gray, green, red, yellow } from 'kolorist'
import fs from 'fs-extra'
import type { InlineConfig, ResolvedConfig } from 'vite'
import { mergeConfig, resolveConfig, build as viteBuild } from 'vite'
import type { SSRContext } from 'vue/server-renderer'
import { nextTick } from 'vue'
import { JSDOM } from 'jsdom'
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
import type { RouteRecordRaw } from 'vue-router'
import { renderDOMHead } from '@unhead/dom'
import type { ViteSSGContext, ViteSSGOptions } from '../types'
import { serializeState } from '../utils/state'
import { renderPreloadLinks } from './preload-links'
import { buildLog, getSize, routesToPaths } from './utils'
import { getCritters } from './critical'
export type Manifest = Record<string, string[]>
export type CreateAppFactory = (client: boolean, routePath?: string) => Promise<ViteSSGContext<true> | ViteSSGContext<false>>
function DefaultIncludedRoutes(paths: string[], routes: Readonly<RouteRecordRaw[]>) {
// ignore dynamic routes
return paths.filter(i => !i.includes(':') && !i.includes('*'))
}
export async function build(ssgOptions: Partial<ViteSSGOptions> = {}, viteConfig: InlineConfig = {}) {
const mode = process.env.MODE || process.env.NODE_ENV || ssgOptions.mode || 'production'
const config = await resolveConfig(viteConfig, 'build', mode)
const cwd = process.cwd()
const root = config.root || cwd
const ssgOut = join(root, '.vite-ssg-temp')
const outDir = config.build.outDir || 'dist'
const out = isAbsolute(outDir) ? outDir : join(root, outDir)
const {
script = 'sync',
mock = false,
entry = await detectEntry(root),
formatting = 'none',
crittersOptions = {},
includedRoutes: configIncludedRoutes = DefaultIncludedRoutes,
onBeforePageRender,
onPageRendered,
onFinished,
dirStyle = 'flat',
includeAllRoutes = false,
format = 'esm',
concurrency = 20,
rootContainerId = 'app',
}: ViteSSGOptions = Object.assign({}, config.ssgOptions || {}, ssgOptions)
if (fs.existsSync(ssgOut))
await fs.remove(ssgOut)
// client
buildLog('Build for client...')
await viteBuild(mergeConfig(viteConfig, {
build: {
ssrManifest: true,
rollupOptions: {
input: {
app: join(root, './index.html'),
},
},
},
mode: config.mode,
}))
// load jsdom before building the SSR and so jsdom will be available
if (mock) {
// @ts-expect-error just ignore it
const { jsdomGlobal }: { jsdomGlobal: () => void } = await import('./jsdomGlobal.mjs')
jsdomGlobal()
}
// server
buildLog('Build for server...')
process.env.VITE_SSG = 'true'
const ssrEntry = await resolveAlias(config, entry)
await viteBuild(mergeConfig(viteConfig, {
build: {
ssr: ssrEntry,
outDir: ssgOut,
minify: false,
cssCodeSplit: false,
rollupOptions: {
output: format === 'esm'
? {
entryFileNames: '[name].mjs',
format: 'esm',
}
: {
entryFileNames: '[name].cjs',
format: 'cjs',
},
},
},
mode: config.mode,
}))
const prefix = format === 'esm' && process.platform === 'win32' ? 'file://' : ''
const ext = format === 'esm' ? '.mjs' : '.cjs'
const serverEntry = join(prefix, ssgOut, parse(ssrEntry).name + ext)
const _require = createRequire(import.meta.url)
const { createApp, includedRoutes: serverEntryIncludedRoutes }: { createApp: CreateAppFactory; includedRoutes: ViteSSGOptions['includedRoutes'] } = format === 'esm'
? await import(serverEntry)
: _require(serverEntry)
const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes
const { routes } = await createApp(false)
let routesPaths = includeAllRoutes
? routesToPaths(routes)
: await includedRoutes(routesToPaths(routes), routes || [])
// uniq
routesPaths = Array.from(new Set(routesPaths))
buildLog('Rendering Pages...', routesPaths.length)
const critters = crittersOptions !== false ? await getCritters(outDir, crittersOptions) : undefined
if (critters)
console.log(`${gray('[vite-ssg]')} ${blue('Critical CSS generation enabled via `critters`')}`)
const ssrManifest: Manifest = JSON.parse(await fs.readFile(join(out, 'ssr-manifest.json'), 'utf-8'))
let indexHTML = await fs.readFile(join(out, 'index.html'), 'utf-8')
indexHTML = rewriteScripts(indexHTML, script)
const { renderToString }: typeof import('vue/server-renderer') = await import('vue/server-renderer')
const queue = new PQueue({ concurrency })
for (const route of routesPaths) {
queue.add(async () => {
try {
const appCtx = await createApp(false, route) as ViteSSGContext<true>
const { app, router, head, initialState, triggerOnSSRAppRendered, transformState = serializeState } = appCtx
if (router) {
await router.push(route)
await router.isReady()
}
const transformedIndexHTML = (await onBeforePageRender?.(route, indexHTML, appCtx)) || indexHTML
const ctx: SSRContext = {}
const appHTML = await renderToString(app, ctx)
await triggerOnSSRAppRendered?.(route, appHTML, appCtx)
// need to resolve assets so render content first
const renderedHTML = await renderHTML({
rootContainerId,
indexHTML: transformedIndexHTML,
appHTML,
initialState: transformState(initialState),
})
// create jsdom from renderedHTML
const jsdom = new JSDOM(renderedHTML)
// render current page's preloadLinks
renderPreloadLinks(jsdom.window.document, ctx.modules || new Set<string>(), ssrManifest)
// render head
if (head)
await renderDOMHead(head.unhead, { document: jsdom.window.document })
const html = jsdom.serialize()
let transformed = (await onPageRendered?.(route, html, appCtx)) || html
if (critters)
transformed = await critters.process(transformed)
const formatted = await formatHtml(transformed, formatting)
const relativeRouteFile = `${(route.endsWith('/')
? `${route}index`
: route).replace(/^\//g, '')}.html`
const filename = dirStyle === 'nested'
? join(route.replace(/^\//g, ''), 'index.html')
: relativeRouteFile
await fs.ensureDir(join(out, dirname(filename)))
await fs.writeFile(join(out, filename), formatted, 'utf-8')
config.logger.info(
`${dim(`${outDir}/`)}${cyan(filename.padEnd(15, ' '))} ${dim(getSize(formatted))}`,
)
}
catch (err: any) {
throw new Error(`${gray('[vite-ssg]')} ${red(`Error on page: ${cyan(route)}`)}\n${err.stack}`)
}
})
}
await queue.start().onIdle()
await fs.remove(ssgOut)
// when `vite-plugin-pwa` is presented, use it to regenerate SW after rendering
const pwaPlugin: VitePluginPWAAPI = config.plugins.find(i => i.name === 'vite-plugin-pwa')?.api
if (pwaPlugin && !pwaPlugin.disabled && pwaPlugin.generateSW) {
buildLog('Regenerate PWA...')
await pwaPlugin.generateSW()
}
console.log(`\n${gray('[vite-ssg]')} ${green('Build finished.')}`)
await onFinished?.()
// ensure build process always exits
const waitInSeconds = 15
const timeout = setTimeout(() => {
console.log(`${gray('[vite-ssg]')} ${yellow(`Build process still running after ${waitInSeconds}s. There might be something misconfigured in your setup. Force exit.`)}`)
process.exit(0)
}, waitInSeconds * 1000)
timeout.unref() // don't wait for timeout
}
async function detectEntry(root: string) {
// pick the first script tag of type module as the entry
const scriptSrcReg = /<script(?:.*?)src=["'](.+?)["'](?!<)(?:.*)\>(?:[\n\r\s]*?)(?:<\/script>)/img
const html = await fs.readFile(join(root, 'index.html'), 'utf-8')
const scripts = [...html.matchAll(scriptSrcReg)] || []
const [, entry] = scripts.find((matchResult) => {
const [script] = matchResult
const [, scriptType] = script.match(/.*\stype=(?:'|")?([^>'"\s]+)/i) || []
return scriptType === 'module'
}) || []
return entry || 'src/main.ts'
}
async function resolveAlias(config: ResolvedConfig, entry: string) {
const resolver = config.createResolver()
const result = await resolver(entry, config.root)
return result || join(config.root, entry)
}
function rewriteScripts(indexHTML: string, mode?: string) {
if (!mode || mode === 'sync')
return indexHTML
return indexHTML.replace(/<script type="module" /g, `<script type="module" ${mode} `)
}
async function renderHTML({
rootContainerId,
indexHTML,
appHTML,
initialState,
}: {
rootContainerId: string
indexHTML: string
appHTML: string
initialState: any
},
) {
const stateScript = initialState
? `\n<script>window.__INITIAL_STATE__=${initialState}</script>`
: ''
const container = `<div id="${rootContainerId}"></div>`
if (indexHTML.includes(container)) {
return indexHTML
.replace(
container,
`<div id="${rootContainerId}" data-server-rendered="true">${appHTML}</div>${stateScript}`,
)
}
const html5Parser = await import('html5parser')
const ast = html5Parser.parse(indexHTML)
let renderedOutput: string | undefined
html5Parser.walk(ast, {
enter: (node) => {
if (!renderedOutput
&& node?.type === html5Parser.SyntaxKind.Tag
&& Array.isArray(node.attributes)
&& node.attributes.length > 0
&& node.attributes.some(attr => attr.name.value === 'id' && attr.value?.value === rootContainerId)
) {
const attributesStringified = [...node.attributes.map(({ name: { value: name }, value }) => `${name}="${value!.value}"`)].join(' ')
const indexHTMLBefore = indexHTML.slice(0, node.start)
const indexHTMLAfter = indexHTML.slice(node.end)
renderedOutput = `${indexHTMLBefore}<${node.name} ${attributesStringified} data-server-rendered="true">${appHTML}</${node.name}>${stateScript}${indexHTMLAfter}`
}
},
})
if (!renderedOutput)
throw new Error(`Could not find a tag with id="${rootContainerId}" to replace it with server-side rendered HTML`)
return renderedOutput
}
async function formatHtml(html: string, formatting: ViteSSGOptions['formatting']) {
if (formatting === 'minify') {
const htmlMinifier = await import('html-minifier')
return htmlMinifier.minify(html, {
collapseWhitespace: true,
caseSensitive: true,
collapseInlineTagWhitespace: false,
minifyJS: true,
minifyCSS: true,
})
}
else if (formatting === 'prettify') {
// @ts-expect-error dynamic import
const prettier = (await import('prettier/esm/standalone.mjs')).default
// @ts-expect-error dynamic import
const parserHTML = (await import('prettier/esm/parser-html.mjs')).default
return prettier.format(html, { semi: false, parser: 'html', plugins: [parserHTML] })
}
return html
}