From 9271fa75b9828cdc771c55c2a28823215c67e91b Mon Sep 17 00:00:00 2001 From: zhangmo8 Date: Fri, 9 Aug 2024 12:40:48 +0800 Subject: [PATCH] feat: cacheDir check for the FILLEXTREME --- packages/core/src/server/index.ts | 12 +++++++- packages/core/src/utils/cacheDir.ts | 31 +++++++++++++++++++++ packages/core/src/utils/index.ts | 1 + packages/core/src/utils/logger.ts | 4 +-- packages/core/src/watcher/create-watcher.ts | 19 ++++--------- 5 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 packages/core/src/utils/cacheDir.ts diff --git a/packages/core/src/server/index.ts b/packages/core/src/server/index.ts index 8b08c430c..d66cd2f29 100644 --- a/packages/core/src/server/index.ts +++ b/packages/core/src/server/index.ts @@ -25,6 +25,8 @@ import { Logger, bootstrap, clearScreen, + getCacheDir, + isCacheDirExists, normalizeBasePath, printServerUrls } from '../utils/index.js'; @@ -125,6 +127,14 @@ export class Server implements ImplDevServer { } const { port, open, protocol, hostname } = this.config; + // check if cache dir exists + const hasCacheDir = await isCacheDirExists( + getCacheDir( + this.compiler.config.config.root, + this.compiler.config.config.persistentCache + ) + ); + const start = Date.now(); // compile the project and start the dev server await this.compile(); @@ -132,7 +142,7 @@ export class Server implements ImplDevServer { // watch extra files after compile this.watcher?.watchExtraFiles?.(); - bootstrap(Date.now() - start, this.compiler.config); + bootstrap(Date.now() - start, this.compiler.config, hasCacheDir); await this.startServer(this.config); diff --git a/packages/core/src/utils/cacheDir.ts b/packages/core/src/utils/cacheDir.ts new file mode 100644 index 000000000..112a0d634 --- /dev/null +++ b/packages/core/src/utils/cacheDir.ts @@ -0,0 +1,31 @@ +import fs from 'fs'; +import path from 'node:path'; + +import { PersistentCacheConfig } from '../types/binding.js'; + +export function getCacheDir( + root: string, + persistentCache?: boolean | PersistentCacheConfig +) { + let cacheDir = path.resolve(root, 'node_modules', '.farm', 'cache'); + + if (typeof persistentCache === 'object' && persistentCache.cacheDir) { + cacheDir = persistentCache.cacheDir; + + if (!path.isAbsolute(cacheDir)) { + cacheDir = path.resolve(root, cacheDir); + } + } + + return cacheDir; +} + +export async function isCacheDirExists(dir: string): Promise { + try { + const hasCacheDir = fs.readdirSync(dir, { withFileTypes: true }); + + return !!(hasCacheDir && hasCacheDir.length); + } catch (e) { + return false; + } +} diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index e720cc724..c011f57db 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -8,3 +8,4 @@ export * from './path.js'; export * from './publicDir.js'; export * from './rebase-url.js'; export * from './plugin-utils.js'; +export * from './cacheDir.js'; diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index 734f87179..469d34dd0 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -222,8 +222,8 @@ export function bootstrapLogger(options?: LoggerOptions): Logger { return new Logger(options); } -export function bootstrap(times: number, config: Config) { - const usePersistentCache = config.config.persistentCache; +export function bootstrap(times: number, config: Config, hasCacheDir: boolean) { + const usePersistentCache = config.config.persistentCache && hasCacheDir; const persistentCacheFlag = usePersistentCache ? colors.bold(PersistentCacheBrand) : ''; diff --git a/packages/core/src/watcher/create-watcher.ts b/packages/core/src/watcher/create-watcher.ts index 4877aae24..45bf14158 100644 --- a/packages/core/src/watcher/create-watcher.ts +++ b/packages/core/src/watcher/create-watcher.ts @@ -3,7 +3,7 @@ import path from 'node:path'; import chokidar, { FSWatcher, WatchOptions } from 'chokidar'; import glob from 'fast-glob'; -import { ResolvedUserConfig } from '../index.js'; +import { ResolvedUserConfig, getCacheDir } from '../index.js'; function resolveChokidarOptions( config: ResolvedUserConfig, @@ -11,18 +11,11 @@ function resolveChokidarOptions( ) { const { ignored = [], ...userChokidarOptions } = config.server?.hmr?.watchOptions ?? {}; - let cacheDir = path.resolve(config.root, 'node_modules', '.farm', 'cache'); - - if ( - typeof config.compilation?.persistentCache === 'object' && - config.compilation.persistentCache.cacheDir - ) { - cacheDir = config.compilation.persistentCache.cacheDir; - - if (!path.isAbsolute(cacheDir)) { - cacheDir = path.resolve(config.root, cacheDir); - } - } + + const cacheDir = getCacheDir( + config.root, + config.compilation?.persistentCache + ); const options: WatchOptions = { ignored: [