🐛 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
This commit is contained in:
Andrey Antukh 2026-11-18 14:55:42 +00:00
parent 8e575894f1
commit eddb824ca7
5 changed files with 66 additions and 13 deletions

View File

@ -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

View File

@ -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",

View File

@ -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; }");
});

View File

@ -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);
},
});

View File

@ -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/;