Conditional code

The build tool’s static code analyzer is capable of removing dead code branches from the bundles it creates. Named conditional blocks are defined using Dojo framework’s has module and can be statically set to true or false through .dojorc and removed at build time.

main.ts

  1. import has from '@dojo/framework/has';
  2. if (has('production')) {
  3. console.log('Starting in production');
  4. } else {
  5. console.log('Starting in dev mode');
  6. }
  7. export const mode = has('production') ? 'dist' : 'dev';

.dojorc

  1. {
  2. "build-app": {
  3. "features": {
  4. "production": true
  5. }
  6. }
  7. }

The above production feature will be set true for production builds (dist mode). The build system uses @dojo/framework/has to identify code as unreachable and remove those dead code branches from the build.

For example, the above code would be rewritten as:

static-build-loader output

  1. import has from '@dojo/framework/has';
  2. if (true) {
  3. console.log('Starting in production');
  4. } else {
  5. console.log('Starting in dev mode');
  6. }
  7. export const mode = true ? 'dist' : 'dev';

The build tool’s dead branch removal would then remove the unreachable code.

Uglify output

  1. console.log('Starting in production');
  2. export const mode = 'dist';

Any features which are not statically asserted, are not re-written. This allows the code to determine at run-time if the feature is present.

Provided features

These features are provided by the build system to help identify a specific environment or mode of operation.

Feature FlagDescription
debugProvides a way to create a code path for code that is only usable when debugging or providing enhanced diagnostics that are not desired in a production build. Defaults to true but should be configured statically as false in production builds.
host-browserDetermines if the current environment contains a window and document object in the global context, therefore it is generally safe to assume the code is running in a browser environment.
host-nodeAttempts to detect if the environment appears to be a node environment.
build-time-renderStatically defined by the build-time rendering system during build-time rendering.