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
* Don't try to load unquoted plain-CSS indented-syntax imports.

View File

@ -156,23 +156,39 @@ String _cleanErrorMessage(_SystemError error) {
}
bool fileExists(String path) {
try {
return _fs.statSync(path).isFile();
} catch (error) {
var systemError = error as _SystemError;
if (systemError.code == 'ENOENT') return false;
rethrow;
}
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 {
return _fs.statSync(path).isFile();
} catch (error) {
var systemError = error as _SystemError;
if (systemError.code == 'ENOENT') return false;
rethrow;
}
});
}
bool dirExists(String path) {
try {
return _fs.statSync(path).isDirectory();
} catch (error) {
var systemError = error as _SystemError;
if (systemError.code == 'ENOENT') return false;
rethrow;
}
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 {
return _fs.statSync(path).isDirectory();
} catch (error) {
var systemError = error as _SystemError;
if (systemError.code == 'ENOENT') return false;
rethrow;
}
});
}
void ensureDir(String path) {

View File

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