is_client
is a small Dart package with properties
(isClient
, isDartClient
) to check
if your application is running on server-side or client-side.
You can also check if the client-side code is running as Dart
(on Dartium) or as compiled JavaScript (on Chrome/Firefox/etc).
The dead code blocks isolated by isClient
property
value can be subjected to a dead code elimination feature
provided by dart2js
, the Dart to Javascript compiler.
// Exmple: Check if this code is running on client-side or server-side.
import "package:is_client/is_client.dart"
...
if ( isClient) {
print("Hello! I'm on client-side!");
} else {
print("Hello! I'm on server-side!");
}
// Example: Check if this code is run as JavaScript or not.
import "package:is_client/is_dartium.dart"
...
if ( isDartClient) {
print("Hello! I'm running Dart!");
} else {
print("Hello! I'm running JavaScript!");
}
By default, a value for isClient
property is
generated runtime. It is not treated as the compile-time
constants.
Meaning, code blocks isolated by if
statements
WILL NOT be targeted by a dead code elination feature in
dart2js
.
To compile out the dead code, you must pass a IS_CLIENT
constant variable to dart2js
via a -D
command
line argument.
dart2js --minify -DIS_CLIENT=true myapp.dart
import "package:is_client:is_client.dart"
...
if (isServer) {
print("Hello! I'm a nonexisting code block compiled out by dart2js!");
} else {
print("Hello!, client!");
}
** 1.6.0 (11/11/2014)**
- Dependency bump
- Tested with dart-1.6.0
** 1.2.0 (2/28/2014)**
- Tested with dart-1.2.0
** 1.1.3 (2/15/2014)**
- Tested with dart-1.1.0, dart 1.1.3
is_client.dart
is released under the the MIT license by Hiroshi Yamamoto higon@freepress.jp