2021-10-12 14:05:20 +00:00
|
|
|
import fse from 'fs-extra'
|
|
|
|
import { resolve } from 'pathe'
|
2022-07-21 10:44:33 +00:00
|
|
|
import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
|
|
|
|
import escapeRE from 'escape-string-regexp'
|
2021-10-12 14:05:20 +00:00
|
|
|
import type { ViteBuildContext } from './vite'
|
|
|
|
|
2021-10-27 11:35:06 +00:00
|
|
|
export async function writeManifest (ctx: ViteBuildContext, extraEntries: 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')
|
|
|
|
|
|
|
|
const entries = [
|
|
|
|
'@vite/client',
|
|
|
|
'entry.mjs',
|
2021-10-27 11:35:06 +00:00
|
|
|
...extraEntries
|
2021-10-12 14:05:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
// Legacy dev manifest
|
|
|
|
const devClientManifest = {
|
2022-01-18 16:59:14 +00:00
|
|
|
publicPath: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir),
|
2021-10-12 14:05:20 +00:00
|
|
|
all: entries,
|
|
|
|
initial: entries,
|
|
|
|
async: [],
|
|
|
|
modules: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientManifest = ctx.nuxt.options.dev
|
|
|
|
? devClientManifest
|
|
|
|
: await fse.readJSON(resolve(clientDist, 'manifest.json'))
|
|
|
|
|
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) {
|
|
|
|
if (clientManifest[key].file) {
|
|
|
|
clientManifest[key].file = clientManifest[key].file.replace(BASE_RE, '')
|
|
|
|
}
|
|
|
|
for (const item of ['css', 'assets']) {
|
|
|
|
if (clientManifest[key][item]) {
|
|
|
|
clientManifest[key][item] = clientManifest[key][item].map(i => i.replace(BASE_RE, ''))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:05:20 +00:00
|
|
|
await fse.mkdirp(serverDist)
|
|
|
|
await fse.writeFile(resolve(serverDist, 'client.manifest.json'), JSON.stringify(clientManifest, null, 2), 'utf8')
|
|
|
|
await fse.writeFile(resolve(serverDist, 'client.manifest.mjs'), 'export default ' + JSON.stringify(clientManifest, null, 2), 'utf8')
|
|
|
|
}
|