mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
feat(nuxi): auto cleanup with project manifest changes (#6672)
This commit is contained in:
parent
94214d6b32
commit
4d5f768aaf
@ -1,5 +1,5 @@
|
||||
import { resolve } from 'pathe'
|
||||
import { cleanupNuxtDirs } from '../utils/fs'
|
||||
import { cleanupNuxtDirs } from '../utils/nuxt'
|
||||
import { defineNuxtCommand } from './index'
|
||||
|
||||
export default defineNuxtCommand({
|
||||
|
@ -11,6 +11,7 @@ import { writeTypes } from '../utils/prepare'
|
||||
import { loadKit } from '../utils/kit'
|
||||
import { importModule } from '../utils/cjs'
|
||||
import { overrideEnv } from '../utils/env'
|
||||
import { writeNuxtManifest, loadNuxtManifest, cleanupNuxtDirs } from '../utils/nuxt'
|
||||
import { defineNuxtCommand } from './index'
|
||||
|
||||
export default defineNuxtCommand({
|
||||
@ -75,6 +76,15 @@ export default defineNuxtCommand({
|
||||
showURL()
|
||||
}
|
||||
|
||||
// Write manifest and also check if we need cache invalidation
|
||||
if (!isRestart) {
|
||||
const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir)
|
||||
const newManifest = await writeNuxtManifest(currentNuxt)
|
||||
if (previousManifest && newManifest && previousManifest._hash !== newManifest._hash) {
|
||||
await cleanupNuxtDirs(currentNuxt.options.rootDir)
|
||||
}
|
||||
}
|
||||
|
||||
await currentNuxt.ready()
|
||||
|
||||
await currentNuxt.hooks.callHook('listen', listener.server, listener)
|
||||
|
@ -4,7 +4,8 @@ import consola from 'consola'
|
||||
import { resolve } from 'pathe'
|
||||
import { resolveModule } from '../utils/cjs'
|
||||
import { getPackageManager, packageManagerLocks } from '../utils/packageManagers'
|
||||
import { cleanupNuxtDirs, rmRecursive, touchFile } from '../utils/fs'
|
||||
import { rmRecursive, touchFile } from '../utils/fs'
|
||||
import { cleanupNuxtDirs } from '../utils/nuxt'
|
||||
import { defineNuxtCommand } from './index'
|
||||
|
||||
async function getNuxtVersion (paths: string | string[]): Promise<string|null> {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { promises as fsp } from 'node:fs'
|
||||
import { dirname, resolve } from 'pathe'
|
||||
import { dirname } from 'pathe'
|
||||
import consola from 'consola'
|
||||
|
||||
// Check if a file exists
|
||||
@ -29,18 +29,6 @@ export async function touchFile (path: string) {
|
||||
await fsp.utimes(path, time, time).catch(() => {})
|
||||
}
|
||||
|
||||
export async function cleanupNuxtDirs (rootDir: string) {
|
||||
consola.info('Cleaning up generated nuxt files and caches...')
|
||||
|
||||
await rmRecursive([
|
||||
'.nuxt',
|
||||
'.output',
|
||||
'dist',
|
||||
'node_modules/.vite',
|
||||
'node_modules/.cache'
|
||||
].map(dir => resolve(rootDir, dir)))
|
||||
}
|
||||
|
||||
export function findup<T> (rootDir: string, fn: (dir: string) => T | undefined): T | null {
|
||||
let dir = rootDir
|
||||
while (dir !== dirname(dir)) {
|
||||
|
58
packages/nuxi/src/utils/nuxt.ts
Normal file
58
packages/nuxi/src/utils/nuxt.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { promises as fsp } from 'node:fs'
|
||||
import { resolve, dirname } from 'pathe'
|
||||
import consola from 'consola'
|
||||
import { hash } from 'ohash'
|
||||
import type { Nuxt } from '@nuxt/schema'
|
||||
import { rmRecursive } from './fs'
|
||||
|
||||
export interface NuxtProjectManifest {
|
||||
_hash: string
|
||||
project: {
|
||||
rootDir: string
|
||||
},
|
||||
versions: {
|
||||
nuxt: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function cleanupNuxtDirs (rootDir: string) {
|
||||
consola.info('Cleaning up generated nuxt files and caches...')
|
||||
|
||||
await rmRecursive([
|
||||
'.nuxt',
|
||||
'.output',
|
||||
'dist',
|
||||
'node_modules/.vite',
|
||||
'node_modules/.cache'
|
||||
].map(dir => resolve(rootDir, dir)))
|
||||
}
|
||||
|
||||
export function resolveNuxtManifest (nuxt: Nuxt): NuxtProjectManifest {
|
||||
const manifest: NuxtProjectManifest = {
|
||||
_hash: null,
|
||||
project: {
|
||||
rootDir: nuxt.options.rootDir
|
||||
},
|
||||
versions: {
|
||||
nuxt: nuxt._version
|
||||
}
|
||||
}
|
||||
manifest._hash = hash(manifest)
|
||||
return manifest
|
||||
}
|
||||
|
||||
export async function writeNuxtManifest (nuxt: Nuxt): Promise<NuxtProjectManifest> {
|
||||
const manifest = resolveNuxtManifest(nuxt)
|
||||
const manifestPath = resolve(nuxt.options.buildDir, 'nuxt.json')
|
||||
await fsp.mkdir(dirname(manifestPath), { recursive: true })
|
||||
await fsp.writeFile(manifestPath, JSON.stringify(manifest, null, 2), 'utf-8')
|
||||
return manifest
|
||||
}
|
||||
|
||||
export async function loadNuxtManifest (buildDir: string): Promise<NuxtProjectManifest | null> {
|
||||
const manifestPath = resolve(buildDir, 'nuxt.json')
|
||||
const manifest: NuxtProjectManifest | null = await fsp.readFile(manifestPath, 'utf-8')
|
||||
.then(data => JSON.parse(data) as NuxtProjectManifest)
|
||||
.catch(() => null)
|
||||
return manifest
|
||||
}
|
Loading…
Reference in New Issue
Block a user