Configuration
OpenCode uses a simple JavaScript configuration file that gives you complete control over your build process.
Configuration File
Create an opencode.config.js file in your project root:
// opencode.config.js
export default {
// Project info
name: 'my-app',
version: '1.0.0',
// Development server
dev: {
port: 3000,
host: 'localhost',
hot: true,
open: true
},
// Build configuration
build: {
target: 'es2022',
outDir: 'dist',
minify: true,
sourcemap: true
},
// Plugins
plugins: []
}
Dev Server Options
| Option | Type | Default | Description |
|---|---|---|---|
port | number | 3000 | Development server port |
host | string | 'localhost' | Server hostname |
hot | boolean | true | Enable hot module replacement |
open | boolean | false | Open browser on start |
https | boolean | false | Enable HTTPS |
proxy | object | {} | API proxy configuration |
Build Options
| Option | Type | Default | Description |
|---|---|---|---|
target | string | 'es2020' | JavaScript target version |
outDir | string | 'dist' | Output directory |
minify | boolean | true | Minify output |
sourcemap | boolean | false | Generate sourcemaps |
clean | boolean | true | Clean outDir before build |
API Proxy
Configure proxy rules for API requests during development:
export default {
dev: {
port: 3000,
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
}
Environment Variables
Access environment variables in your config:
export default {
build: {
minify: process.env.NODE_ENV === 'production',
sourcemap: process.env.NODE_ENV !== 'production'
}
}
Or use a .env file:
# .env
VITE_API_URL=https://api.example.com
VITE_DEBUG=true
TypeScript Configuration
For TypeScript projects, you can use opencode.config.ts:
import { defineConfig } from 'opencode';
export default defineConfig({
build: {
target: 'es2022',
minify: true
},
plugins: [
// TypeScript is included by default
]
});
Multiple Configurations
Export different configs for different environments:
import { defineConfig } from 'opencode';
export default defineConfig(({ mode }) => {
if (mode === 'development') {
return {
dev: { port: 3000 },
build: { sourcemap: true }
};
}
return {
build: {
minify: true,
sourcemap: false
}
};
});
💡 Configuration Tips
Use defineConfig for TypeScript intellisense and validation. Changes to the config file are automatically detected during development.