Files
webstudio/packages/cli/src/config.ts
T
Ivan Starkov 54e686a4e3 experimental: Add ssg-netlify and ssg-vercel to the cli (#3835)
## Description

### Todos

- [x] - Expand support: `ssg` and `ssg-(netlify|vercel)` differ by only
one file. This expansion allows a single external template to apply to
multiple internal uses (previously hardcoded in prebuild, see
`defaults`).
- [x] - Add & test ssg-vercel and ssg-netlify constants.mjs templates to
support image optimizations.

## Steps for reproduction

```bash
cd ./fixtures/ssg
pnpm cli build --template ssg-netlify --template internal --preview && pnpm prettier --write .
pnpm build
```

then drag drop on netlify and see the result
[example](https://66a9d3c5c6bb86681c6611e4--inspiring-liger-df7a81.netlify.app/)
```html
<img alt="" width="14" height="16" src="/.netlify/images?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=32&amp;q=80" class="w-image c161qeci" sizes="100vw" srcset="/.netlify/images?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=16&amp;q=80 16w, /.netlify/images?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=32&amp;q=80 32w" decoding="async" loading="lazy">
```


```bash
cd ./fixtures/ssg
pnpm cli build --template ssg-vercel --template internal --preview && pnpm prettier --write .
pnpm build
```

Upload to https://github.com/webstudio-is/ssg-vercel-test/tree/main
See vercel deployment
[example](https://ssg-vercel-test-r0a778rpj-getwebstudio.vercel.app/)

```html
<img alt="" width="14" height="16" src="/_vercel/image?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=32&amp;q=80" class="w-image c161qeci" sizes="100vw" srcset="/_vercel/image?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=16&amp;q=80 16w, /_vercel/image?url=%2Fassets%2Ficonly_svg_converted-converted_zMaMiAAutUl8XrITgz7d1.svg&amp;w=32&amp;q=80 32w" decoding="async" loading="lazy">
```





## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-07-31 19:06:43 +03:00

93 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { join } from "node:path";
import envPaths from "env-paths";
import { z } from "zod";
const GLOBAL_CONFIG_FOLDER = envPaths("webstudio").config;
const GLOBAL_CONFIG_FILE_NAME = "webstudio-config.json";
export const GLOBAL_CONFIG_FILE = join(
GLOBAL_CONFIG_FOLDER,
GLOBAL_CONFIG_FILE_NAME
);
export const LOCAL_CONFIG_FILE = ".webstudio/config.json";
export const LOCAL_DATA_FILE = ".webstudio/data.json";
const zLocalConfig = z.object({
projectId: z.string(),
});
export type LocalConfig = z.infer<typeof zLocalConfig>;
export const jsonToLocalConfig = (json: unknown) => {
return zLocalConfig.parse(json);
};
const zGlobalConfig = z.record(
z
.union([
z.object({
// origin mistakenly called host in the past
host: z.string(),
token: z.string(),
}),
z.object({
origin: z.string(),
token: z.string(),
}),
])
.transform((value) => {
if ("host" in value) {
return {
origin: value.host,
token: value.token,
};
}
return value;
})
);
export const jsonToGlobalConfig = (json: unknown) => {
return zGlobalConfig.parse(json);
};
export type GlobalConfig = z.infer<typeof zGlobalConfig>;
export const PROJECT_TEMPLATES = [
{
value: "vanilla" as const,
label: "Vanilla",
expand: ["defaults"],
},
{ value: "vercel" as const, label: "Vercel", expand: ["defaults", "vercel"] },
{
value: "netlify-functions" as const,
label: "Netlify Functions",
expand: ["defaults", "netlify-functions"],
},
{
value: "netlify-edge-functions" as const,
label: "Netlify Edge Functions",
expand: ["defaults", "netlify-edge-functions"],
},
{ value: "ssg" as const, label: "Static Site Generation (SSG)" },
{
value: "ssg-netlify" as const,
label: "Static Site Generation (SSG) Netlify",
expand: ["ssg", "ssg-netlify"],
},
{
value: "ssg-vercel" as const,
label: "Static Site Generation (SSG) Vercel",
expand: ["ssg", "ssg-vercel"],
},
];
// This feature will be made public eventually; currently, it’s only used for internal tasks.
export const INTERNAL_TEMPLATES = [
{
value: "cloudflare",
label: "Cloudflare",
expand: ["defaults", "cloudflare"],
},
];