Vite Plugin for SAP CDS
raw JSON → 0.2.0 verified Mon Apr 27 auth: no javascript
A Vite plugin for @sap/cds (CAP) that enables importing .cds model files, running cds watch with Vite, and using the @sap/cds runtime in the browser. Current version 0.2.0 supports @sap/cds ^9. Release cadence is irregular with multiple minor patches. Key differentiators: allows using Vite's HMR and bundling for CAP frontends, and enables running the CAP runtime inside the browser via polyfills. Requires peer dependency @sap/cds ^9.
Common errors
error Cannot find module '@sap/cds' ↓
cause @sap/cds is not installed as a peer dependency.
fix
Run npm install @sap/cds
error Uncaught TypeError: cds.compile is not a function ↓
cause cds imported from wrong source or not initialized.
fix
Ensure you import cds from '@sap/cds', not from the plugin: import cds from '@sap/cds'
error The requested module 'vite' does not provide an export named 'defineConfig' ↓
cause Vite is not installed or version mismatch.
fix
Run npm install vite@5
Warnings
breaking The plugin is designed for Vite 5+; compatibility with older Vite versions is not guaranteed. ↓
fix Use Vite 5 or later.
deprecated The 'minify: false' option was removed in v0.1.10; manual chunks configuration may be needed. ↓
fix If you need to disable minify, configure Vite's build.minify or use a custom rollup plugin.
gotcha When using the CAP runtime in the browser, cds.context is not unique per request/transaction due to limited async context tracking. ↓
fix Avoid relying on cds.context for per-request state in browser usage.
Install
npm install vite-plugin-cds yarn add vite-plugin-cds pnpm add vite-plugin-cds Imports
- cds wrong
import cds from 'vite-plugin-cds' or const cds = require('vite-plugin-cds')correctimport { cds } from 'vite-plugin-cds' - node wrong
import node from 'vite-plugin-cds'correctimport { node } from 'vite-plugin-cds' - cap wrong
import cap from 'vite-plugin-cds'correctimport { cap } from 'vite-plugin-cds'
Quickstart
// vite.config.js
import { defineConfig } from 'vite'
import { cds } from 'vite-plugin-cds'
export default defineConfig({
plugins: [cds()],
root: './app',
})
// In your JavaScript file:
import cdsModel from './index.cds'
console.log(cdsModel) // compiled CSN object