fix(bridge): port nested _nuxt dir fix to bridge vite (#3538)

This commit is contained in:
Daniel Roe 2022-03-07 14:37:42 +00:00 committed by GitHub
parent 120ee4f795
commit 919db5b514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -1,11 +1,13 @@
import { promises as fsp } from 'fs' import { promises as fsp } from 'fs'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import fse from 'fs-extra'
import { addPluginTemplate, useNuxt } from '@nuxt/kit' import { addPluginTemplate, useNuxt } from '@nuxt/kit'
import { joinURL, stringifyQuery } from 'ufo' import { joinURL, stringifyQuery, withoutTrailingSlash } from 'ufo'
import { resolve, join } from 'pathe' import { resolve, join } from 'pathe'
import { build, generate, prepare, getNitroContext, NitroContext, createDevServer, wpfs, resolveMiddleware, scanMiddleware, writeTypes } from '@nuxt/nitro' import { build, generate, prepare, getNitroContext, NitroContext, createDevServer, wpfs, resolveMiddleware, scanMiddleware, writeTypes } from '@nuxt/nitro'
import { AsyncLoadingPlugin } from './async-loading' import { AsyncLoadingPlugin } from './async-loading'
import { distDir } from './dirs' import { distDir } from './dirs'
import { isDirectory, readDirRecursively } from './vite/utils/fs'
export function setupNitroBridge () { export function setupNitroBridge () {
const nuxt = useNuxt() const nuxt = useNuxt()
@ -69,6 +71,34 @@ export function setupNitroBridge () {
}) })
} }
nuxt.hook('nitro:generate', async () => {
const clientDist = resolve(nuxt.options.buildDir, 'dist/client')
// Remove public files that have been duplicated into buildAssetsDir
// TODO: Add option to configure this behaviour in vite
const publicDir = join(nuxt.options.srcDir, nuxt.options.dir.static)
let publicFiles: string[] = []
if (await isDirectory(publicDir)) {
publicFiles = readDirRecursively(publicDir).map(r => r.replace(publicDir, ''))
for (const file of publicFiles) {
try {
fse.rmSync(join(clientDist, file))
} catch {}
}
}
// Copy doubly-nested /_nuxt/_nuxt files into buildAssetsDir
// TODO: Workaround vite issue
if (await isDirectory(clientDist)) {
const nestedAssetsPath = withoutTrailingSlash(join(clientDist, nuxt.options.app.buildAssetsDir))
if (await isDirectory(nestedAssetsPath)) {
await fse.copy(nestedAssetsPath, clientDist, { recursive: true })
await fse.remove(nestedAssetsPath)
}
}
})
// Expose process.env.NITRO_PRESET // Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset nuxt.options.env.NITRO_PRESET = nitroContext.preset

View File

@ -0,0 +1,18 @@
import { promises as fsp, readdirSync, statSync } from 'fs'
import { join } from 'pathe'
export function readDirRecursively (dir: string) {
return readdirSync(dir).reduce((files, file) => {
const name = join(dir, file)
const isDirectory = statSync(name).isDirectory()
return isDirectory ? [...files, ...readDirRecursively(name)] : [...files, name]
}, [])
}
export async function isDirectory (path: string) {
try {
return (await fsp.stat(path)).isDirectory()
} catch (_err) {
return false
}
}