From 92cbbc3fcaefe864a7e1e19593f22138e623d87f Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 13 Mar 2023 14:01:54 +0200 Subject: [PATCH 1/3] perf: improve isFileReadable performance --- packages/vite/src/node/utils.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index b614da2696318c..93ea4295c6f765 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -537,7 +537,16 @@ export function writeFile( export function isFileReadable(filename: string): boolean { try { + // The "throwIfNoEntry" is performance optimization for cases where file does not exist + const fileExists = Boolean(fs.statSync(filename, { throwIfNoEntry: false })) + + if (!fileExists) { + return false + } + + // Check if current process has read permission to the file fs.accessSync(filename, fs.constants.R_OK) + return true } catch { return false From 38b90bdc722547a45bd0712451be70df47124464 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 13 Mar 2023 14:40:43 +0100 Subject: [PATCH 2/3] chore: comment typo --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 93ea4295c6f765..1413ea9204b12e 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -537,7 +537,7 @@ export function writeFile( export function isFileReadable(filename: string): boolean { try { - // The "throwIfNoEntry" is performance optimization for cases where file does not exist + // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist const fileExists = Boolean(fs.statSync(filename, { throwIfNoEntry: false })) if (!fileExists) { From c392e25ea3f17b6015f3b8a1aeab343f5977fe47 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 13 Mar 2023 16:08:03 +0200 Subject: [PATCH 3/3] chore: simplify condition --- packages/vite/src/node/utils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 1413ea9204b12e..e48db640fe2280 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -538,9 +538,7 @@ export function writeFile( export function isFileReadable(filename: string): boolean { try { // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist - const fileExists = Boolean(fs.statSync(filename, { throwIfNoEntry: false })) - - if (!fileExists) { + if (!fs.statSync(filename, { throwIfNoEntry: false })) { return false }