Skip to content

AI-native design editor. Open-source Figma alternative.

feat(dom-css): add Tailwind compiler adapter

Danila Poyarkov2c880186b09e
diff --git a/bun.lock b/bun.lock
index bfc396bc8..accfd2386 100644
--- a/bun.lock
+++ b/bun.lock
@@ -162,6 +162,7 @@
"dependencies": {
"@acemir/cssom": "^0.9.31",
"parse5": "^8.0.1",
+ "tailwindcss": "^4.2.1",
},
"devDependencies": {
"tsdown": "^0.21.7",
diff --git a/packages/dom-css/README.md b/packages/dom-css/README.md
index c1f274364..b8060e837 100644
--- a/packages/dom-css/README.md
+++ b/packages/dom-css/README.md
@@ -10,10 +10,10 @@ Current scope:
- Browser-backed runtime adapter for native HTML parsing, serialization, and computed-style extraction
- Headless runtime adapter with `parse5` HTML parsing and CSSOM-backed style computation for basic selectors, nested CSSOM rules, cascade order, inheritance, common shorthands, and simple custom-property/calc values
- Initial SceneGraph ⇄ DesignDOM conversion helpers for simple HTML/CSS-shaped layouts
-- Tailwind-generated CSS ingestion through CSSOM for utility-class card fixtures
+- `compileTailwindCSS()` adapter that delegates utility compilation to Tailwind v4 and feeds the generated CSS through CSSOM
Planned scope:
- Broader SceneGraph ⇄ DesignDOM conversion
- Browser runtime parity fixtures
-- First-class Tailwind compiler helpers
+- Broader Tailwind compiler options for custom themes, plugins, and caller-provided stylesheet loading
diff --git a/packages/dom-css/package.json b/packages/dom-css/package.json
index 6bd2ef63a..7a3dd7983 100644
--- a/packages/dom-css/package.json
+++ b/packages/dom-css/package.json
@@ -47,6 +47,7 @@
},
"dependencies": {
"@acemir/cssom": "^0.9.31",
- "parse5": "^8.0.1"
+ "parse5": "^8.0.1",
+ "tailwindcss": "^4.2.1"
}
}
diff --git a/packages/dom-css/src/index.ts b/packages/dom-css/src/index.ts
index 199413eef..d05203839 100644
--- a/packages/dom-css/src/index.ts
+++ b/packages/dom-css/src/index.ts
@@ -2,7 +2,9 @@ export { serializeHTML, serializeNode } from './serialize'
export { createBrowserCSSRuntime, createCSSRuntime, createHeadlessCSSRuntime } from './runtime'
export { designDocumentToSceneGraph } from './to-scene-graph'
export { sceneGraphToDesignDocument } from './from-scene-graph'
+export { compileTailwindCSS } from './tailwind'
export type { ToDesignDocumentOptions } from './from-scene-graph'
+export type { CompileTailwindCSSOptions } from './tailwind'
export type { ToSceneGraphOptions } from './to-scene-graph'
export type {
CSSComputeOptions,
diff --git a/packages/dom-css/src/tailwind.ts b/packages/dom-css/src/tailwind.ts
new file mode 100644
index 000000000..a071c0cb3
--- /dev/null
+++ b/packages/dom-css/src/tailwind.ts
@@ -0,0 +1,40 @@
+import { compile } from 'tailwindcss'
+
+export interface CompileTailwindCSSOptions {
+ css?: string
+ base?: string
+ loadStylesheet?: (id: string, base: string) => Promise<string>
+}
+
+const DEFAULT_TAILWIND_CSS = '@import "tailwindcss";'
+
+export async function compileTailwindCSS(
+ classes: string | Iterable<string>,
+ options: CompileTailwindCSSOptions = {}
+): Promise<string> {
+ const compiler = await compile(options.css ?? DEFAULT_TAILWIND_CSS, {
+ base: options.base,
+ loadStylesheet: async (id, base) => {
+ const content = options.loadStylesheet
+ ? await options.loadStylesheet(id, base)
+ : await loadTailwindStylesheet(id)
+ return { path: id, base, content }
+ }
+ })
+ return compiler.build(normalizeClasses(classes))
+}
+
+function normalizeClasses(classes: string | Iterable<string>): string[] {
+ const classNames = typeof classes === 'string' ? [classes] : Array.from(classes)
+ return classNames
+ .flatMap((className) => className.split(/\s+/))
+ .map((className) => className.trim())
+ .filter((className) => className.length > 0)
+}
+
+async function loadTailwindStylesheet(id: string): Promise<string> {
+ const stylesheet = id === 'tailwindcss' ? 'tailwindcss/index.css' : id
+ const url = new URL(import.meta.resolve(stylesheet))
+ const { readFile } = await import('node:fs/promises')
+ return readFile(url, 'utf8')
+}
diff --git a/packages/dom-css/tsdown.config.ts b/packages/dom-css/tsdown.config.ts
index ed597968c..ec959a2a5 100644
--- a/packages/dom-css/tsdown.config.ts
+++ b/packages/dom-css/tsdown.config.ts
@@ -15,7 +15,7 @@ export default defineConfig({
moduleSideEffects: false
},
deps: {
- neverBundle: ['@open-pencil/core', /^@open-pencil\/core\//],
+ neverBundle: ['@open-pencil/core', /^@open-pencil\/core\//, 'node:fs/promises'],
onlyBundle: false
}
})
diff --git a/tests/engine/dom-css/conversion.test.ts b/tests/engine/dom-css/conversion.test.ts
index 3ab8b4fe4..ee0f14ea8 100644
--- a/tests/engine/dom-css/conversion.test.ts
+++ b/tests/engine/dom-css/conversion.test.ts
@@ -1,10 +1,8 @@
import { describe, expect, it } from 'bun:test'
-import { readFile } from 'node:fs/promises'
-
-import { compile } from 'tailwindcss'
import { colorToCSS } from '@open-pencil/core/color'
import {
+ compileTailwindCSS,
createHeadlessCSSRuntime,
designDocumentToSceneGraph,
sceneGraphToDesignDocument,
@@ -23,8 +21,6 @@ const cardHTML = `
</article>
`
-const tailwindRoot = new URL('../../../node_modules/tailwindcss/', import.meta.url)
-
const cardCSS = `
.card {
display: flex;
@@ -50,19 +46,6 @@ const cardCSS = `
}
`
-async function compileTailwindClasses(classes: string[]) {
- const compiler = await compile('@import "tailwindcss";', {
- async loadStylesheet(id, base) {
- const file = id === 'tailwindcss' ? 'index.css' : id.replace('tailwindcss/', '')
- return {
- base: base || tailwindRoot.pathname,
- content: await readFile(new URL(file, tailwindRoot), 'utf8')
- }
- }
- })
- return compiler.build(classes)
-}
-
const cardDocument: DesignDocument = {
type: 'document',
children: [
@@ -169,7 +152,7 @@ describe('@open-pencil/dom-css conversion', () => {
]
const document = await runtime.computeStyles(
runtime.parseHTML(`<article class="${classes.join(' ')}"><h1>OpenPencil</h1></article>`),
- await compileTailwindClasses(classes)
+ await compileTailwindCSS(classes)
)
const graph = designDocumentToSceneGraph(document)
const page = graph.getPages()[0]
diff --git a/tests/engine/dom-css/tailwind.test.ts b/tests/engine/dom-css/tailwind.test.ts
new file mode 100644
index 000000000..4ad33af11
--- /dev/null
+++ b/tests/engine/dom-css/tailwind.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from 'bun:test'
+
+import { compileTailwindCSS, createHeadlessCSSRuntime } from '@open-pencil/dom-css'
+
+describe('@open-pencil/dom-css Tailwind', () => {
+ it('compiles utility candidates through Tailwind', async () => {
+ const css = await compileTailwindCSS('flex w-80 p-6 rounded-xl bg-white')
+
+ expect(css).toContain('.flex')
+ expect(css).toContain('.w-80')
+ expect(css).toContain('.p-6')
+ expect(css).toContain('--spacing')
+ })
+
+ it('feeds Tailwind CSS variables through headless style computation', async () => {
+ const runtime = createHeadlessCSSRuntime()
+ const css = await compileTailwindCSS(['w-80', 'p-6'])
+ const document = await runtime.computeStyles(
+ runtime.parseHTML('<section class="w-80 p-6"></section>'),
+ css
+ )
+ const element = document.children[0]
+
+ expect(element?.type).toBe('element')
+ if (element?.type !== 'element') return
+ expect(element.computedStyle?.width).toBe('320px')
+ expect(element.computedStyle?.['padding-top']).toBe('24px')
+ })
+})