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.

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
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.
npm install vite-plugin-cds
yarn add vite-plugin-cds
pnpm add vite-plugin-cds

Import a .cds model file in a Vite app using the plugin. The plugin compiles the model at build time.

// 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