From 1d2418ee8583b7e07d2929d4a695e125c11d7177 Mon Sep 17 00:00:00 2001 From: Ivan Vasilov Date: Thu, 24 Apr 2025 22:26:19 +0200 Subject: [PATCH] chore: Skip building of `libpg-query` (#35255) * Don't build libpg-query package. * Use the WASM build of libpg-query in ai-commands. * Fix missing mdast type. * fix: wasm loading in ai-commands tests * fix: better polyfill * Make the sql-to-rest page to be client-side only. --------- Co-authored-by: Greg Richardson --- package.json | 3 ++- packages/ai-commands/package.json | 1 + packages/ai-commands/test/sql-util.ts | 2 +- packages/ai-commands/test/util.ts | 2 +- packages/ai-commands/tsconfig.json | 1 + packages/ai-commands/vitest.setup.ts | 24 ++++++++++++++++++++++++ packages/ui-patterns/SqlToRest/index.tsx | 2 +- pnpm-lock.yaml | 14 ++++++++------ 8 files changed, 39 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index e9d85ca7e2..6bb693c185 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,8 @@ "vinxi>esbuild": "0.25.2", "@tanstack/directive-functions-plugin>vite": "6.2.6", "@tanstack/react-start-plugin>vite": "6.2.6" - } + }, + "neverBuiltDependencies": ["libpg-query"] }, "engines": { "pnpm": ">=9", diff --git a/packages/ai-commands/package.json b/packages/ai-commands/package.json index 0a00ce7590..fdbc75ef0d 100644 --- a/packages/ai-commands/package.json +++ b/packages/ai-commands/package.json @@ -25,6 +25,7 @@ "devDependencies": { "@ai-sdk/openai": "^0.0.72", "@types/common-tags": "^1.8.4", + "@types/mdast": "^4.0.0", "@types/node": "catalog:", "api-types": "workspace:*", "chalk": "^5.3.0", diff --git a/packages/ai-commands/test/sql-util.ts b/packages/ai-commands/test/sql-util.ts index d9c5eea6c6..6338685d73 100644 --- a/packages/ai-commands/test/sql-util.ts +++ b/packages/ai-commands/test/sql-util.ts @@ -1,4 +1,4 @@ -import { A_Const, A_Expr, ColumnRef, CreatePolicyStmt, Node, parseQuery } from 'libpg-query' +import { A_Const, A_Expr, ColumnRef, CreatePolicyStmt, Node, parseQuery } from 'libpg-query/wasm' export type PolicyInfo = { name: string diff --git a/packages/ai-commands/test/util.ts b/packages/ai-commands/test/util.ts index 6a911fcc03..b73bba0834 100644 --- a/packages/ai-commands/test/util.ts +++ b/packages/ai-commands/test/util.ts @@ -1,6 +1,6 @@ import chalk from 'chalk' +import type { Code } from 'mdast' import { fromMarkdown } from 'mdast-util-from-markdown' -import type { Code } from 'mdast-util-from-markdown/lib' import { format } from 'sql-formatter' declare global { diff --git a/packages/ai-commands/tsconfig.json b/packages/ai-commands/tsconfig.json index f612f0c8a2..825ae2a5a0 100644 --- a/packages/ai-commands/tsconfig.json +++ b/packages/ai-commands/tsconfig.json @@ -3,6 +3,7 @@ "include": ["."], "exclude": ["dist", "build", "node_modules"], "compilerOptions": { + "moduleResolution": "bundler", "types": ["./test/extensions.d.ts"] } } diff --git a/packages/ai-commands/vitest.setup.ts b/packages/ai-commands/vitest.setup.ts index 1b83069042..b8e0e2e20d 100644 --- a/packages/ai-commands/vitest.setup.ts +++ b/packages/ai-commands/vitest.setup.ts @@ -9,3 +9,27 @@ if (!process.env.CI) { statSync(envPath) config({ path: envPath }) } + +// Modify fetch to support wasm file URLs +globalThis.fetch = async (input: string | URL | Request, init?: RequestInit) => { + if (input instanceof Request) { + return fetch(input, init) + } + + const url = new URL(input) + if (url.protocol === 'file:' && url.pathname.endsWith('.wasm')) { + return fetchFileNode(input, 'application/wasm') + } + + return fetch(input, init) +} + +async function fetchFileNode(input: string | URL, type: string) { + const fs = await import('node:fs') + const { Readable } = await import('node:stream') + const { fileURLToPath } = await import('node:url') + const path = fileURLToPath(input) + const nodeStream = fs.createReadStream(path) + const stream = Readable.toWeb(nodeStream) as ReadableStream + return new Response(stream, { headers: { 'Content-Type': type } }) +} diff --git a/packages/ui-patterns/SqlToRest/index.tsx b/packages/ui-patterns/SqlToRest/index.tsx index 1e83262f39..75e91e42d2 100644 --- a/packages/ui-patterns/SqlToRest/index.tsx +++ b/packages/ui-patterns/SqlToRest/index.tsx @@ -25,7 +25,7 @@ function FallbackComponent({ error }: FallbackProps) { } // Lazy load client side to prevent hydration issues when browser produces an error -const SqlToRest = dynamic(() => import('./sql-to-rest')) +const SqlToRest = dynamic(() => import('./sql-to-rest'), { ssr: false }) export default function SqlToRestWithFallback(props: SqlToRestProps) { return ( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d2afa5c9f2..480833f206 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1496,6 +1496,9 @@ importers: '@types/common-tags': specifier: ^1.8.4 version: 1.8.4 + '@types/mdast': + specifier: ^4.0.0 + version: 4.0.1 '@types/node': specifier: 'catalog:' version: 20.12.11 @@ -1613,7 +1616,7 @@ importers: dependencies: '@mertasan/tailwindcss-variables': specifier: ^2.2.3 - version: 2.7.0(autoprefixer@10.4.16(postcss@8.5.3))(postcss@8.5.3) + version: 2.7.0(autoprefixer@10.4.16(postcss@8.4.38))(postcss@8.4.38) '@radix-ui/colors': specifier: ^0.1.8 version: 0.1.9 @@ -8669,7 +8672,6 @@ packages: resolution: {integrity: sha512-t0q23FIpvHDTtnORW+bDJziGsal5uh9RJTJ1fyH8drd4lICOoXhJ5pLMUZ5C0VQei6dNmwTzzoTRgMkO9JgHEQ==} peerDependencies: eslint: '>= 5' - bundledDependencies: [] eslint-plugin-import@2.29.1: resolution: {integrity: sha512-BbPC0cuExzhiMo4Ff1BTVwHpjjv28C5R+btTOGaCRC7UEz801up0JadwkeSk5Ued6TG34uaczuVuH6qyy5YUxw==} @@ -16596,11 +16598,11 @@ snapshots: '@types/react': 18.3.3 react: 18.3.1 - '@mertasan/tailwindcss-variables@2.7.0(autoprefixer@10.4.16(postcss@8.5.3))(postcss@8.5.3)': + '@mertasan/tailwindcss-variables@2.7.0(autoprefixer@10.4.16(postcss@8.4.38))(postcss@8.4.38)': dependencies: - autoprefixer: 10.4.16(postcss@8.5.3) + autoprefixer: 10.4.16(postcss@8.4.38) lodash: 4.17.21 - postcss: 8.5.3 + postcss: 8.4.38 '@mjackson/node-fetch-server@0.2.0': {} @@ -20501,7 +20503,7 @@ snapshots: '@types/mdast@4.0.1': dependencies: - '@types/unist': 3.0.3 + '@types/unist': 2.0.8 '@types/mdx-js__react@1.5.6': dependencies: