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

Bugfix: always set useFSEvents option for Chokidar #500

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/wmr/src/lib/fs-watcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chokidar from 'chokidar';

// We disable Chokidar's automatic fsevents usage, so the useFSEvents option is required.
let useFsEvents;
function initFsEvents() {
if (useFsEvents === undefined) {
try {
eval('require')('fsevents');
useFsEvents = true;
} catch (e) {
useFsEvents = false;
}
}
}

/** @type {typeof chokidar.watch} */
export function watch(files, opts) {
initFsEvents();
return chokidar.watch(files, {
useFsEvents,
...opts
});
}
4 changes: 2 additions & 2 deletions packages/wmr/src/start.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chokidar from 'chokidar';
import * as kl from 'kolorist';
import server from './server.js';
import wmrMiddleware from './wmr-middleware.js';
import { getServerAddresses, supportsSearchParams } from './lib/net-utils.js';
import { normalizeOptions } from './lib/normalize-options.js';
import { setCwd } from './plugins/npm-plugin/registry.js';
import { formatBootMessage, debug, hasDebugFlag } from './lib/output-utils.js';
import { watch } from './lib/fs-watcher.js';

/**
* @typedef OtherOptions
Expand Down Expand Up @@ -43,7 +43,7 @@ export default async function start(options = {}) {
console.log(kl.yellow(`WMR: Automatic config reloading is not supported on Node <= 12.18.4`));
} else {
const logWatcher = debug('wmr:watcher');
const watcher = chokidar.watch(configWatchFiles, {
const watcher = watch(configWatchFiles, {
cwd: cloned.root,
disableGlobbing: true
});
Expand Down
13 changes: 3 additions & 10 deletions packages/wmr/src/wmr-middleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { resolve, dirname, relative, sep, posix } from 'path';
import * as kl from 'kolorist';
import { promises as fs, createReadStream } from 'fs';
import chokidar from 'chokidar';
import wmrPlugin, { getWmrClient } from './plugins/wmr/plugin.js';
import wmrStylesPlugin, { modularizeCss, processSass } from './plugins/wmr/styles-plugin.js';
import { createPluginContainer } from './lib/rollup-plugin-container.js';
Expand All @@ -11,6 +10,7 @@ import sassPlugin from './plugins/sass-plugin.js';
import { getMimeType } from './lib/mimetypes.js';
import { codeFrame } from './lib/output-utils.js';
import { getPlugins } from './lib/plugins.js';
import { watch } from './lib/fs-watcher.js';

const NOOP = () => {};

Expand Down Expand Up @@ -44,17 +44,10 @@ export default function wmrMiddleware(options) {

NonRollup.buildStart();

let useFsEvents = false;
try {
eval('require')('fsevents');
useFsEvents = true;
} catch (e) {}

const watcher = chokidar.watch([cwd, resolve(root, 'package.json')], {
const watcher = watch([cwd, resolve(root, 'package.json')], {
cwd,
disableGlobbing: true,
ignored: [/(^|[/\\])(node_modules|\.git|\.DS_Store)([/\\]|$)/, resolve(cwd, out), resolve(cwd, distDir)],
useFsEvents
ignored: [/(^|[/\\])(node_modules|\.git|\.DS_Store)([/\\]|$)/, resolve(cwd, out), resolve(cwd, distDir)]
});
const pendingChanges = new Set();

Expand Down