From eddb824ca72e230f5f54d1d4d588a5354028a1ac Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 18 Nov 2026 14:55:42 +0000 Subject: [PATCH] :bug: Resolve UI CSS copy paths during bundle builds Resolve the UI CSS source and destination from the package directory so Vite works when launched outside that directory. Add a regression test and quiet the bundle's rsync output. AI-assisted-by: gpt-6-luna --- frontend/packages/ui/README.md | 1 + frontend/packages/ui/package.json | 3 +- .../packages/ui/tests/copy-css-plugin.test.ts | 49 +++++++++++++++++++ frontend/packages/ui/vite.config.mts | 19 ++++--- frontend/scripts/build | 7 ++- 5 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 frontend/packages/ui/tests/copy-css-plugin.test.ts diff --git a/frontend/packages/ui/README.md b/frontend/packages/ui/README.md index ef431d1b50..a4e58ca410 100644 --- a/frontend/packages/ui/README.md +++ b/frontend/packages/ui/README.md @@ -9,3 +9,4 @@ Run from workspace root: - **`pnpm storybook:ui`** - Start Storybook for component development - **`pnpm build:ui`** - Build the library for production - **`pnpm start:ui`** - Build in watch mode for development +- **`pnpm --dir packages/ui test`** - Run the package tests diff --git a/frontend/packages/ui/package.json b/frontend/packages/ui/package.json index 1726ff6d1f..7c1d7767f8 100644 --- a/frontend/packages/ui/package.json +++ b/frontend/packages/ui/package.json @@ -20,7 +20,8 @@ }, "scripts": { "watch": "vite build --watch", - "build": "vite build" + "build": "vite build", + "test": "vitest run --config vite.config.mts" }, "devDependencies": { "@babel/core": "^8.0.6", diff --git a/frontend/packages/ui/tests/copy-css-plugin.test.ts b/frontend/packages/ui/tests/copy-css-plugin.test.ts new file mode 100644 index 0000000000..dbbd8c5187 --- /dev/null +++ b/frontend/packages/ui/tests/copy-css-plugin.test.ts @@ -0,0 +1,49 @@ +import { + mkdtempSync, + mkdirSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, expect, it } from "vitest"; +import { copyCssPlugin } from "../vite.config.mts"; + +const temporaryDirectories: string[] = []; + +afterEach(() => { + for (const directory of temporaryDirectories.splice(0)) { + rmSync(directory, { recursive: true, force: true }); + } +}); + +it("copies the generated CSS using paths based on the package directory", () => { + const temporaryDirectory = mkdtempSync( + path.join(os.tmpdir(), "penpot-ui-css-copy-"), + ); + temporaryDirectories.push(temporaryDirectory); + + const packageDirectory = path.join( + temporaryDirectory, + "frontend", + "packages", + "ui", + ); + const cssPath = path.join(packageDirectory, "dist", "ui.css"); + const destinationPath = path.join( + temporaryDirectory, + "frontend", + "resources", + "public", + "css", + "ui.css", + ); + + mkdirSync(path.dirname(cssPath), { recursive: true }); + writeFileSync(cssPath, ".ui { color: red; }"); + + copyCssPlugin(packageDirectory).closeBundle(); + + expect(readFileSync(destinationPath, "utf8")).toBe(".ui { color: red; }"); +}); diff --git a/frontend/packages/ui/vite.config.mts b/frontend/packages/ui/vite.config.mts index 9519e70987..ed2fc91c48 100644 --- a/frontend/packages/ui/vite.config.mts +++ b/frontend/packages/ui/vite.config.mts @@ -2,19 +2,22 @@ import { defineConfig, esmExternalRequirePlugin } from "vite"; import react from "@vitejs/plugin-react"; import dts from "vite-plugin-dts"; -import * as path from "path"; -import { copyFileSync } from "node:fs"; +import * as path from "node:path"; +import { copyFileSync, mkdirSync } from "node:fs"; const externalDeps = ["react", "react-dom", "react/jsx-runtime"]; -const copyCssPlugin = () => ({ +export const copyCssPlugin = (packageDir = import.meta.dirname) => ({ name: "copy-css", closeBundle: () => { - try { - copyFileSync("dist/ui.css", "../../resources/public/css/ui.css"); - } catch (e) { - console.log("Error copying css file", e); - } + const cssPath = path.resolve(packageDir, "dist/ui.css"); + const destinationPath = path.resolve( + packageDir, + "../../resources/public/css/ui.css", + ); + + mkdirSync(path.dirname(destinationPath), { recursive: true }); + copyFileSync(cssPath, destinationPath); }, }); diff --git a/frontend/scripts/build b/frontend/scripts/build index 379b730ed7..5164061129 100755 --- a/frontend/scripts/build +++ b/frontend/scripts/build @@ -55,13 +55,12 @@ pnpm run build:app:assets; sed -i "s/\.\/render.js/.\/render.js?version=$VERSION_TAG/g" resources/public/js/worker/main*.js -rsync -avr resources/public/ target/dist/ +rsync -ar resources/public/ target/dist/ # Include all plugins on the bundle -rsync -avr ../plugins/dist/apps/ target/dist/plugins/; +rsync -ar ../plugins/dist/apps/ target/dist/plugins/; # Include the MCP plugin on the bundle mkdir -p target/dist/plugins/mcp; -rsync -avr ../mcp/packages/plugin/dist/ target/dist/plugins/mcp/; - +rsync -ar ../mcp/packages/plugin/dist/ target/dist/plugins/mcp/;