fix(vite): don't copy publicDir files to _nuxt (#2135)

This commit is contained in:
Daniel Roe 2021-11-24 15:42:38 +00:00 committed by GitHub
parent 84b6201dea
commit c5eed5e12b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import * as rollup from 'rollup'
import fse from 'fs-extra'
import { printFSTree } from './utils/tree'
import { getRollupConfig } from './rollup/config'
import { hl, prettyPath, serializeTemplate, writeFile, isDirectory } from './utils'
import { hl, prettyPath, serializeTemplate, writeFile, isDirectory, readDirRecursively } from './utils'
import { NitroContext } from './context'
import { scanMiddleware } from './server/middleware'
@ -30,14 +30,20 @@ async function cleanupDir (dir: string) {
export async function generate (nitroContext: NitroContext) {
consola.start('Generating public...')
const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client')
if (await isDirectory(clientDist)) {
await fse.copy(clientDist, join(nitroContext.output.publicDir, nitroContext._nuxt.publicPath))
const publicDir = nitroContext._nuxt.publicDir
let publicFiles: string[] = []
if (await isDirectory(publicDir)) {
publicFiles = readDirRecursively(publicDir).map(r => r.replace(publicDir, ''))
await fse.copy(publicDir, nitroContext.output.publicDir)
}
const publicDir = nitroContext._nuxt.publicDir
if (await isDirectory(publicDir)) {
await fse.copy(publicDir, nitroContext.output.publicDir)
const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client')
if (await isDirectory(clientDist)) {
await fse.copy(clientDist, join(nitroContext.output.publicDir, nitroContext._nuxt.publicPath), {
// TODO: Workaround vite's issue that duplicates public files
// https://github.com/nuxt/framework/issues/1192
filter: src => !publicFiles.includes(src.replace(clientDist, ''))
})
}
consola.success('Generated public ' + prettyPath(nitroContext.output.publicDir))

View File

@ -1,5 +1,5 @@
import { createRequire } from 'module'
import { relative, dirname, resolve } from 'pathe'
import { relative, dirname, join, resolve } from 'pathe'
import fse from 'fs-extra'
import jiti from 'jiti'
import defu from 'defu'
@ -148,3 +148,11 @@ export function readPackageJson (
throw error
}
}
export function readDirRecursively (dir: string) {
return fse.readdirSync(dir).reduce((files, file) => {
const name = join(dir, file)
const isDirectory = fse.statSync(name).isDirectory()
return isDirectory ? [...files, ...readDirRecursively(name)] : [...files, name]
}, [])
}

View File

@ -49,6 +49,7 @@ export async function bundle (nuxt: Nuxt) {
}
},
base: nuxt.options.build.publicPath,
publicDir: resolve(nuxt.options.srcDir, nuxt.options.dir.public),
// TODO: move to kit schema when it exists
vue: {
isProduction: !nuxt.options.dev,