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: CLI Mutiple template support (#2957)

Ivan Starkov2b888d52fd38
diff --git a/fixtures/webstudio-custom-template/app/root.tsx b/fixtures/webstudio-custom-template/app/root.tsx
index d873875663..61d6aff561 100644
--- a/fixtures/webstudio-custom-template/app/root.tsx
+++ b/fixtures/webstudio-custom-template/app/root.tsx
@@ -1 +1,15 @@
-export { Root as default } from "@webstudio-is/react-sdk";
+import { Root as SdkRoot } from "@webstudio-is/react-sdk";
+import { useEffect } from "react";
+
+/**
+ * Custom root so we can add some custom logic for specific resurces etc like analytics, etc.
+ */
+const Root = (props: React.ComponentProps<typeof SdkRoot>) => {
+ useEffect(() => {
+ console.info("Hello, world!");
+ }, []);
+
+ return <SdkRoot {...props} />;
+};
+
+export default Root;
diff --git a/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx b/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx
new file mode 100644
index 0000000000..61d6aff561
--- /dev/null
+++ b/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx
@@ -0,0 +1,15 @@
+import { Root as SdkRoot } from "@webstudio-is/react-sdk";
+import { useEffect } from "react";
+
+/**
+ * Custom root so we can add some custom logic for specific resurces etc like analytics, etc.
+ */
+const Root = (props: React.ComponentProps<typeof SdkRoot>) => {
+ useEffect(() => {
+ console.info("Hello, world!");
+ }, []);
+
+ return <SdkRoot {...props} />;
+};
+
+export default Root;
diff --git a/fixtures/webstudio-custom-template/package.json b/fixtures/webstudio-custom-template/package.json
index ce6f7935b4..bb7ef17212 100644
--- a/fixtures/webstudio-custom-template/package.json
+++ b/fixtures/webstudio-custom-template/package.json
@@ -9,7 +9,7 @@
"size-test": "rm -rf ./public && remix build && size-limit",
"fixtures:link": "webstudio link --link 'https://main.prs.webstudio.is/builder/0d856812-61d8-4014-a20a-82e01c0eb8ee?authToken=d225fafb-4f20-4340-9359-c21df7c49a3f'",
"fixtures:sync": "webstudio sync && pnpm prettier --write ./.webstudio/",
- "fixtures:build": "webstudio build --template ./custom-template --preview && pnpm prettier --write ./app/ ./package.json"
+ "fixtures:build": "webstudio build --template ./custom-template --template ./custom-template-stage --preview && pnpm prettier --write ./app/ ./package.json"
},
"private": true,
"sideEffects": false,
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index d586150613..653820da04 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -19,7 +19,8 @@ export const buildOptions = (yargs: CommonYargsArgv) =>
describe: "[Experimental] Use preview version of the project",
})
.option("template", {
- type: "string",
+ type: "array",
+ string: true,
describe: `Template to use for the build [choices: ${PROJECT_TEMPALTES.join(
", "
)}]`,
diff --git a/packages/cli/src/commands/init-flow.ts b/packages/cli/src/commands/init-flow.ts
index a16f0830f3..e5c49eaeef 100644
--- a/packages/cli/src/commands/init-flow.ts
+++ b/packages/cli/src/commands/init-flow.ts
@@ -104,7 +104,7 @@ export const initFlow = async (
await build({
...options,
- ...(projectTemplate && { template: projectTemplate }),
+ ...(projectTemplate && { template: [projectTemplate] }),
});
if (shouldInstallDeps === true) {
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 735afae25f..6e6ab26c95 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -211,7 +211,7 @@ export const prebuild = async (options: {
/**
* Template to use for the build in addition to defaults template
**/
- template?: string;
+ template?: string[];
}) => {
if (options.template === undefined) {
throw new Error(
@@ -219,14 +219,22 @@ export const prebuild = async (options: {
);
}
- if (
- options.template !== "vanilla" &&
- (await isCliTemplate(options.template)) === false &&
- options.template.startsWith(".") === false
- ) {
- throw Error(
- `\n Template ${options.template} is not available \n Please check webstudio --help for more details`
- );
+ for (const template of options.template) {
+ // default template is always applied no need to check
+ if (template === "vanilla") {
+ continue;
+ }
+
+ // Template is local user template
+ if (template.startsWith(".")) {
+ continue;
+ }
+
+ if ((await isCliTemplate(template)) === false) {
+ throw Error(
+ `\n Template ${options.template} is not available \n Please check webstudio --help for more details`
+ );
+ }
}
const spinner = ora("Scaffolding the project files");
@@ -244,8 +252,13 @@ export const prebuild = async (options: {
await copyTemplates();
- if (options.template !== "vanilla") {
- await copyTemplates(options.template);
+ for (const template of options.template) {
+ // default template is already applied no need to copy twice
+ if (template === "vanilla") {
+ continue;
+ }
+
+ await copyTemplates(template);
}
const constants: typeof sharedConstants = await import(