Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Add utility to generate css text from data (#1021)

Bogdan Chadkin5c9014873914
diff --git a/apps/designer/app/routes/s/css.ts b/apps/designer/app/routes/s/css.ts
index 9b1371176e..ac6b838eb3 100644
--- a/apps/designer/app/routes/s/css.ts
+++ b/apps/designer/app/routes/s/css.ts
@@ -1,9 +1,11 @@
import { type ActionArgs, json } from "@remix-run/node";
+import { generateCssText } from "@webstudio-is/project";
+import { db } from "@webstudio-is/project/server";
import env from "~/env/env.public.server";
import { getBuildParams } from "~/shared/router-utils";
import { sentryException } from "~/shared/sentry";
-import { generateCssText } from "~/shared/css-utils";
import { createContext } from "~/shared/context.server";
+import { loadCanvasData } from "~/shared/db";
export const loader = async ({ request }: ActionArgs) => {
try {
@@ -14,7 +16,29 @@ export const loader = async ({ request }: ActionArgs) => {
throw json("Required project info", { status: 400 });
}
- const cssText = await generateCssText(buildParams, context);
+ const project = await db.project.loadByParams(buildParams, context);
+ if (project === null) {
+ throw json("Project not found", { status: 404 });
+ }
+
+ const canvasData = await loadCanvasData(
+ {
+ project,
+ env: buildParams.mode === "published" ? "prod" : "dev",
+ pageIdOrPath:
+ "pageId" in buildParams ? buildParams.pageId : buildParams.pagePath,
+ },
+ context
+ );
+ if (canvasData === undefined) {
+ throw json("Page not found", { status: 404 });
+ }
+
+ const cssText = generateCssText({
+ assets: canvasData.assets,
+ build: canvasData.build ?? undefined,
+ tree: canvasData.tree ?? undefined,
+ });
return new Response(cssText, {
headers: {
diff --git a/apps/designer/app/shared/css-utils/generate-css-text.ts b/apps/designer/app/shared/css-utils/generate-css-text.ts
deleted file mode 100644
index bd43430698..0000000000
--- a/apps/designer/app/shared/css-utils/generate-css-text.ts
+++ /dev/null
@@ -1,73 +0,0 @@
-import { json } from "@remix-run/node";
-import { db } from "@webstudio-is/project/server";
-import { addGlobalRules, getStyleRules } from "@webstudio-is/project";
-import { createCssEngine } from "@webstudio-is/css-engine";
-import {
- getComponentMeta,
- getComponentNames,
- idAttribute,
-} from "@webstudio-is/react-sdk";
-import type { AppContext } from "@webstudio-is/trpc-interface/server";
-import type { Asset } from "@webstudio-is/asset-uploader";
-import { loadCanvasData } from "~/shared/db";
-import type { BuildParams } from "../router-utils";
-
-export const generateCssText = async (
- buildParams: BuildParams,
- context: AppContext
-) => {
- const project = await db.project.loadByParams(buildParams, context);
-
- if (project === null) {
- throw json("Project not found", { status: 404 });
- }
-
- const canvasData = await loadCanvasData(
- {
- project,
- env: buildParams.mode === "published" ? "prod" : "dev",
- pageIdOrPath:
- "pageId" in buildParams ? buildParams.pageId : buildParams.pagePath,
- },
- context
- );
-
- if (canvasData === undefined) {
- throw json("Page not found", { status: 404 });
- }
-
- const engine = createCssEngine({ name: "ssr" });
-
- const assets = new Map<Asset["id"], Asset>(
- canvasData.assets.map((asset) => [asset.id, asset])
- );
- addGlobalRules(engine, { assets });
-
- for (const breakpoint of canvasData.build?.breakpoints ?? []) {
- engine.addMediaRule(breakpoint.id, breakpoint);
- }
-
- for (const component of getComponentNames()) {
- const meta = getComponentMeta(component);
- const presetStyle = meta?.presetStyle;
- if (presetStyle !== undefined) {
- engine.addStyleRule(`[data-ws-component=${component}]`, {
- style: presetStyle,
- });
- }
- }
-
- const styleRules = getStyleRules(
- new Map(canvasData.build?.styles),
- new Map(canvasData.tree?.styleSourceSelections)
- );
- for (const { breakpointId, instanceId, style } of styleRules) {
- engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, {
- breakpoint: breakpointId,
- style,
- });
- }
-
- const { cssText } = engine;
- return cssText;
-};
diff --git a/apps/designer/app/shared/css-utils/index.ts b/apps/designer/app/shared/css-utils/index.ts
deleted file mode 100644
index b8234bcfae..0000000000
--- a/apps/designer/app/shared/css-utils/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./generate-css-text";
diff --git a/packages/project/src/shared/styles/css.ts b/packages/project/src/shared/styles/css.ts
new file mode 100644
index 0000000000..db7286cba9
--- /dev/null
+++ b/packages/project/src/shared/styles/css.ts
@@ -0,0 +1,54 @@
+import { createCssEngine } from "@webstudio-is/css-engine";
+import type { Asset } from "@webstudio-is/asset-uploader";
+import type { Tree } from "@webstudio-is/project-build";
+import {
+ getComponentMeta,
+ getComponentNames,
+ idAttribute,
+} from "@webstudio-is/react-sdk";
+import type { Build } from "../schema";
+import { addGlobalRules } from "./global-rules";
+import { getStyleRules } from "./style-rules";
+
+type Data = {
+ assets: Asset[];
+ build?: Build;
+ tree?: Tree;
+};
+
+export const generateCssText = (data: Data) => {
+ const assets = new Map<Asset["id"], Asset>(
+ data.assets.map((asset) => [asset.id, asset])
+ );
+ const breakpoints = data.build?.breakpoints ?? [];
+ const styles = new Map(data.build?.styles);
+ const styleSourceSelections = new Map(data.tree?.styleSourceSelections);
+
+ const engine = createCssEngine({ name: "ssr" });
+
+ addGlobalRules(engine, { assets });
+
+ for (const breakpoint of breakpoints) {
+ engine.addMediaRule(breakpoint.id, breakpoint);
+ }
+
+ for (const component of getComponentNames()) {
+ const meta = getComponentMeta(component);
+ const presetStyle = meta?.presetStyle;
+ if (presetStyle !== undefined) {
+ engine.addStyleRule(`[data-ws-component=${component}]`, {
+ style: presetStyle,
+ });
+ }
+ }
+
+ const styleRules = getStyleRules(styles, styleSourceSelections);
+ for (const { breakpointId, instanceId, style } of styleRules) {
+ engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, {
+ breakpoint: breakpointId,
+ style,
+ });
+ }
+
+ return engine.cssText;
+};
diff --git a/packages/project/src/shared/styles/index.ts b/packages/project/src/shared/styles/index.ts
index 1964a1fa6f..24b800043a 100644
--- a/packages/project/src/shared/styles/index.ts
+++ b/packages/project/src/shared/styles/index.ts
@@ -1,2 +1,3 @@
export * from "./global-rules";
export * from "./style-rules";
+export * from "./css";