Skip to content

Commit

Permalink
feat: cacheDir check for the FILLEXTREME
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangmo8 committed Aug 9, 2024
1 parent 04b8c5f commit ceb581c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
12 changes: 11 additions & 1 deletion packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
Logger,
bootstrap,
clearScreen,
getCacheDir,
isCacheDirExists,
normalizeBasePath,
printServerUrls
} from '../utils/index.js';
Expand Down Expand Up @@ -125,14 +127,22 @@ 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();

// 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);

Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/utils/cacheDir.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
try {
const hasCacheDir = fs.readdirSync(dir, { withFileTypes: true });

return !!(hasCacheDir && hasCacheDir.length);
} catch (e) {
return false;
}
}
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: '';
Expand Down
19 changes: 6 additions & 13 deletions packages/core/src/watcher/create-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@ 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,
insideChokidarOptions: WatchOptions
) {
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: [
Expand Down

0 comments on commit ceb581c

Please sign in to comment.