Skip to content

Commit

Permalink
refactor: add sdk runtime (#4417)
Browse files Browse the repository at this point in the history
Similar to react-sdk/runtime added another entry point to sdk package
which includes all the stuff imported on published sites to reduce js
size.

./fixtures/webstudio-remix-netlify-functions/build/client: 392kB
(-176kB)
  • Loading branch information
TrySound authored Nov 13, 2024
1 parent bb8e41a commit f75a837
Show file tree
Hide file tree
Showing 38 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
}
const results = [
await assertSize('./fixtures/ssg/dist/client', 308),
await assertSize('./fixtures/webstudio-remix-netlify-functions/build/client', 568),
await assertSize('./fixtures/webstudio-remix-netlify-functions/build/client', 396),
]
for (const result of results) {
if (result.passed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
generateObjectExpression,
isLiteralExpression,
parseObjectExpression,
sitemapResourceUrl,
} from "@webstudio-is/sdk";
import { sitemapResourceUrl } from "@webstudio-is/sdk/runtime";
import {
Box,
Button,
Expand Down
7 changes: 2 additions & 5 deletions apps/builder/app/routes/rest.resources-loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { z } from "zod";
import { json, type ActionFunctionArgs } from "@remix-run/server-runtime";
import {
loadResource,
isLocalResource,
ResourceRequest,
} from "@webstudio-is/sdk";
import { ResourceRequest } from "@webstudio-is/sdk";
import { isLocalResource, loadResource } from "@webstudio-is/sdk/runtime";
import { loader as siteMapLoader } from "../shared/$resources/sitemap.xml.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
import { checkCsrf } from "~/services/csrf-session.server";
Expand Down
6 changes: 4 additions & 2 deletions apps/builder/app/shared/nano-states/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import type {
ImageAsset,
} from "@webstudio-is/sdk";
import {
createJsonStringifyProxy,
decodeDataSourceVariable,
encodeDataSourceVariable,
isPlainObject,
transpileExpression,
} from "@webstudio-is/sdk";
import {
createJsonStringifyProxy,
isPlainObject,
} from "@webstudio-is/sdk/runtime";
import {
collectionComponent,
normalizeProps,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/ssg-netlify-by-project-id/pages/index/+data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PageContextServer } from "vike/types";
import { redirect } from "vike/abort";
import { loadResources } from "@webstudio-is/sdk";
import { loadResources } from "@webstudio-is/sdk/runtime";
import {
getPageMeta,
getResources,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/ssg/pages/another-page/+data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PageContextServer } from "vike/types";
import { redirect } from "vike/abort";
import { loadResources } from "@webstudio-is/sdk";
import { loadResources } from "@webstudio-is/sdk/runtime";
import {
getPageMeta,
getResources,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/ssg/pages/index/+data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PageContextServer } from "vike/types";
import { redirect } from "vike/abort";
import { loadResources } from "@webstudio-is/sdk";
import { loadResources } from "@webstudio-is/sdk/runtime";
import {
getPageMeta,
getResources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderToString } from "react-dom/server";
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { isLocalResource, loadResources } from "@webstudio-is/sdk";
import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import { Page } from "../__generated__/[sitemap.xml]._index";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/webstudio-custom-template/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderToString } from "react-dom/server";
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { isLocalResource, loadResources } from "@webstudio-is/sdk";
import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import { Page } from "../__generated__/[sitemap.xml]._index";
import {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/webstudio-remix-vercel/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
loadResources,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/sdk";
} from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import {
Page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderToString } from "react-dom/server";
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { isLocalResource, loadResources } from "@webstudio-is/sdk";
import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
import { Page } from "__CLIENT__";
import { getPageMeta, getRemixParams, getResources } from "__SERVER__";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PageContextServer } from "vike/types";
import { redirect } from "vike/abort";
import { loadResources } from "@webstudio-is/sdk";
import { loadResources } from "@webstudio-is/sdk/runtime";
import { getPageMeta, getResources } from "__SERVER__";

