Skip to content

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

fix(core): load fontoxpath under Node's ESM loader (#824)

MrHard02fd53a8e0ba
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dc1e9e940..baaed9dd7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -114,6 +114,7 @@
- Export layers with two shadows as one `effects` prop instead of repeating the `shadow` attribute, background blurs as `backgroundBlur` instead of a layer blur, hidden children with `visible={false}` instead of leaving them out, and per-corner radii even when the uniform radius is 0.
- Apply `strokeAlign`, `strokeDash`, `minH`, and `maxH` in design JSX, which were accepted but ignored, and make `minW` and `maxW` set the layer's minimum and maximum width rather than only clamping its initial width.
- Keep the text, visibility, or swapped component an instance sets through a component property when a page it appears on loads on its own, instead of resetting it to the component's default.
+- Run XPath queries from `openpencil query` and the MCP `query_nodes` tool under Node, which failed with `evaluateXPathToNodes is not a function` (#787).
- Render the canvas with the Vue SDK's `CanvasRoot` and `CanvasSurface`; CanvasKit never started there and the canvas stayed blank.
- Keep the view centered on what you were looking at when zooming to 100% or another fixed level, instead of jumping elsewhere whenever the zoom changes.
- Draw collaborators' names on their cursors with proper letter spacing and fallback fonts, and end long names with an ellipsis.
diff --git a/packages/core/src/xpath.ts b/packages/core/src/xpath.ts
index c267816d8..175eb2ba0 100644
--- a/packages/core/src/xpath.ts
+++ b/packages/core/src/xpath.ts
@@ -1,4 +1,4 @@
-import type { IDomFacade } from 'fontoxpath'
+import type * as FontoxpathModule from 'fontoxpath'
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
@@ -214,6 +214,17 @@ function createDomFacade(graph: SceneGraph) {
}
}
+type Fontoxpath = typeof FontoxpathModule
+
+// fontoxpath is CommonJS. Bundlers and Bun hand its exports out as named
+// exports, but Node's ESM loader exposes them only on `default`.
+async function loadFontoxpath(): Promise<Fontoxpath> {
+ const mod: Partial<Fontoxpath> & { default?: Fontoxpath } = await import('fontoxpath')
+ if (mod.evaluateXPathToNodes && mod.evaluateXPathToBoolean) return mod as Fontoxpath
+ if (mod.default) return mod.default
+ throw new Error('fontoxpath exports not found')
+}
+
export interface XPathQueryOptions {
limit?: number
page?: string
@@ -230,8 +241,8 @@ export async function queryByXPath(
if (targetPages.length === 0) return []
- const { evaluateXPathToNodes } = await import('fontoxpath')
- const domFacade = createDomFacade(graph) as IDomFacade
+ const { evaluateXPathToNodes } = await loadFontoxpath()
+ const domFacade = createDomFacade(graph) as FontoxpathModule.IDomFacade
const results: SceneNode[] = []
for (const page of targetPages) {
@@ -309,8 +320,8 @@ export async function matchByXPath(
selector: string,
node: SceneNode
): Promise<boolean> {
- const { evaluateXPathToBoolean } = await import('fontoxpath')
- const domFacade = createDomFacade(graph) as IDomFacade
+ const { evaluateXPathToBoolean } = await loadFontoxpath()
+ const domFacade = createDomFacade(graph) as FontoxpathModule.IDomFacade
const wrapped = wrapNode(graph, node)
try {
return evaluateXPathToBoolean(`self::*[${selector}]`, wrapped, domFacade)
diff --git a/tools/checks/package-quality/src/smoke/scenarios.ts b/tools/checks/package-quality/src/smoke/scenarios.ts
index 5abdf5570..9b64fb85b 100644
--- a/tools/checks/package-quality/src/smoke/scenarios.ts
+++ b/tools/checks/package-quality/src/smoke/scenarios.ts
@@ -55,5 +55,9 @@ export const runtimeScenarios: RuntimeScenario[] = [
{
name: 'DOM/CSS JSX runtime',
code: "const { jsx, jsxToDesignDocument } = await import('@open-pencil/dom-css/jsx-runtime'); const document = await jsxToDesignDocument(jsx('section', { class: 'card', style: { width: '120px' }, children: 'OpenPencil' })); const node = document.children[0]; if (node?.type !== 'element' || node.inlineStyle?.width !== '120px') throw new Error('DOM/CSS JSX runtime failed')"
+ },
+ {
+ name: 'XPath query',
+ code: "const { SceneGraph } = await import('@open-pencil/scene-graph'); const { FigmaAPI } = await import('@open-pencil/core/figma-api'); const { matchByXPath, queryByXPath } = await import('@open-pencil/core/xpath'); const graph = new SceneGraph(); new FigmaAPI(graph).createRectangle(); const found = await queryByXPath(graph, '//RECTANGLE'); if (found.length !== 1 || !(await matchByXPath(graph, '@width > 0', found[0]))) throw new Error('XPath query smoke failed')"
}
]