Skip to content

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

fix(dom-css): keep browser entrypoint free of headless CSS

Danila Poyarkov4293ad1a8702
diff --git a/packages/dom-css/src/browser.ts b/packages/dom-css/src/browser.ts
index f70d2c65a..90d31f5f4 100644
--- a/packages/dom-css/src/browser.ts
+++ b/packages/dom-css/src/browser.ts
@@ -1,15 +1,15 @@
import type { SceneGraph } from '@open-pencil/scene-graph'
-import { jsxToDesignDocument, type JSXChild } from './jsx/runtime'
-
-export { Fragment, jsx, jsxs } from './jsx/runtime'
-export type { JSXChild, JSXElementProps, JSXStyleInput, JSXTag } from './jsx/runtime'
import { mergeCSSText } from './css-text'
+import { jsxToDesignDocumentCore, type JSXChild } from './jsx/core'
import { createBrowserCSSRuntime, type BrowserCSSRuntimeOptions } from './runtime/browser'
-import { compileTailwindCSS, type CompileTailwindCSSOptions } from './tailwind'
+import type { CompileTailwindCSSOptions } from './tailwind'
import { designDocumentToSceneGraph, type ToSceneGraphOptions } from './to-scene-graph'
import type { CSSComputeOptions, DesignDocument } from './types'
+export { Fragment, jsx, jsxs } from './jsx/core'
+export type { JSXChild, JSXElementProps, JSXStyleInput, JSXTag } from './jsx/core'
+
export interface BrowserToDesignDocumentOptions extends BrowserCSSRuntimeOptions {
cssText?: string
compute?: CSSComputeOptions
@@ -39,6 +39,14 @@ function createRuntime(options: BrowserCSSRuntimeOptions) {
return createBrowserCSSRuntime({ sandbox: 'iframe', ...options })
}
+async function compileBrowserTailwindCSS(
+ candidates: string | Iterable<string>,
+ options: CompileTailwindCSSOptions
+): Promise<string> {
+ const { compileTailwindCSS } = await import('./tailwind')
+ return compileTailwindCSS(candidates, options)
+}
+
function resolveBrowserDocument(documentOverride: Document | undefined): Document {
if (documentOverride) return documentOverride
if (typeof document === 'undefined') {
@@ -81,7 +89,7 @@ export async function browserTailwindHTMLToDesignDocument(
candidates: string | Iterable<string>,
options: BrowserTailwindHTMLToDesignDocumentOptions = {}
): Promise<DesignDocument> {
- const cssText = await compileTailwindCSS(candidates, options)
+ const cssText = await compileBrowserTailwindCSS(candidates, options)
return browserHTMLToDesignDocument(html, { ...options, cssText })
}
@@ -98,7 +106,7 @@ export async function browserJSXToDesignDocument(
input: JSXChild,
options: BrowserToDesignDocumentOptions = {}
): Promise<DesignDocument> {
- const document = await jsxToDesignDocument(input)
+ const document = jsxToDesignDocumentCore(input)
const runtime = createRuntime(options)
return runtime.computeStyles(document, options.cssText, options.compute)
}
@@ -116,7 +124,7 @@ export async function browserTailwindJSXToDesignDocument(
candidates: string | Iterable<string>,
options: BrowserTailwindToDesignDocumentOptions = {}
): Promise<DesignDocument> {
- const cssText = await compileTailwindCSS(candidates, options)
+ const cssText = await compileBrowserTailwindCSS(candidates, options)
return browserJSXToDesignDocument(input, { ...options, cssText })
}
diff --git a/packages/dom-css/src/jsx/core.ts b/packages/dom-css/src/jsx/core.ts
new file mode 100644
index 000000000..19560b40e
--- /dev/null
+++ b/packages/dom-css/src/jsx/core.ts
@@ -0,0 +1,130 @@
+import type { DesignDocument, DesignNode, DesignStyleDeclaration } from '../types'
+
+export const Fragment = Symbol.for('open-pencil.dom-css.fragment')
+
+type JSXComponent = (props: JSXElementProps) => JSXChild
+export type JSXTag = string | JSXComponent | typeof Fragment
+export type JSXStyleValue = string | number | null | undefined
+export type JSXStyleObject = Record<string, JSXStyleValue>
+export type JSXStyleInput = string | JSXStyleObject
+export type JSXChild = DesignNode | JSXChild[] | string | number | boolean | null | undefined
+
+export interface JSXElementProps {
+ children?: JSXChild
+ class?: string
+ className?: string
+ style?: JSXStyleInput
+ key?: string | number
+ [name: string]: unknown
+}
+
+function cssPropertyName(name: string) {
+ if (name.startsWith('--')) return name
+ return name.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`)
+}
+
+function styleObjectToDeclaration(style: JSXStyleObject): DesignStyleDeclaration | undefined {
+ const entries = Object.entries(style)
+ .filter(
+ (entry): entry is [string, string | number] => entry[1] !== null && entry[1] !== undefined
+ )
+ .map(([property, value]) => [cssPropertyName(property), String(value)] as const)
+ .filter(([, value]) => value.length > 0)
+
+ if (entries.length === 0) return undefined
+ return Object.fromEntries(entries)
+}
+
+function styleStringToDeclaration(style: string): DesignStyleDeclaration | undefined {
+ const entries = style
+ .split(';')
+ .map((declaration) => declaration.split(':'))
+ .filter((parts): parts is [string, string, ...string[]] => parts.length >= 2)
+ .map(([property, ...value]) => [property.trim().toLowerCase(), value.join(':').trim()] as const)
+ .filter(([property, value]) => property.length > 0 && value.length > 0)
+
+ if (entries.length === 0) return undefined
+ return Object.fromEntries(entries)
+}
+
+function styleToDeclaration(style: JSXStyleInput | undefined): DesignStyleDeclaration | undefined {
+ if (typeof style === 'string') return styleStringToDeclaration(style)
+ if (style) return styleObjectToDeclaration(style)
+ return undefined
+}
+
+function attributeValue(value: unknown): string | undefined {
+ if (value === null || value === undefined || value === false) return undefined
+ if (value === true) return ''
+ if (typeof value === 'string') return value
+ if (typeof value === 'number' || typeof value === 'bigint') return String(value)
+ return undefined
+}
+
+function propsToAttrs(props: JSXElementProps): Record<string, string> {
+ const attrs: Record<string, string> = {}
+ const classValue = props.class ?? props.className
+ if (classValue) attrs.class = classValue
+
+ for (const [name, value] of Object.entries(props)) {
+ if (name === 'children' || name === 'class' || name === 'className' || name === 'style') {
+ continue
+ }
+ if (name === 'key' || name.startsWith('on')) continue
+ const attr = attributeValue(value)
+ if (attr !== undefined) attrs[name] = attr
+ }
+
+ return attrs
+}
+
+function normalizeChild(child: JSXChild, nodes: DesignNode[]): void {
+ if (child === null || child === undefined || typeof child === 'boolean') return
+ if (Array.isArray(child)) {
+ for (const item of child) normalizeChild(item, nodes)
+ return
+ }
+ if (typeof child === 'string' || typeof child === 'number') {
+ nodes.push({ type: 'text', text: String(child) })
+ return
+ }
+ nodes.push(child)
+}
+
+export function normalizeJSXChildren(children: JSXChild): DesignNode[] {
+ const nodes: DesignNode[] = []
+ normalizeChild(children, nodes)
+ return nodes
+}
+
+export function jsx(tag: JSXTag, props: JSXElementProps = {}): DesignNode | DesignNode[] {
+ if (tag === Fragment) return normalizeJSXChildren(props.children)
+ if (typeof tag === 'function') return normalizeJSXChildren(tag(props))
+
+ return {
+ type: 'element',
+ tagName: tag,
+ attrs: propsToAttrs(props),
+ inlineStyle: styleToDeclaration(props.style),
+ children: normalizeJSXChildren(props.children)
+ }
+}
+
+export const jsxs = jsx
+
+export function jsxToDesignDocumentCore(input: JSXChild): DesignDocument {
+ return {
+ type: 'document',
+ children: normalizeJSXChildren(input)
+ }
+}
+
+export namespace JSX {
+ export type Element = DesignNode | DesignNode[]
+ export interface ElementChildrenAttribute {
+ children: unknown
+ }
+ export interface IntrinsicElements {
+ [tagName: string]: JSXElementProps
+ }
+}
diff --git a/packages/dom-css/src/jsx/runtime.ts b/packages/dom-css/src/jsx/runtime.ts
index dc222f10d..ad85034a7 100644
--- a/packages/dom-css/src/jsx/runtime.ts
+++ b/packages/dom-css/src/jsx/runtime.ts
@@ -2,31 +2,18 @@ import type { SceneGraph } from '@open-pencil/scene-graph'
import { compileTailwindCSS, type CompileTailwindCSSOptions } from '../tailwind'
import { designDocumentToSceneGraph, type ToSceneGraphOptions } from '../to-scene-graph'
-import type {
- CSSComputeOptions,
- CSSRuntime,
- DesignDocument,
- DesignNode,
- DesignStyleDeclaration
-} from '../types'
-
-export const Fragment = Symbol.for('open-pencil.dom-css.fragment')
-
-type JSXComponent = (props: JSXElementProps) => JSXChild
-export type JSXTag = string | JSXComponent | typeof Fragment
-export type JSXStyleValue = string | number | null | undefined
-export type JSXStyleObject = Record<string, JSXStyleValue>
-export type JSXStyleInput = string | JSXStyleObject
-export type JSXChild = DesignNode | JSXChild[] | string | number | boolean | null | undefined
-
-export interface JSXElementProps {
- children?: JSXChild
- class?: string
- className?: string
- style?: JSXStyleInput
- key?: string | number
- [name: string]: unknown
-}
+import type { CSSComputeOptions, CSSRuntime, DesignDocument } from '../types'
+import { jsxToDesignDocumentCore, type JSXChild } from './core'
+
+export { Fragment, jsx, jsxs } from './core'
+export type {
+ JSXChild,
+ JSXElementProps,
+ JSXStyleInput,
+ JSXStyleObject,
+ JSXStyleValue,
+ JSXTag
+} from './core'
export interface JSXToDesignDocumentOptions {
cssText?: string
@@ -48,107 +35,11 @@ async function runtimeForOptions(runtime: CSSRuntime | undefined) {
return createCSSRuntime()
}
-function cssPropertyName(name: string) {
- if (name.startsWith('--')) return name
- return name.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`)
-}
-
-function styleObjectToDeclaration(style: JSXStyleObject): DesignStyleDeclaration | undefined {
- const entries = Object.entries(style)
- .filter(
- (entry): entry is [string, string | number] => entry[1] !== null && entry[1] !== undefined
- )
- .map(([property, value]) => [cssPropertyName(property), String(value)] as const)
- .filter(([, value]) => value.length > 0)
-
- if (entries.length === 0) return undefined
- return Object.fromEntries(entries)
-}
-
-function styleStringToDeclaration(style: string): DesignStyleDeclaration | undefined {
- const entries = style
- .split(';')
- .map((declaration) => declaration.split(':'))
- .filter((parts): parts is [string, string, ...string[]] => parts.length >= 2)
- .map(([property, ...value]) => [property.trim().toLowerCase(), value.join(':').trim()] as const)
- .filter(([property, value]) => property.length > 0 && value.length > 0)
-
- if (entries.length === 0) return undefined
- return Object.fromEntries(entries)
-}
-
-function styleToDeclaration(style: JSXStyleInput | undefined): DesignStyleDeclaration | undefined {
- if (typeof style === 'string') return styleStringToDeclaration(style)
- if (style) return styleObjectToDeclaration(style)
- return undefined
-}
-
-function attributeValue(value: unknown): string | undefined {
- if (value === null || value === undefined || value === false) return undefined
- if (value === true) return ''
- if (typeof value === 'string') return value
- if (typeof value === 'number' || typeof value === 'bigint') return String(value)
- return undefined
-}
-
-function propsToAttrs(props: JSXElementProps): Record<string, string> {
- const attrs: Record<string, string> = {}
- const classValue = props.class ?? props.className
- if (classValue) attrs.class = classValue
-
- for (const [name, value] of Object.entries(props)) {
- if (name === 'children' || name === 'class' || name === 'className' || name === 'style')
- continue
- if (name === 'key' || name.startsWith('on')) continue
- const attr = attributeValue(value)
- if (attr !== undefined) attrs[name] = attr
- }
-
- return attrs
-}
-
-function normalizeChild(child: JSXChild, nodes: DesignNode[]): void {
- if (child === null || child === undefined || typeof child === 'boolean') return
- if (Array.isArray(child)) {
- for (const item of child) normalizeChild(item, nodes)
- return
- }
- if (typeof child === 'string' || typeof child === 'number') {
- nodes.push({ type: 'text', text: String(child) })
- return
- }
- nodes.push(child)
-}
-
-function normalizeChildren(children: JSXChild): DesignNode[] {
- const nodes: DesignNode[] = []
- normalizeChild(children, nodes)
- return nodes
-}
-
-export function jsx(tag: JSXTag, props: JSXElementProps = {}): DesignNode | DesignNode[] {
- if (tag === Fragment) return normalizeChildren(props.children)
- if (typeof tag === 'function') return normalizeChildren(tag(props))
-
- return {
- type: 'element',
- tagName: tag,
- attrs: propsToAttrs(props),
- inlineStyle: styleToDeclaration(props.style),
- children: normalizeChildren(props.children)
- }
-}
-
-export const jsxs = jsx
-
export async function jsxToDesignDocument(
input: JSXChild,
options: JSXToDesignDocumentOptions = {}
): Promise<DesignDocument> {
- const document: DesignDocument = {
- type: 'document',
- children: normalizeChildren(input)
- }
+ const document = jsxToDesignDocumentCore(input)
if (!options.cssText && !options.runtime && !options.compute) return document
const runtime = await runtimeForOptions(options.runtime)
@@ -181,12 +72,4 @@ export async function tailwindJSXToSceneGraph(
return designDocumentToSceneGraph(document, options)
}
-export namespace JSX {
- export type Element = DesignNode | DesignNode[]
- export interface ElementChildrenAttribute {
- children: unknown
- }
- export interface IntrinsicElements {
- [tagName: string]: JSXElementProps
- }
-}
+export type { JSX } from './core'
diff --git a/packages/dom-css/src/tailwind.ts b/packages/dom-css/src/tailwind.ts
index a071c0cb3..a2649802f 100644
--- a/packages/dom-css/src/tailwind.ts
+++ b/packages/dom-css/src/tailwind.ts
@@ -35,6 +35,6 @@ function normalizeClasses(classes: string | Iterable<string>): string[] {
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')
+ const { readFile } = await import(/* @vite-ignore */ 'node:fs/promises')
return readFile(url, 'utf8')
}
diff --git a/vite/aliases.ts b/vite/aliases.ts
index 4e64c984e..9466e8a2a 100644
--- a/vite/aliases.ts
+++ b/vite/aliases.ts
@@ -3,18 +3,49 @@ import { resolve } from 'node:path'
export function createOpenPencilAliases(rootDir: string) {
const emptyNodeModule = resolve(rootDir, 'vite/empty-node-module.ts')
- return {
- fs: emptyNodeModule,
- path: emptyNodeModule,
- '@': resolve(rootDir, 'src'),
- '#vue': resolve(rootDir, 'packages/vue/src'),
- '#core': resolve(rootDir, 'packages/core/src'),
- '#dom-css': resolve(rootDir, 'packages/dom-css/src'),
- '@open-pencil/vue': resolve(rootDir, 'packages/vue/src'),
- '@open-pencil/core': resolve(rootDir, 'packages/core/src'),
- '@open-pencil/dom-css': resolve(rootDir, 'packages/dom-css/src'),
- 'opentype.js': resolve(rootDir, 'node_modules/opentype.js/dist/opentype.module.js'),
- mermaid: resolve(rootDir, 'src/app/shell/markdown/index.ts'),
- 'beautiful-mermaid': resolve(rootDir, 'src/app/shell/markdown/index.ts')
- }
+ return [
+ { find: /^fs$/, replacement: emptyNodeModule },
+ { find: /^path$/, replacement: emptyNodeModule },
+ { find: '@', replacement: resolve(rootDir, 'src') },
+ { find: '#vue', replacement: resolve(rootDir, 'packages/vue/src') },
+ { find: '#core', replacement: resolve(rootDir, 'packages/core/src') },
+ { find: '#dom-css', replacement: resolve(rootDir, 'packages/dom-css/src') },
+ {
+ find: /^@open-pencil\/dom-css\/browser$/,
+ replacement: resolve(rootDir, 'packages/dom-css/src/browser.ts')
+ },
+ {
+ find: /^@open-pencil\/dom-css\/jsx-runtime$/,
+ replacement: resolve(rootDir, 'packages/dom-css/src/jsx/runtime.ts')
+ },
+ {
+ find: /^@open-pencil\/dom-css\/jsx-dev-runtime$/,
+ replacement: resolve(rootDir, 'packages/dom-css/src/jsx/dev-runtime.ts')
+ },
+ {
+ find: /^@open-pencil\/dom-css$/,
+ replacement: resolve(rootDir, 'packages/dom-css/src/index.ts')
+ },
+ {
+ find: /^@open-pencil\/scene-graph$/,
+ replacement: resolve(rootDir, 'packages/scene-graph/src/index.ts')
+ },
+ { find: '@open-pencil/scene-graph', replacement: resolve(rootDir, 'packages/scene-graph/src') },
+ { find: /^@open-pencil\/pen$/, replacement: resolve(rootDir, 'packages/pen/src/index.ts') },
+ { find: '@open-pencil/pen', replacement: resolve(rootDir, 'packages/pen/src') },
+ { find: /^@open-pencil\/kiwi$/, replacement: resolve(rootDir, 'packages/kiwi/src/index.ts') },
+ { find: '@open-pencil/kiwi', replacement: resolve(rootDir, 'packages/kiwi/src') },
+ { find: /^@open-pencil\/fig$/, replacement: resolve(rootDir, 'packages/fig/src/index.ts') },
+ { find: '@open-pencil/fig', replacement: resolve(rootDir, 'packages/fig/src') },
+ { find: /^@open-pencil\/vue$/, replacement: resolve(rootDir, 'packages/vue/src/index.ts') },
+ { find: '@open-pencil/vue', replacement: resolve(rootDir, 'packages/vue/src') },
+ { find: /^@open-pencil\/core$/, replacement: resolve(rootDir, 'packages/core/src/index.ts') },
+ { find: '@open-pencil/core', replacement: resolve(rootDir, 'packages/core/src') },
+ {
+ find: 'opentype.js',
+ replacement: resolve(rootDir, 'node_modules/opentype.js/dist/opentype.module.js')
+ },
+ { find: 'mermaid', replacement: resolve(rootDir, 'src/app/shell/markdown/index.ts') },
+ { find: 'beautiful-mermaid', replacement: resolve(rootDir, 'src/app/shell/markdown/index.ts') }
+ ]
}