Are you an LLM? You can read better optimized documentation at /options/context.md for this page in Markdown format
context
- Type:
string - Optional: Yes ✅
The value of this at the top level of each output chunk. For IIFE and UMD formats, this defaults to 'window' or 'global' depending on the platform.
Examples
Set custom context
js
export default {
context: 'globalThis',
output: {
format: 'iife',
},
};Use window for browser builds
js
export default {
context: 'window',
platform: 'browser',
output: {
format: 'iife',
},
};In-depth
The context option controls what this refers to in the top-level scope of your bundled code. This is particularly important for:
- IIFE bundles that need to access global objects
- UMD bundles that run in multiple environments
- Code that references
thisat the top level
By default:
- For
'iife'and'umd'formats on browser platform:thisis'window' - For
'iife'and'umd'formats on node platform:thisis'global' - For ES modules:
thisisundefined
Using 'globalThis' provides a cross-platform way to access the global object that works in both browser and Node.js environments.