Check fs.existsSync() before running fs.statSync() (#812)

This produces non-negligible speed improvements for Node users with
lots of imports.
This commit is contained in:
Natalie Weizenbaum 2019-09-03 13:26:57 -07:00 committed by GitHub
parent d9ebb5eff0
commit 35880d171f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 15 deletions

View File

@ -1,3 +1,7 @@
## 1.22.12
* Improve the performance of Node.js compilation involving many `@import`s.
## 1.22.11 ## 1.22.11
* Don't try to load unquoted plain-CSS indented-syntax imports. * Don't try to load unquoted plain-CSS indented-syntax imports.

View File

@ -156,6 +156,13 @@ String _cleanErrorMessage(_SystemError error) {
} }
bool fileExists(String path) { bool fileExists(String path) {
return _systemErrorToFileSystemException(() {
// `existsSync()` is faster than `statSync()`, but it doesn't clarify
// whether the entity in question is a file or a directory. Since false
// negatives are much more common than false positives, it works out in our
// favor to check this first.
if (!_fs.existsSync(path)) return false;
try { try {
return _fs.statSync(path).isFile(); return _fs.statSync(path).isFile();
} catch (error) { } catch (error) {
@ -163,9 +170,17 @@ bool fileExists(String path) {
if (systemError.code == 'ENOENT') return false; if (systemError.code == 'ENOENT') return false;
rethrow; rethrow;
} }
});
} }
bool dirExists(String path) { bool dirExists(String path) {
return _systemErrorToFileSystemException(() {
// `existsSync()` is faster than `statSync()`, but it doesn't clarify
// whether the entity in question is a file or a directory. Since false
// negatives are much more common than false positives, it works out in our
// favor to check this first.
if (!_fs.existsSync(path)) return false;
try { try {
return _fs.statSync(path).isDirectory(); return _fs.statSync(path).isDirectory();
} catch (error) { } catch (error) {
@ -173,6 +188,7 @@ bool dirExists(String path) {
if (systemError.code == 'ENOENT') return false; if (systemError.code == 'ENOENT') return false;
rethrow; rethrow;
} }
});
} }
void ensureDir(String path) { void ensureDir(String path) {

View File

@ -1,5 +1,5 @@
name: sass name: sass
version: 1.22.11 version: 1.22.12-dev
description: A Sass implementation in Dart. description: A Sass implementation in Dart.
author: Sass Team author: Sass Team
homepage: https://github.com/sass/dart-sass homepage: https://github.com/sass/dart-sass