2021-10-12 14:05:20 +00:00
|
|
|
import fse from 'fs-extra'
|
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'
|
|
|
|
import type { Manifest } from 'vue-bundle-renderer'
|
2021-10-12 14:05:20 +00:00
|
|
|
import type { ViteBuildContext } from './vite'
|
|
|
|
|
2022-08-07 09:52:34 +00:00
|
|
|
export async function writeManifest (ctx: ViteBuildContext, css: string[] = []) {
|
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')
|
|
|
|
|
2022-08-07 09:52:34 +00:00
|
|
|
const devClientManifest: Manifest = {
|
|
|
|
'@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
|
|
|
}
|
|
|
|
|
2023-12-14 10:32:22 +00:00
|
|
|
const manifestFile = resolve(clientDist, 'manifest.json')
|
2021-10-12 14:05:20 +00:00
|
|
|
const clientManifest = ctx.nuxt.options.dev
|
|
|
|
? devClientManifest
|
2023-12-14 10:32:22 +00:00
|
|
|
: await fse.readJSON(manifestFile)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:05:20 +00:00
|
|
|
await fse.mkdirp(serverDist)
|
2022-08-16 11:19:39 +00:00
|
|
|
|
2023-09-11 08:47:57 +00:00
|
|
|
if (ctx.config.build?.cssCodeSplit === false) {
|
|
|
|
const entryCSS = Object.values(clientManifest as Record<string, { file?: string }>).find(val => (val).file?.endsWith('.css'))?.file
|
|
|
|
if (entryCSS) {
|
|
|
|
const key = relative(ctx.config.root!, ctx.entry)
|
|
|
|
clientManifest[key].css ||= []
|
|
|
|
clientManifest[key].css!.push(entryCSS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
await fse.writeFile(resolve(serverDist, 'client.manifest.json'), stringifiedManifest, 'utf8')
|
|
|
|
await fse.writeFile(resolve(serverDist, 'client.manifest.mjs'), 'export default ' + stringifiedManifest, 'utf8')
|
2022-08-29 09:37:53 +00:00
|
|
|
|
|
|
|
if (!ctx.nuxt.options.dev) {
|
2023-12-14 10:32:22 +00:00
|
|
|
await fse.rm(manifestFile, { force: true })
|
2022-08-29 09:37:53 +00:00
|
|
|
}
|
2021-10-12 14:05:20 +00:00
|
|
|
}
|