From 262961cb576d46f5518097c2c79c3e0dfb0e384d Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Thu, 6 Jul 2023 16:03:32 -0700 Subject: [PATCH] Check the fs library to determine if we're on Node.js (#2033) This fixes an issue where some environments (like VS Code) define `process` but don't load the Node.js entrypoint, causing Sass to think it's running under Node without access to its Node dependencies. Closes #2032 --- CHANGELOG.md | 5 +++++ lib/src/io/node.dart | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bee925237..d626470f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ emitted in source order as much as possible, instead of always being emitted after the CSS of all module dependencies. +### JavaScript API + +* Produce a better error message when an environment that supports some Node.js + APIs loads the browser entrypoint but attempts to access the filesystem. + ### Embedded Sass * Fix a bug where nested relative `@imports` failed to load when using the diff --git a/lib/src/io/node.dart b/lib/src/io/node.dart index 6baa2d2ed..da58048a2 100644 --- a/lib/src/io/node.dart +++ b/lib/src/io/node.dart @@ -230,7 +230,16 @@ bool get isMacOS => process?.platform == 'darwin'; bool get isJS => true; -bool get isNode => process != null; +/// The fs module object, used to check whether this has been loaded as Node. +/// +/// It's safest to check for a library we load in manually rather than one +/// that's ambiently available so that we don't get into a weird state in +/// environments like VS Code that support some Node.js libraries but don't load +/// Node.js entrypoints for dependencies. +@JS('fs') +external final Object? _fsNullable; + +bool get isNode => _fsNullable != null; bool get isBrowser => isJS && !isNode;