Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cacheDir check for the FILLEXTREME #1708

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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