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

OptionTypeDefaultDescription
portnumber3000Development server port
hoststring'localhost'Server hostname
hotbooleantrueEnable hot module replacement
openbooleanfalseOpen browser on start
httpsbooleanfalseEnable HTTPS
proxyobject{}API proxy configuration

Build Options

OptionTypeDefaultDescription
targetstring'es2020'JavaScript target version
outDirstring'dist'Output directory
minifybooleantrueMinify output
sourcemapbooleanfalseGenerate sourcemaps
cleanbooleantrueClean 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.