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.

feat: move css engine loader to shared folder

Andras Bacsai2e39bbfd27bd
diff --git a/apps/designer/app/routes/s/css.ts b/apps/designer/app/routes/s/css.ts
index 0bcd837737..fefa0244e1 100644
--- a/apps/designer/app/routes/s/css.ts
+++ b/apps/designer/app/routes/s/css.ts
@@ -1,51 +1,19 @@
import { ActionFunction, json } from "@remix-run/node";
-import { db } from "@webstudio-is/project/server";
-import { utils } from "@webstudio-is/project";
-import { loadCanvasData } from "~/shared/db";
-import { getBuildParams } from "~/shared/router-utils";
+
+import { BuildParams, getBuildParams } from "~/shared/router-utils";
import env from "~/env.server";
import { sentryException } from "~/shared/sentry";
-import { createCssEngine } from "@webstudio-is/css-engine";
-import { idAttribute } from "@webstudio-is/react-sdk";
-import { addGlobalRules } from "~/canvas/shared/styles";
+import { generateCssText } from "~/shared/css-utils";
export const loader: ActionFunction = async ({ request }) => {
try {
- const buildParams = getBuildParams(request);
+ const buildParams: BuildParams = getBuildParams(request);
if (buildParams === undefined) {
throw json("Required project info", { status: 404 });
}
- const project = await db.project.loadByParams(buildParams);
-
- if (project === null) {
- throw json("Project not found", { status: 404 });
- }
-
- const canvasData = await loadCanvasData(
- project,
- buildParams.mode === "published" ? "prod" : "dev",
- buildParams.pageId
- );
-
- if (canvasData === undefined) {
- throw json("Page not found", { status: 404 });
- }
-
- const engine = createCssEngine();
-
- addGlobalRules(engine, canvasData);
-
- for (const breakpoint of canvasData.breakpoints) {
- engine.addMediaRule(breakpoint.id, breakpoint);
- }
-
- const cssRules = utils.tree.getCssRules(canvasData.tree?.root);
- for (const [instanceId, cssRule] of cssRules) {
- engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, cssRule);
- }
- const { cssText } = engine;
+ const cssText = await generateCssText(buildParams);
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
new file mode 100644
index 0000000000..36c3c88a07
--- /dev/null
+++ b/apps/designer/app/shared/css-utils/generate-css-text.ts
@@ -0,0 +1,44 @@
+import { json } from "@remix-run/node";
+import { db } from "@webstudio-is/project/server";
+import { utils } from "@webstudio-is/project";
+import { loadCanvasData } from "~/shared/db";
+import { createCssEngine } from "@webstudio-is/css-engine";
+import { idAttribute } from "@webstudio-is/react-sdk";
+import { addGlobalRules } from "~/canvas/shared/styles";
+import { BuildParams } from "../router-utils";
+
+export const generateCssText = async (buildParams: BuildParams) => {
+ if (buildParams === undefined) {
+ throw json("Required project info", { status: 404 });
+ }
+ const project = await db.project.loadByParams(buildParams);
+
+ if (project === null) {
+ throw json("Project not found", { status: 404 });
+ }
+
+ const canvasData = await loadCanvasData(
+ project,
+ buildParams.mode === "published" ? "prod" : "dev",
+ buildParams.pageId
+ );
+
+ if (canvasData === undefined) {
+ throw json("Page not found", { status: 404 });
+ }
+
+ const engine = createCssEngine();
+
+ addGlobalRules(engine, canvasData);
+
+ for (const breakpoint of canvasData.breakpoints) {
+ engine.addMediaRule(breakpoint.id, breakpoint);
+ }
+
+ const cssRules = utils.tree.getCssRules(canvasData.tree?.root);
+ for (const [instanceId, cssRule] of cssRules) {
+ engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, cssRule);
+ }
+ 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
index 5cab20bf12..53f6803340 100644
--- a/apps/designer/app/shared/css-utils/index.ts
+++ b/apps/designer/app/shared/css-utils/index.ts
@@ -1 +1,2 @@
export * from "./delete-css-rules-by-breakpoint";
+export * from "./generate-css-text";