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.

Make route-templates typeable (#2296)

Ivan Starkov4b1ed5fc7398
diff --git a/fixtures/webstudio-remix-vercel/app/constants.ts b/fixtures/webstudio-remix-vercel/app/constants.ts
new file mode 100644
index 0000000000..4ee7631403
--- /dev/null
+++ b/fixtures/webstudio-remix-vercel/app/constants.ts
@@ -0,0 +1,2 @@
+export const assetBaseUrl = "/assets/";
+export const imageBaseUrl = "/assets/";
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[blog].[page].[deep-nested-page]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[blog].[page].[deep-nested-page]._index.tsx
index 1dd14d02f1..1b34d01b6e 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[blog].[page].[deep-nested-page]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[blog].[page].[deep-nested-page]._index.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/[blog].[page].[deep-nested-page]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "/assets/" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "/assets/";
- const imageBaseUrl = "/assets/";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[blog].[page]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[blog].[page]._index.tsx
index 2258620f56..a8c035f29c 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[blog].[page]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[blog].[page]._index.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/[blog].[page]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "/assets/" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "/assets/";
- const imageBaseUrl = "/assets/";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[blog]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[blog]._index.tsx
index 4761d5fcfc..adf9f6b332 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[blog]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[blog]._index.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/[blog]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "/assets/" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "/assets/";
- const imageBaseUrl = "/assets/";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index bcbf1a5f0e..573fe6be53 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/[radix]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "/assets/" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "/assets/";
- const imageBaseUrl = "/assets/";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index a636b2902e..c19d3717db 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/_index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "/assets/" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "/assets/";
- const imageBaseUrl = "/assets/";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/packages/cli/__generated__/index.ts b/packages/cli/__generated__/index.ts
new file mode 100644
index 0000000000..3a58139072
--- /dev/null
+++ b/packages/cli/__generated__/index.ts
@@ -0,0 +1,56 @@
+/**
+ * The only intent of this file is to support typings inside ../templates/route-template for easier development.
+ **/
+import type { PageData } from "../templates/route-template";
+import type { Components } from "@webstudio-is/react-sdk";
+import type { Asset } from "@webstudio-is/sdk";
+
+export const components = new Map() as Components;
+
+export const fontAssets: Asset[] = [];
+
+export const pageData: PageData = {
+ build: {
+ props: [],
+ instances: [],
+ dataSources: [],
+ },
+ pages: [],
+
+ page: {
+ id: "",
+ name: "",
+ title: "",
+ meta: {},
+ rootInstanceId: "",
+ path: "",
+ },
+ assets: [],
+};
+
+export const user: { email: string | null } | undefined = {
+ email: "email@domain",
+};
+export const projectId = "project-id";
+
+const indexesWithinAncestors = new Map<string, number>([]);
+
+const executeComputingExpressions = (
+ variables: Map<string, unknown>
+): Map<string, unknown> => {
+ return new Map();
+};
+
+const executeEffectfulExpression = (
+ code: string,
+ args: Map<string, unknown>,
+ variables: Map<string, unknown>
+): Map<string, unknown> => {
+ return new Map();
+};
+
+export const utils = {
+ indexesWithinAncestors,
+ executeComputingExpressions,
+ executeEffectfulExpression,
+};
diff --git a/packages/cli/src/__generated__/router.ts b/packages/cli/src/__generated__/router.ts
index d4f7bd28b9..8c638be6d5 100644
--- a/packages/cli/src/__generated__/router.ts
+++ b/packages/cli/src/__generated__/router.ts
@@ -5,15 +5,13 @@ If needed to make any changes. Add them to ./templates folder and run pnpm run b
*/
-import { ASSETS_BASE } from "../config";
-
export const getRouteTemplate = () => {
return `/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -34,6 +32,7 @@ import {
} from "../__generated__/index";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -56,7 +55,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: "${ASSETS_BASE}" + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -157,9 +156,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = "${ASSETS_BASE}";
- const imageBaseUrl = "${ASSETS_BASE}";
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/packages/cli/src/__generated__/templates.ts b/packages/cli/src/__generated__/templates.ts
index 83f5307455..fb97f01c0f 100644
--- a/packages/cli/src/__generated__/templates.ts
+++ b/packages/cli/src/__generated__/templates.ts
@@ -44,6 +44,13 @@ export const templates: Record<ProjectTarget, Folder> = {
{
name: "app",
files: [
+ {
+ name: "constants.ts",
+ content:
+ 'export const assetBaseUrl = "/assets/";\nexport const imageBaseUrl = "/assets/";\n',
+ encoding: "utf-8",
+ merge: false,
+ },
{
name: "root.tsx",
content:
diff --git a/packages/cli/src/config.ts b/packages/cli/src/config.ts
index f0b97478ed..4d7a82ed3a 100644
--- a/packages/cli/src/config.ts
+++ b/packages/cli/src/config.ts
@@ -11,6 +11,7 @@ export const GLOBAL_CONFIG_FILE = join(
export const LOCAL_CONFIG_FILE = ".webstudio/config.json";
export const LOCAL_DATA_FILE = ".webstudio/data.json";
+// @todo remove
export const ASSETS_BASE = "/assets/";
export type LocalConfig = {
diff --git a/packages/cli/templates/build.ts b/packages/cli/templates/build.ts
index d932916508..51e3e038af 100644
--- a/packages/cli/templates/build.ts
+++ b/packages/cli/templates/build.ts
@@ -117,10 +117,10 @@ If needed to make any changes. Add them to ./templates folder and run pnpm run b
*/
-import { ASSETS_BASE } from "../config";
+
export const getRouteTemplate = () => {
- return \`${routeTemplateFile.replace(/ASSETS_BASE/g, '"${ASSETS_BASE}"')}\`
+ return \`${routeTemplateFile}\`
}`;
await ensureFileInPath(ROUTER_TEMPLATE_PATH, routeTemplateContent);
diff --git a/packages/cli/templates/defaults/app/constants.ts b/packages/cli/templates/defaults/app/constants.ts
new file mode 100644
index 0000000000..4ee7631403
--- /dev/null
+++ b/packages/cli/templates/defaults/app/constants.ts
@@ -0,0 +1,2 @@
+export const assetBaseUrl = "/assets/";
+export const imageBaseUrl = "/assets/";
diff --git a/packages/cli/templates/route-template.tsx b/packages/cli/templates/route-template.tsx
index ef53aa889a..d4d11b5783 100644
--- a/packages/cli/templates/route-template.tsx
+++ b/packages/cli/templates/route-template.tsx
@@ -1,9 +1,9 @@
/* eslint-disable camelcase */
import {
- V2_MetaFunction,
- LinksFunction,
- LinkDescriptor,
- ActionArgs,
+ type V2_MetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
json,
} from "@remix-run/node";
@@ -24,6 +24,7 @@ import {
} from "../__generated__/index";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
+import { assetBaseUrl, imageBaseUrl } from "~/constants.ts";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
@@ -46,7 +47,7 @@ export const links: LinksFunction = () => {
if (asset.type === "font") {
result.push({
rel: "preload",
- href: ASSETS_BASE + asset.name,
+ href: assetBaseUrl + asset.name,
as: "font",
crossOrigin: "anonymous",
// @todo add mimeType
@@ -147,9 +148,6 @@ const Outlet = () => {
});
}
- const assetBaseUrl = ASSETS_BASE;
- const imageBaseUrl = ASSETS_BASE;
-
const params: Params = {
assetBaseUrl,
imageBaseUrl,
diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json
index 13237ee04f..9ad982a38a 100644
--- a/packages/cli/tsconfig.json
+++ b/packages/cli/tsconfig.json
@@ -1,7 +1,9 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
- "exclude": ["./templates/*"],
"compilerOptions": {
- "module": "esnext"
+ "module": "esnext",
+ "paths": {
+ "~/constants.ts": ["./templates/defaults/app/constants.ts"]
+ }
}
}