2024-06-24 09:39:38 +00:00
|
|
|
import { readFileSync } from 'node:fs'
|
|
|
|
import { mkdir, rm, writeFile } from 'node:fs/promises'
|
|
|
|
|
2023-09-11 08:47:57 +00:00
|
|
|
import { relative, resolve } from 'pathe'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { withTrailingSlash, withoutLeadingSlash } from 'ufo'
|
2022-07-21 10:44:33 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2022-08-16 11:19:39 +00:00
|
|
|
import { normalizeViteManifest } from 'vue-bundle-renderer'
|
2021-10-12 14:05:20 +00:00
|
|
|
import type { ViteBuildContext } from './vite'
|
|
|
|
|
2024-07-01 09:30:09 +00:00
|
|
|
export async function writeManifest (ctx: ViteBuildContext) {
|
2021-10-12 14:05:20 +00:00
|
|
|
// Write client manifest for use in vue-bundle-renderer
|
|
|
|
const clientDist = resolve(ctx.nuxt.options.buildDir, 'dist/client')
|
|
|
|
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
|
|
|
|
|
2023-12-14 10:32:22 +00:00
|
|
|
const manifestFile = resolve(clientDist, 'manifest.json')
|
2024-07-02 18:27:38 +00:00
|
|
|
const clientManifest = JSON.parse(readFileSync(manifestFile, 'utf-8'))
|
2021-10-12 14:05:20 +00:00
|
|
|
|
2022-07-25 09:52:21 +00:00
|
|
|
const buildAssetsDir = withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir))
|
|
|
|
const BASE_RE = new RegExp(`^${escapeRE(buildAssetsDir)}`)
|
|
|
|
|
2022-07-21 10:44:33 +00:00
|
|
|
for (const key in clientManifest) {
|
2023-12-14 11:25:20 +00:00
|
|
|
const entry = clientManifest[key]
|
|
|
|
if (entry.file) {
|
|
|
|
entry.file = entry.file.replace(BASE_RE, '')
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
|
|
|
for (const item of ['css', 'assets']) {
|
2023-12-14 11:25:20 +00:00
|
|
|
if (entry[item]) {
|
|
|
|
entry[item] = entry[item].map((i: string) => i.replace(BASE_RE, ''))
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:39:38 +00:00
|
|
|
await mkdir(serverDist, { recursive: true })
|
2022-08-16 11:19:39 +00:00
|
|
|
|
2023-09-11 08:47:57 +00:00
|
|
|
if (ctx.config.build?.cssCodeSplit === false) {
|
2024-06-13 22:35:00 +00:00
|
|
|
for (const key in clientManifest as Record<string, { file?: string }>) {
|
|
|
|
const val = clientManifest[key]
|
|
|
|
if (val.file?.endsWith('.css')) {
|
|
|
|
const key = relative(ctx.config.root!, ctx.entry)
|
|
|
|
clientManifest[key].css ||= []
|
|
|
|
clientManifest[key].css!.push(val.file)
|
|
|
|
break
|
|
|
|
}
|
2023-09-11 08:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:52:34 +00:00
|
|
|
const manifest = normalizeViteManifest(clientManifest)
|
2022-08-16 11:19:39 +00:00
|
|
|
await ctx.nuxt.callHook('build:manifest', manifest)
|
2023-12-21 09:26:59 +00:00
|
|
|
const stringifiedManifest = JSON.stringify(manifest, null, 2)
|
2024-06-24 09:39:38 +00:00
|
|
|
await writeFile(resolve(serverDist, 'client.manifest.json'), stringifiedManifest, 'utf8')
|
|
|
|
await writeFile(resolve(serverDist, 'client.manifest.mjs'), 'export default ' + stringifiedManifest, 'utf8')
|
2022-08-29 09:37:53 +00:00
|
|
|
|
|
|
|
if (!ctx.nuxt.options.dev) {
|
2024-06-24 09:39:38 +00:00
|
|
|
await rm(manifestFile, { force: true })
|
2022-08-29 09:37:53 +00:00
|
|
|
}
|
2021-10-12 14:05:20 +00:00
|
|
|
}
|