is_client.dart

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 <code>isClient</code> property value can be subjected to the dead code elimination feature provided by <code>dart2js</code>, the Dart to Javasctipt compiler.

Build Status

Code Example

// Sample Code: Check if the code is running on client-side or server-side.

import "package:is_client:is_client.dart"
...
if ( isClient) {
  print("Hello! I'm running on client-side!");
} else {
  print("Hello! I'm running on server-side!");
}
// Sample Code: Check if the code is running as JavaScript or not.

import "package:is_client:is_dartium.dart"
...
if ( isDartClient) {
  print("Hello! I'm a Dart on Dartium!");
} else {
  print("Hello! I'm a JavaScript on Chrome or else!");
}

Compile-time dead code elimination

By default, a value for <code>isClient</code> property are generated runtime. They are not treated as the compile-time constants. Meaning, the code block isolated by <code>if</code> statements WILL NOT be targetted by a dead code elination feature in <code>dart2js</code>.

To compile out the dead code, you must pass the <code>IS_CLIENT</code> constant variable to <code>dart2js</code> via the <code>-D</code> command line arguments.

dart2js --minify -DIS_CLIENT=true myapp.dart
// Code sample
import "package:is_client:is_client.dart"
...
if (isServer) {
  print("Hello! I'm a nonexisting block after dart2js's compilation!");
} else {
  print("Hello! I'm alive!");
}

Change Logs

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

License

is_client.dart is released under the [the MIT license](LICENSE) by Hiroshi Yamamoto

Libraries

is_client

Properties to check if an application is running on server-side or client-side.

is_dartium

Properties to check if a Dart application is running on server-side or client-side, Dart browsers or other JS browsers. This library can only be imported by a client-side code since this depends on <code>'dart:html'</code>