export const data = async (pageContext: PageContextServer) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-sdk/src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createContext, useContext, useMemo } from "react";
import type { ImageLoader } from "@webstudio-is/image";
import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
import {
createJsonStringifyProxy,
isPlainObject,
} from "@webstudio-is/sdk/runtime";

export type Params = {
/**
Expand Down
5 changes: 4 additions & 1 deletion packages/react-sdk/src/variable-state.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
import { useState, useMemo, type Dispatch, type SetStateAction } from "react";
import {
createJsonStringifyProxy,
isPlainObject,
} from "@webstudio-is/sdk/runtime";

export const useVariableState = <S,>(
initialState: S | (() => S)
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-components-react-remix/src/webhook-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useEffect,
} from "react";
import { useFetcher, type Fetcher, type FormProps } from "@remix-run/react";
import { formIdFieldName, formBotFieldName } from "@webstudio-is/sdk";
import { formIdFieldName, formBotFieldName } from "@webstudio-is/sdk/runtime";

export const defaultTag = "form";

Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"types": "./lib/types/index.d.ts",
"import": "./lib/index.js"
},
"./runtime": {
"webstudio": "./src/runtime.ts",
"types": "./lib/types/runtime.d.ts",
"import": "./lib/runtime.js"
},
"./normalize.css": {
"webstudio": "./src/__generated__/normalize.css.ts",
"types": "./lib/types/__generated__/normalize.css.d.ts",
Expand All @@ -30,7 +35,7 @@
"typecheck": "tsc",
"test": "vitest run",
"build:normalize.css": "tsx --conditions=webstudio ./scripts/normalize.css.ts && prettier --write src/__generated__/normalize.css.ts",
"build": "rm -rf lib && esbuild src/index.ts src/__generated__/normalize.css.ts --outdir=lib --bundle --format=esm --packages=external",
"build": "rm -rf lib && esbuild src/index.ts src/runtime.ts src/__generated__/normalize.css.ts --outdir=lib --bundle --format=esm --packages=external",
"dts": "tsc --project tsconfig.dts.json"
},
"dependencies": {
Expand Down
3 changes: 0 additions & 3 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export * from "./schema/webstudio";
export * from "./instances-utils";
export * from "./page-utils";
export * from "./scope";
export * from "./resource-loader";
export * from "./expression";
export * from "./resources-generator";
export * from "./page-meta-generator";
export * from "./url-pattern";
export * from "./form-fields";
export * from "./to-string";
17 changes: 17 additions & 0 deletions packages/sdk/src/resource-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import type { ResourceRequest } from "./schema/resources";

const LOCAL_RESOURCE_PREFIX = "$resources";

/**
* Prevents fetch cycles by prefixing local resources.
*/
export const isLocalResource = (pathname: string, resourceName?: string) => {
const segments = pathname.split("/").filter(Boolean);

if (resourceName === undefined) {
return segments[0] === LOCAL_RESOURCE_PREFIX;
}

return segments.join("/") === `${LOCAL_RESOURCE_PREFIX}/${resourceName}`;
};

export const sitemapResourceUrl = `/${LOCAL_RESOURCE_PREFIX}/sitemap.xml`;

export const loadResource = async (
customFetch: typeof fetch,
resourceRequest: ResourceRequest
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./resource-loader";
export * from "./to-string";
export * from "./form-fields";
17 changes: 0 additions & 17 deletions packages/sdk/src/schema/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,3 @@ export type ResourceRequest = z.infer<typeof ResourceRequest>;
export const Resources = z.map(ResourceId, Resource);

export type Resources = z.infer<typeof Resources>;

const LOCAL_RESOURCE_PREFIX = "$resources";

/**
* Prevents fetch cycles by prefixing local resources.
*/
export const isLocalResource = (pathname: string, resourceName?: string) => {
const segments = pathname.split("/").filter(Boolean);

if (resourceName === undefined) {
return segments[0] === LOCAL_RESOURCE_PREFIX;
}

return segments.join("/") === `${LOCAL_RESOURCE_PREFIX}/${resourceName}`;
};

export const sitemapResourceUrl = `/${LOCAL_RESOURCE_PREFIX}/sitemap.xml`;

0 comments on commit f75a837

Please sign in to comment.