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

refactor: v2 watcher #2020

Merged
merged 2 commits into from
Dec 24, 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
3 changes: 1 addition & 2 deletions examples/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ fetch("https://wallhaven.fun/api/wallhaven/w/yx6e9l").then((res) => res.json());
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
<div>
<img width="500" src="/aaa.jpg" alt="">

<img width="500" src="/aaa.jpg" alt="">
</div>
</div>
<HelloWorld msg="Farm + Vue" />
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,19 @@ export class Server extends httpServer {

await this.watcher.createWatcher();

this.watcher.watcher.on('add', async (file: string) => {
this.watcher.on('add', async (file: string) => {
// TODO pluginContainer hooks
});

this.watcher.watcher.on('unlink', async (file: string) => {
this.watcher.on('unlink', async (file: string) => {
const parentFiles = this.compiler.getParentFiles(file);
const normalizeParentFiles = parentFiles.map((file) =>
normalizePath(file)
);
this.hmrEngine.hmrUpdate(normalizeParentFiles, true);
});

this.watcher.watcher.on('change', async (file: string) => {
this.watcher.on('change', async (file: string) => {
file = normalizePath(file);
const shortFile = getShortName(file, this.resolvedUserConfig.root);
const isConfigFile = this.resolvedUserConfig.configFilePath === file;
Expand Down Expand Up @@ -302,7 +302,7 @@ export class Server extends httpServer {
);

if (filteredAdded.length > 0) {
this.watcher.watcher.add(filteredAdded);
this.watcher.add(filteredAdded);
}
};

Expand Down Expand Up @@ -699,7 +699,7 @@ export class Server extends httpServer {
}

await Promise.allSettled([
this.watcher.watcher.close(),
this.watcher.close(),
this.ws.wss.close(),
this.closeHttpServerFn()
]);
Expand Down
151 changes: 46 additions & 105 deletions packages/core/src/watcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,53 @@ import type {

export const debugWatcher = createDebugger('farm:watcher');

export default class Watcher {
private watchedFiles = new Set<string>();
interface IWatcher {
resolvedWatchOptions: WatchOptions;
watcher: FSWatcher;
extraWatchedFiles: string[];
getInternalWatcher(): FSWatcher;
filterWatchFile(file: string, root: string): boolean;
getExtraWatchedFiles(compiler?: Compiler | null): string[];
watchExtraFiles(): void;
createWatcher(): Promise<void>;
resolveChokidarOptions(): void;
close(): Promise<void>;
}

export default class Watcher extends EventEmitter implements IWatcher {
private watchedFiles = new Set<string>();
public resolvedWatchOptions: WatchOptions;
public extraWatchedFiles: string[] = [];
private _watcher: FSWatcher;

constructor(public config: ResolvedUserConfig) {
super();
this.resolveChokidarOptions();
}

on(
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
listener: (path: string) => void
): this {
this._watcher.on(event, listener);
return this;
}

add(paths: string | ReadonlyArray<string>): this {
this._watcher.add(paths);
return this;
}

unwatch(paths: string | ReadonlyArray<string>): this {
this._watcher.unwatch(paths);
return this;
}

getWatched(): { [directory: string]: string[] } {
return this._watcher.getWatched();
}

getInternalWatcher() {
return this.watcher;
return this._watcher;
}

filterWatchFile(file: string, root: string): boolean {
Expand All @@ -54,114 +89,20 @@ export default class Watcher {
watchExtraFiles() {
this.extraWatchedFiles.forEach((file) => {
if (!this.watchedFiles.has(file)) {
this.watcher.add(file);
this._watcher.add(file);
this.watchedFiles.add(file);
}
});
}

async watch() {}

// async watch() {
// const compiler = this.getCompiler();

// const handlePathChange = async (path: string) => {
// if (this.close) return;

// try {
// if (this.compiler instanceof NewServer && this.compiler.getCompiler()) {
// await this.compiler.hmrEngine.hmrUpdate(path);
// }

// if (
// this.compiler instanceof Compiler &&
// this.compiler.hasModule(path)
// ) {
// await compilerHandler(
// async () => {
// const result = await compiler.update([path], true);
// this.handleUpdateFinish(result, compiler);
// compiler.writeResourcesToDisk();
// },
// this.config,
// this.logger,
// { clear: true }
// );
// }
// } catch (error) {
// this.logger.error(error);
// }
// };

// const filesToWatch = [this.config.root, ...this.getExtraWatchedFiles()];
// this.watchedFiles = new Set(filesToWatch);
// this.watcher ??= createWatcher(this.config, filesToWatch);

// this.watcher.on('change', (path) => {
// if (this.close) return;
// handlePathChange(path);
// });

// if (this.compiler instanceof NewServer) {
// this.compiler.hmrEngine?.onUpdateFinish((result) =>
// this.handleUpdateFinish(result, compiler)
// );
// }
// }

// async watchConfigs(callback: (files: string[]) => void) {
// const filesToWatch = Array.from([
// ...(this.config.envFiles ?? []),
// ...(this.config.configFileDependencies ?? []),
// ...(this.config.configFilePath ? [this.config.configFilePath] : [])
// ]).filter((file) => file && existsSync(file));
// const chokidarOptions = {
// awaitWriteFinish:
// process.platform === 'linux'
// ? undefined
// : {
// stabilityThreshold: 10,
// pollInterval: 80
// }
// };
// this.watcher ??= createWatcher(this.config, filesToWatch, chokidarOptions);

// this.watcher.on('change', (path) => {
// if (this.close) return;
// if (filesToWatch.includes(path)) {
// callback([path]);
// }
// });
// return this;
// }

// private handleUpdateFinish(updateResult: JsUpdateResult, compiler: Compiler) {
// const addedFiles = [
// ...updateResult.added,
// ...updateResult.extraWatchResult.add
// ].map((addedModule) =>
// compiler.transformModulePath(this.config.root, addedModule)
// );

// const filteredAdded = addedFiles.filter((file) =>
// this.filterWatchFile(file, this.config.root)
// );

// if (filteredAdded.length > 0) {
// this.watcher.add(filteredAdded);
// }
// }

async createWatcher() {
const compiler = await createInlineCompiler(this.config, {
progress: false
});
// TODO type error here
// @ts-ignore
const enabledWatcher = this.config.watch !== null;
const files = [this.config.root, ...this.getExtraWatchedFiles(compiler)];

this.watcher = enabledWatcher
this._watcher = enabledWatcher
? (chokidar.watch(files, this.resolvedWatchOptions) as FSWatcher)
: new NoopWatcher(this.resolvedWatchOptions);
}
Expand Down Expand Up @@ -200,10 +141,10 @@ export default class Watcher {
}

async close() {
if (this.watcher) {
if (this._watcher) {
debugWatcher?.('close watcher');
await this.watcher.close();
this.watcher = null;
await this._watcher.close();
this._watcher = null;
}
}
}
Expand Down Expand Up @@ -245,7 +186,7 @@ export async function handlerWatcher(
const logger = resolvedUserConfig.logger;
const watcher = new Watcher(resolvedUserConfig);
await watcher.createWatcher();
watcher.watcher.on('change', async (file: string | string[] | any) => {
watcher.on('change', async (file: string | string[] | any) => {
file = normalizePath(file);
// TODO restart with node side v2.0 we may be think about this feature
// const shortFile = getShortName(file, resolvedUserConfig.root);
Expand Down Expand Up @@ -278,7 +219,7 @@ export async function handlerWatcher(
);

if (filteredAdded.length > 0) {
watcher.watcher.add(filteredAdded);
watcher.add(filteredAdded);
}
};

Expand Down
Loading