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'
|
2024-09-18 19:41:53 +00:00
|
|
|
import type { Manifest as RendererManifest } from 'vue-bundle-renderer'
|
|
|
|
import type { Manifest as ViteClientManifest } from 'vite'
|
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) {
|
2024-08-09 16:43:42 +00:00
|
|
|
// This is only used for ssr: false - when ssr is enabled we use vite-node runtime manifest
|
2024-09-18 19:41:53 +00:00
|
|
|
const devClientManifest: RendererManifest = {
|
2024-08-09 16:43:42 +00:00
|
|
|
'@vite/client': {
|
|
|
|
isEntry: true,
|
|
|
|
file: '@vite/client',
|
|
|
|
css: [],
|
|
|
|
module: true,
|
|
|
|
resourceType: 'script',
|
|
|
|
},
|
|
|
|
[ctx.entry]: {
|
|
|
|
isEntry: true,
|
|
|
|
file: ctx.entry,
|
|
|
|
module: true,
|
|
|
|
resourceType: 'script',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-09-18 19:41:53 +00:00
|
|
|
const clientManifest = ctx.nuxt.options.dev ? devClientManifest : JSON.parse(readFileSync(manifestFile, 'utf-8')) as ViteClientManifest
|
|
|
|
const manifestEntries = Object.values(clientManifest)
|
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)}`)
|
|
|
|
|
2024-09-18 19:41:53 +00:00
|
|
|
for (const entry of manifestEntries) {
|
2023-12-14 11:25:20 +00:00
|
|
|
if (entry.file) {
|
|
|
|
entry.file = entry.file.replace(BASE_RE, '')
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
2024-09-18 19:41:53 +00:00
|
|
|
for (const item of ['css', 'assets'] as const) {
|
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-09-18 19:41:53 +00:00
|
|
|
for (const entry of manifestEntries) {
|
|
|
|
if (entry.file?.endsWith('.css')) {
|
2024-06-13 22:35:00 +00:00
|
|
|
const key = relative(ctx.config.root!, ctx.entry)
|
2024-09-18 19:41:53 +00:00
|
|
|
clientManifest[key]!.css ||= []
|
|
|
|
;(clientManifest[key]!.css as string[]).push(entry.file)
|
2024-06-13 22:35:00 +00:00
|
|
|
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
|
|
|
}
|