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 fse from 'fs-extra'
import { printFSTree } from './utils/tree' import { printFSTree } from './utils/tree'
import { getRollupConfig } from './rollup/config' 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 { NitroContext } from './context'
import { scanMiddleware } from './server/middleware' import { scanMiddleware } from './server/middleware'
@ -30,14 +30,20 @@ async function cleanupDir (dir: string) {
export async function generate (nitroContext: NitroContext) { export async function generate (nitroContext: NitroContext) {
consola.start('Generating public...') consola.start('Generating public...')
const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client') const publicDir = nitroContext._nuxt.publicDir
if (await isDirectory(clientDist)) { let publicFiles: string[] = []
await fse.copy(clientDist, join(nitroContext.output.publicDir, nitroContext._nuxt.publicPath)) if (await isDirectory(publicDir)) {
publicFiles = readDirRecursively(publicDir).map(r => r.replace(publicDir, ''))
await fse.copy(publicDir, nitroContext.output.publicDir)
} }
const publicDir = nitroContext._nuxt.publicDir const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client')
if (await isDirectory(publicDir)) { if (await isDirectory(clientDist)) {
await fse.copy(publicDir, nitroContext.output.publicDir) 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)) consola.success('Generated public ' + prettyPath(nitroContext.output.publicDir))

View File

@ -1,5 +1,5 @@
import { createRequire } from 'module' import { createRequire } from 'module'
import { relative, dirname, resolve } from 'pathe' import { relative, dirname, join, resolve } from 'pathe'
import fse from 'fs-extra' import fse from 'fs-extra'
import jiti from 'jiti' import jiti from 'jiti'
import defu from 'defu' import defu from 'defu'
@ -148,3 +148,11 @@ export function readPackageJson (
throw error 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, base: nuxt.options.build.publicPath,
publicDir: resolve(nuxt.options.srcDir, nuxt.options.dir.public),
// TODO: move to kit schema when it exists // TODO: move to kit schema when it exists
vue: { vue: {
isProduction: !nuxt.options.dev, isProduction: !nuxt.options.dev,