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.

refactor: replace constants in templates (#3770)

Bogdan Chadkina10c9d16b34c
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
index 1f27ce9be8..b303d23e7b 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[another-page]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
index 4987ac6bde..f6cc1d7435 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/_index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
index a8a339ab5b..324cfc402f 100644
--- a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[script-test]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
index 007a29b4c3..9bb5e05977 100644
--- a/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
+import { renderToString } from "react-dom/server";
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk";
import { Page } from "../__generated__/[sitemap.xml]._index";
@@ -8,7 +9,6 @@ import {
getRemixParams,
} from "../__generated__/[sitemap.xml]._index.server";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
-import { renderToString } from "react-dom/server";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
index 0aec9c9eb2..0fb7b111b4 100644
--- a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[world]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-custom-template/app/routes/_index.tsx b/fixtures/webstudio-custom-template/app/routes/_index.tsx
index 4987ac6bde..f6cc1d7435 100644
--- a/fixtures/webstudio-custom-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/_index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/_index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
index 1f27ce9be8..b303d23e7b 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[another-page]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
index 4987ac6bde..f6cc1d7435 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/_index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
index 1f27ce9be8..b303d23e7b 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[another-page]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
index 4987ac6bde..f6cc1d7435 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/_index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
index 3719c7868a..877b51c99a 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[_route_with_symbols_]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
index f9d4ee6ab0..e9c9589e32 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[form]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
index b6dbae013b..0fc091f607 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[heading-with-id]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
index be58b73e09..3455d84d54 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[nested].[nested-page]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index 35829b7683..92d6ef4994 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[radix]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
index 035fbff60f..da681e1663 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/[resources]._index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index 4987ac6bde..f6cc1d7435 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "../__generated__/_index.server";
-
-import css from "../__generated__/index.css?url";
import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
+import css from "../__generated__/index.css?url";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index cd7d83a170..826eb401f2 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -196,6 +196,10 @@ const copyTemplates = async (template: string = "defaults") => {
}
};
+const importFrom = (importee: string, importer: string) => {
+ return relative(dirname(importer), importee).replaceAll("\\", "/");
+};
+
export const prebuild = async (options: {
/**
* Use preview (opensource) version of the project
@@ -691,21 +695,29 @@ export const prebuild = async (options: {
const getTemplates =
documentType === "html" ? framework.html : framework.xml;
for (const { file, template } of getTemplates({ pagePath })) {
- const base = relative(dirname(file), generatedDir).replaceAll("\\", "/");
const content = template
- .replaceAll("__CLIENT__", `${base}/${generatedBasename}`)
- .replaceAll("__SERVER__", `${base}/${generatedBasename}.server`)
- .replaceAll("__CSS__", `${base}/index.css`);
+ .replaceAll("__CONSTANTS__", importFrom("./app/constants.mjs", file))
+ .replaceAll(
+ "__CLIENT__",
+ importFrom(`./app/__generated__/${generatedBasename}`, file)
+ )
+ .replaceAll(
+ "__SERVER__",
+ importFrom(`./app/__generated__/${generatedBasename}.server`, file)
+ )
+ .replaceAll(
+ "__CSS__",
+ importFrom(`./app/__generated__/index.css`, file)
+ );
await createFileIfNotExists(file, content);
}
}
// MARK: - Default sitemap.xml
for (const { file, template } of framework.defaultSitemap()) {
- const base = relative(dirname(file), generatedDir).replaceAll("\\", "/");
const content = template.replaceAll(
"__SITEMAP__",
- `${base}/$resources.sitemap.xml`
+ importFrom(`./app/__generated__/$resources.sitemap.xml`, file)
);
await createFileIfNotExists(file, content);
}
@@ -736,13 +748,9 @@ export const prebuild = async (options: {
for (const { file, template } of framework.redirect({
pagePath: redirect.old,
})) {
- const base = relative(dirname(file), generatedDir).replaceAll(
- "\\",
- "/"
- );
const content = template.replaceAll(
"__REDIRECT__",
- `${base}/${generatedBasename}`
+ importFrom(`./app/__generated__/${generatedBasename}`, file)
);
await createFileIfNotExists(file, content);
}
diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsx
index 3670483745..da704fbce3 100644
--- a/packages/cli/templates/defaults/app/route-templates/html.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx
@@ -31,9 +31,8 @@ import {
projectId,
contactEmail,
} from "__SERVER__";
-
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "__CONSTANTS__";
import css from "__CSS__?url";
-import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/packages/cli/templates/defaults/app/route-templates/xml.tsx b/packages/cli/templates/defaults/app/route-templates/xml.tsx
index 9be86c2dd6..0e86057f23 100644
--- a/packages/cli/templates/defaults/app/route-templates/xml.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/xml.tsx
@@ -1,10 +1,10 @@
/* eslint-disable camelcase */
+import { renderToString } from "react-dom/server";
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk";
import { Page } from "__CLIENT__";
import { loadResources, getPageMeta, getRemixParams } from "__SERVER__";
-import { assetBaseUrl, imageBaseUrl, imageLoader } from "../constants.mjs";
-import { renderToString } from "react-dom/server";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "__CONSTANTS__";
export const loader = async (arg: LoaderFunctionArgs) => {
const url = new URL(arg.request.url);
diff --git a/packages/react-sdk/placeholder.d.ts b/packages/react-sdk/placeholder.d.ts
index 3cd2b9a264..ff24f7d190 100644
--- a/packages/react-sdk/placeholder.d.ts
+++ b/packages/react-sdk/placeholder.d.ts
@@ -1,3 +1,10 @@
+declare module "__CONSTANTS__" {
+ import type { ImageLoader } from "@webstudio-is/image";
+ export const assetBaseUrl: string;
+ export const imageBaseUrl: string;
+ export const imageLoader: ImageLoader;
+}
+
declare module "__CLIENT__" {
import type { FontAsset, ImageAsset, System } from "@webstudio-is/sdk";