LikeC4 Vite Plugin
LikeC4 Vite Plugin allows you to embed views directly, without any pre-build/generate steps.
The plugin automatically generates all the necessary code to render the views in your application, with Hot Module Replacement (HMR) supported.
This is useful for building documentation, tutorials, or any other application where you want to include diagrams.
-
Create Vite project
Section titled “Create Vite project”To get started, we will need to create a new Vite project using react-ts template.
# create a new project with npmnpm create vite@latest -- --template react-ts# create a new project with pnpmpnpm create vite@latest --template react-ts# create a new project with yarnyarn create vite --template react-ts# create a new project with bunbun create vite@latest --template react-ts
-
Install LikeC4
Section titled “Install LikeC4”Add
likec4
dependency:npm i -D likec4pnpm add -D likec4yarn add -D likec4bun add -d likec4
-
Configure Vite
Section titled “Configure Vite”Add LikeC4 plugin to vite config:
vite.config.ts import { defineConfig } from 'vite'import react from '@vitejs/plugin-react'import { LikeC4VitePlugin } from 'likec4/vite-plugin'export default defineConfig({plugins: [react(),LikeC4VitePlugin(),],})
-
Add type references
Section titled “Add type references”Add types reference to the
vite-env.d.ts
file
(or create new one, likesrc/likec4.d.ts
)src/vite-env.d.ts /// <reference types="vite/client" />/// <reference types="likec4/vite-plugin-modules" />Another option is to add to the
tsconfig.json
:tsconfig.json {"compilerOptions": {"types": ["likec4/vite-plugin-modules"]}}
-
Add LikeC4 model
Section titled “Add LikeC4 model”Create
src/tutorial.c4
and copy the following model from tutorial:src/tutorial.c4 // Tutorial - https://likec4.dev/tutorial/specification {element actorelement systemelement component}model {customer = actor 'Customer' {description 'The regular customer of the system'}44 collapsed linessaas = system 'Our SaaS' {component ui 'Frontend' {description 'Nextjs application, hosted on Vercel'style {icon tech:nextjsshape browser}}component backend 'Backend Services' {description 'Implements business logicand exposes as REST API'}// UI requests data from the Backendui -> backend 'fetches via HTTPS'}// Customer uses the UIcustomer -> ui 'opens in browser'customer -> saas 'enjoys our product'}views {view index {title 'Landscape view'include *}view saas of saas {include *style * {opacity 25%}style customer {color muted}}}
-
Use LikeC4 view in your app
Section titled “Use LikeC4 view in your app”Change the
src/main.tsx
file and import LikeC4 view from thelikec4:react
module:src/main.tsx import { createRoot } from 'react-dom/client'import { LikeC4View } from 'likec4:react'createRoot(document.getElementById('root')!).render(<LikeC4View viewId='index' />)
-
Start vite dev server
Section titled “Start vite dev server”npm run devpnpm run devyarn run devbun run devOpen the browser and navigate to
http://localhost:5173/
.
You should see the LikeC4 diagram rendered in your app.
Plugin
Section titled “Plugin”Options
Section titled “Options”Option | Description |
---|---|
workspace | directory with source files (defaults to vite root) |
printErrors | if model is invalid, errors are reported to the logger (default true ) |
throwIfInvalid | fails with rejected promise if model is invalid (default false ) |
graphviz | wasm (default) or binary - use local binaries of Graphviz (“dot”) or bundled WASM |
Multi-project workspaces
Section titled “Multi-project workspaces”If you have multiple projects in your workspace:
// where `project-a` and `project-b` are the names of your projectsimport { LikeC4View as ProjectA_LikeC4View } from 'likec4:react/project-a'import { LikeC4View as ProjectB_LikeC4View } from 'likec4:react/project-b'
const example = () => ( <> <ProjectA_LikeC4View viewId='index' /> <ProjectB_LikeC4View viewId='index' /> </>)
Usage with API
Section titled “Usage with API”It is also possible to initiate using LikeC4 API:
import { defineConfig } from 'vite'import react from '@vitejs/plugin-react'import { LikeC4 } from 'likec4'import { LikeC4VitePlugin } from 'likec4/vite-plugin'
const { languageServices } = await LikeC4.fromSource(` specification { element system element user } model { customer = user 'Customer' cloud = system 'System' } views { view index { include * } }`)
export default defineConfig({ plugins: [ react(), LikeC4VitePlugin({ languageServices, }), ],})
Virtual modules
Section titled “Virtual modules”Other modules are available to get access to the model:
// For multi-project workspacesimport { projects } from 'likec4:projects'
// Pick first one (default)import { useLikeC4Views, useLikeC4View } from 'likec4:single-project'
// Project by nameimport { useLikeC4Views, useLikeC4View } from 'likec4:model/project-a'
// Other modulesimport { loadDotSources } from 'likec4:dot'import { mmdSource } from 'likec4:mmd/project-a'
Complete list - vite-plugin/modules.d.ts
Here are some examples of how to use the plugin with different frameworks
With Astro
Section titled “With Astro”You can use LikeC4VitePlugin with Astro and Starlight documentation tool as well.
Configure Astro:
import { defineConfig } from 'astro/config';import starlight from '@astrojs/starlight';import { LikeC4VitePlugin } from 'likec4/vite-plugin'
export default defineConfig({ integrations: [ starlight({ title: 'Your architecture docs site', }), ], vite: { plugins: [ LikeC4VitePlugin({}), ], },});
To use React components, first you need to wrap them in astro components:
---import { LikeC4View as ReactLikeC4View, type LikeC4ViewId } from 'likec4:react';interface Props { viewId: LikeC4ViewId;}const { viewId } = Astro.props---
<ReactLikeC4View viewId={viewId} client:only="react"></ReactLikeC4View>
Then you can use in markdown:
---title: Welcome to my docs---
import LikeC4View from '../../components/LikeC4View.astro';
## Introduction
This is an example of using LikeC4 in your documentation
<LikeC4View viewId="index" />
With Next.js
Section titled “With Next.js”For Next.js (since it does not use Vite), there is a workaround - library mode:
Vite will generate a bundled library with likec4 diagrams, that you can import from your Next.js app.
Export everything from likec4:react
:
export * from 'likec4:react'
Configure Vite:
import { defineConfig } from 'vite'import react from '@vitejs/plugin-react'import { LikeC4VitePlugin } from 'likec4/vite-plugin'
export default defineConfig({ build: { // Build views to 'lib' directory outDir: 'lib', lib: { entry: 'src/likec4/index.tsx', }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled // to avoid code duplication external: [ 'react', 'react-dom', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react-dom/client', ], }, }, plugins: [ react(), LikeC4VitePlugin(), ],})
Run vite build
and import outputs from your Next.js app:
import { LikeC4View } from '../lib'
export default function Home() { return ( <LikeC4View viewId="index" /> )}
You can run vite build --watch
as a background process to watch for changes in likec4 source files.