Skip to content
Draft
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
2 changes: 1 addition & 1 deletion admin/scripts/copyUntypedFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import fs from 'fs-extra';
import path from 'path';
import chokidar from 'chokidar';
import * as chokidar from 'chokidar';

const srcDir = path.join(process.cwd(), 'src');
const libDir = path.join(process.cwd(), 'lib');
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@docusaurus/utils-common": "3.10.1",
"@docusaurus/utils-validation": "3.10.1",
"boxen": "^6.2.1",
"chokidar": "^3.5.3",
"chokidar": "^5.0.0",
"cli-table3": "^0.6.3",
"combine-promises": "^1.1.0",
"commander": "^5.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/start/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function doStart(

const reloadableSite = await createReloadableSite({siteDirParam, cliOptions});

setupSiteFileWatchers(
await setupSiteFileWatchers(
{props: reloadableSite.get().props, cliOptions},
({plugin}) => {
if (plugin) {
Expand Down
56 changes: 38 additions & 18 deletions packages/docusaurus/src/commands/start/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/

import path from 'path';
import chokidar from 'chokidar';
import {posixPath} from '@docusaurus/utils';
import * as chokidar from 'chokidar';
import {Globby, posixPath} from '@docusaurus/utils';
import picomatch from 'picomatch';

import type {StartCLIOptions} from './start';
import type {LoadedPlugin, Props} from '@docusaurus/types';

Expand All @@ -27,12 +29,18 @@ export function createPollingOptions(
};
}

export type FileWatchEventName =
| 'add'
| 'addDir'
| 'change'
| 'unlink'
| 'unlinkDir';
type ChokidarEventNames = keyof chokidar.FSWatcherEventMap;

// We only subscribe to a subset of Chokidar events we care about
const FileWatchEvents = [
'add',
'change',
'unlink',
'addDir',
'unlinkDir',
] as const satisfies ChokidarEventNames[];

export type FileWatchEventName = (typeof FileWatchEvents)[number];

export type FileWatchEvent = {
name: FileWatchEventName;
Expand All @@ -48,10 +56,10 @@ type WatchParams = {
* Watch file system paths for changes and emit events
* Returns an async handle to stop watching
*/
export function watch(
async function watch(
params: WatchParams,
callback: (event: FileWatchEvent) => void,
): () => Promise<void> {
): Promise<() => Promise<void>> {
const {pathsToWatch, siteDir, ...options} = params;

const fsWatcher = chokidar.watch(pathsToWatch, {
Expand All @@ -60,12 +68,22 @@ export function watch(
...options,
});

fsWatcher.on('all', (name, eventPath) => callback({name, path: eventPath}));
console.log('watch glob', {
patterns: pathsToWatch,
scans: pathsToWatch.map((pattern) => picomatch.scan(pattern).base),
result: await Globby(pathsToWatch),
});

FileWatchEvents.forEach((eventName) =>
fsWatcher.on(eventName, (eventPath) => {
callback({name: eventName, path: eventPath});
}),
);

return () => fsWatcher.close();
}

export function getSitePathsToWatch({props}: {props: Props}): string[] {
function getSitePathsToWatch({props}: {props: Props}): string[] {
return [
// TODO we should also watch all imported modules!
// Use https://github.com/vercel/nft ?
Expand All @@ -74,7 +92,7 @@ export function getSitePathsToWatch({props}: {props: Props}): string[] {
];
}

export function getPluginPathsToWatch({
function getPluginPathsToWatch({
siteDir,
plugin,
}: {
Expand All @@ -93,7 +111,7 @@ export function getPluginPathsToWatch({
.map(normalizeToSiteDir);
}

export function setupSiteFileWatchers(
export async function setupSiteFileWatchers(
{
props,
cliOptions,
Expand All @@ -105,15 +123,15 @@ export function setupSiteFileWatchers(
plugin: LoadedPlugin | null;
event: FileWatchEvent;
}) => void,
): void {
): Promise<void> {
const {siteDir} = props;
const pollingOptions = createPollingOptions(cliOptions);

// TODO on config / or local plugin updates,
// the getFilePathsToWatch lifecycle code might get updated
// so we should probably reset the watchers?

watch(
const siteWatcher = watch(
{
pathsToWatch: getSitePathsToWatch({props}),
siteDir: props.siteDir,
Expand All @@ -122,8 +140,8 @@ export function setupSiteFileWatchers(
(event) => callback({plugin: null, event}),
);

props.plugins.forEach((plugin) => {
watch(
const pluginWatchers = props.plugins.map((plugin) => {
return watch(
{
pathsToWatch: getPluginPathsToWatch({plugin, siteDir}),
siteDir,
Expand All @@ -132,4 +150,6 @@ export function setupSiteFileWatchers(
(event) => callback({plugin, event}),
);
});

await Promise.all([siteWatcher, ...pluginWatchers]);
}
42 changes: 2 additions & 40 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading