Compile-Time Flags
Vue uses compile-time flags to enable conditional compilation and better tree-shaking in production builds.Overview
Compile-time flags are global constants replaced at build time by bundlers. They allow:- Dead code elimination in production
- Feature toggles for bundle size optimization
- Development-only code that gets stripped in production
- Platform-specific code paths
Core Flags
__DEV__
Indicates development mode.
Type: boolean
Usage:
__BROWSER__
Indicates code is running in a browser environment.
Type: boolean
Usage:
- Browser builds:
true - SSR builds:
false - Universal builds: Set via bundler
__GLOBAL__
Indicates building for IIFE (global variable) format.
Type: boolean
Usage:
- Global/UMD builds:
true - ESM/CJS builds:
false
__ESM_BROWSER__
Indicates building for browser ESM format.
Type: boolean
Usage:
- ESM-browser builds:
true - Other builds:
false
__ESM_BUNDLER__
Indicates building for bundler ESM format.
Type: boolean
Usage:
- ESM-bundler builds:
true - Other builds:
false
__CJS__
Indicates building for CommonJS format.
Type: boolean
Usage:
- CommonJS builds:
true - Other builds:
false
Feature Flags
__FEATURE_OPTIONS_API__
Enables/disables the Options API.
Type: boolean
Default: true
Usage:
Disable to reduce bundle size if only using Composition API:
- ~20-30KB reduction in bundle size
- Options API becomes unavailable
- Only Composition API works
__FEATURE_PROD_DEVTOOLS__
Enables Vue DevTools in production.
Type: boolean
Default: false
Usage:
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__
Enables detailed hydration mismatch warnings in production SSR.
Type: boolean
Default: false
Usage:
Compiler-Specific Flags
Compiler-SFC Flags
Internal flags used in@vue/compiler-sfc:
Source Map Flags
Configuration
Vite
webpack
Rollup
Runtime Feature Detection
Vue automatically initializes feature flags at runtime if not provided:TypeScript Declarations
Declare flags in your project for type safety:Best Practices
1. Always Define Feature Flags
Explicitly define all feature flags in your build config to avoid runtime warnings:2. Use JSON.stringify()
Always wrap values inJSON.stringify() to ensure proper string replacement:
3. Tree-Shaking Optimization
Structure code to maximize tree-shaking:4. Guard Development Imports
Keep development-only imports inside__DEV__ checks:
5. Platform-Specific Code
Use flags for platform-specific implementations:Bundle Size Impact
Disabling features reduces bundle size:| Flag | Size Savings | Impact |
|---|---|---|
__VUE_OPTIONS_API__: false | ~20-30 KB | Removes Options API |
__VUE_PROD_DEVTOOLS__: false | ~5-10 KB | Removes devtools integration |
__DEV__: false | Varies | Removes all dev warnings |