Skip to content

Commit

Permalink
chore: upgrade to chokidar 4 (#2502)
Browse files Browse the repository at this point in the history
Also remove long-obsolete sapper handling
  • Loading branch information
benmccann authored Sep 26, 2024
1 parent 7c76ec5 commit fc2144b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.25",
"@vscode/emmet-helper": "2.8.4",
"chokidar": "^3.4.1",
"chokidar": "^4.0.1",
"estree-walker": "^2.0.1",
"fdir": "^6.2.0",
"lodash": "^4.17.21",
Expand Down
7 changes: 1 addition & 6 deletions packages/language-server/src/lib/FallbackWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ export class FallbackWatcher {
this.watcher = watch(
workspacePaths.map((workspacePath) => join(workspacePath, recursivePatterns)),
{
ignored: (path: string) =>
gitOrNodeModules.test(path) &&
// Handle Sapper's alias mapping
!path.includes('src/node_modules') &&
!path.includes('src\\node_modules'),

ignored: gitOrNodeModules,
// typescript would scan the project files on init.
// We only need to know what got updated.
ignoreInitial: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.25",
"chokidar": "^3.4.1",
"chokidar": "^4.0.1",
"fdir": "^6.2.0",
"picocolors": "^1.0.0",
"sade": "^1.7.4"
Expand Down
76 changes: 51 additions & 25 deletions packages/svelte-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,7 @@ async function openAllDocuments(
) {
const offset = workspaceUri.fsPath.length + 1;
// We support a very limited subset of glob patterns: You can only have ** at the end or the start
const ignored: Array<(path: string) => boolean> = filePathsToIgnore.map((i) => {
if (i.endsWith('**')) i = i.slice(0, -2);

if (i.startsWith('**')) {
i = i.slice(2);

if (i.includes('*'))
throw new Error(
'Invalid svelte-check --ignore pattern: Only ** at the start or end is supported'
);

return (path) => path.includes(i);
}

if (i.includes('*'))
throw new Error(
'Invalid svelte-check --ignore pattern: Only ** at the start or end is supported'
);

return (path) => path.startsWith(i);
});
const ignored = createIgnored(filePathsToIgnore);
const isIgnored = (path: string) => {
path = path.slice(offset);
for (const i of ignored) {
Expand Down Expand Up @@ -84,6 +64,30 @@ async function openAllDocuments(
}
}

function createIgnored(filePathsToIgnore: string[]): Array<(path: string) => boolean> {
return filePathsToIgnore.map((i) => {
if (i.endsWith('**')) i = i.slice(0, -2);

if (i.startsWith('**')) {
i = i.slice(2);

if (i.includes('*'))
throw new Error(
'Invalid svelte-check --ignore pattern: Only ** at the start or end is supported'
);

return (path) => path.includes(i);
}

if (i.includes('*'))
throw new Error(
'Invalid svelte-check --ignore pattern: Only ** at the start or end is supported'
);

return (path) => path.startsWith(i);
});
}

async function getDiagnostics(
workspaceUri: URI,
writer: Writer,
Expand Down Expand Up @@ -149,10 +153,32 @@ class DiagnosticsWatcher {
filePathsToIgnore: string[],
ignoreInitialAdd: boolean
) {
watch(`${workspaceUri.fsPath}/**/*.{svelte,d.ts,ts,js,jsx,tsx,mjs,cjs,mts,cts}`, {
ignored: ['node_modules', 'vite.config.{js,ts}.timestamp-*']
.concat(filePathsToIgnore)
.map((ignore) => path.join(workspaceUri.fsPath, ignore)),
const fileEnding = /\.(svelte|d\.ts|ts|js|jsx|tsx|mjs|cjs|mts|cts)$/;
const viteConfigRegex = /vite\.config\.(js|ts)\.timestamp-/;
const userIgnored = createIgnored(filePathsToIgnore);
const offset = workspaceUri.fsPath.length + 1;

watch(workspaceUri.fsPath, {
ignored: (path, stats) => {
if (
path.includes('node_modules') ||
path.includes('.git') ||
(stats?.isFile() && (!fileEnding.test(path) || viteConfigRegex.test(path)))
) {
return true;
}

if (userIgnored.length !== 0) {
path = path.slice(offset);
for (const i of userIgnored) {
if (i(path)) {
return true;
}
}
}

return false;
},
ignoreInitial: ignoreInitialAdd
})
.on('add', (path) => this.updateDocument(path, true))
Expand Down
22 changes: 18 additions & 4 deletions pnpm-lock.yaml

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

0 comments on commit fc2144b

Please sign in to comment.