logo
dojo/framework

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

import has from '@dojo/framework/has';

if (has('production')) {
    console.log('Starting in production');
} else {
    console.log('Starting in dev mode');
}

export const mode = has('production') ? 'dist' : 'dev';

.dojorc

{
    "build-app": {
        "features": {
            "production": true
        }
    }
}

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

import has from '@dojo/framework/has';

if (true) {
    console.log('Starting in production');
} else {
    console.log('Starting in dev mode');
}

export const mode = true ? 'dist' : 'dev';

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

Uglify output

console.log('Starting in production');
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 Flag Description
debug Provides 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-browser Determines 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-node Attempts to detect if the environment appears to be a node environment.
build-time-render Statically defined by the build-time rendering system during build-time rendering.