Skip to content

LikeC4 Vite Plugin

NPM VersionNPM Downloads

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.


  1. To get started, we will need to create a new Vite project using react-ts template.

    # create a new project with npm
    npm create vite@latest -- --template react-ts


  2. Add likec4 dependency:

    npm i -D likec4


  3. 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(),
    ],
    })


  4. Add types reference to the vite-env.d.ts file
    (or create new one, like src/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"
    ]
    }
    }


  5. Create src/tutorial.c4 and copy the following model from tutorial:

    src/tutorial.c4
    // Tutorial - https://likec4.dev/tutorial/
    specification {
    element actor
    element system
    element component
    }
    model {
    customer = actor 'Customer' {
    description 'The regular customer of the system'
    }
    44 collapsed lines
    saas = system 'Our SaaS' {
    component ui 'Frontend' {
    description 'Nextjs application, hosted on Vercel'
    style {
    icon tech:nextjs
    shape browser
    }
    }
    component backend 'Backend Services' {
    description '
    Implements business logic
    and exposes as REST API
    '
    }
    // UI requests data from the Backend
    ui -> backend 'fetches via HTTPS'
    }
    // Customer uses the UI
    customer -> 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
    }
    }
    }


  6. Change the src/main.tsx file and import LikeC4 view from the likec4:react module:

    src/main.tsx
    import { createRoot } from 'react-dom/client'
    import { LikeC4View } from 'likec4:react'
    createRoot(document.getElementById('root')!).render(
    <LikeC4View viewId='index' />
    )


  7. npm run dev

    Open the browser and navigate to http://localhost:5173/.
    You should see the LikeC4 diagram rendered in your app.

OptionDescription
workspacedirectory with source files (defaults to vite root)
printErrorsif model is invalid, errors are reported to the logger (default true)
throwIfInvalidfails with rejected promise if model is invalid (default false)
graphvizwasm (default) or binary - use local binaries of Graphviz (“dot”) or bundled WASM

If you have multiple projects in your workspace:

src/main.tsx
// where `project-a` and `project-b` are the names of your projects
import { 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' />
</>
)

It is also possible to initiate using LikeC4 API:

vite.config.ts
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,
}),
],
})

Other modules are available to get access to the model:

// For multi-project workspaces
import { projects } from 'likec4:projects'
// Pick first one (default)
import { useLikeC4Views, useLikeC4View } from 'likec4:single-project'
// Project by name
import { useLikeC4Views, useLikeC4View } from 'likec4:model/project-a'
// Other modules
import { 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

You can use LikeC4VitePlugin with Astro and Starlight documentation tool as well.
Configure Astro:

astro.config.mjs
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:

src/components/LikeC4View.astro
---
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:

src/content/docs/example.mdx
---
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" />

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:

src/likec4/index.tsx
export * from 'likec4:react'

Configure Vite:

vite.config.ts
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:

pages/index.tsx
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.