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.

Use eval to parse shared consts during prebuild, add windows support (#2315)

Ivan Starkov98c2226a8ec5
diff --git a/fixtures/webstudio-remix-vercel/app/constants.ts b/fixtures/webstudio-remix-vercel/app/constants.mjs
similarity index 59%
rename from fixtures/webstudio-remix-vercel/app/constants.ts
rename to fixtures/webstudio-remix-vercel/app/constants.mjs
index 53c50668a5..5a92afbe55 100644
--- a/fixtures/webstudio-remix-vercel/app/constants.ts
+++ b/fixtures/webstudio-remix-vercel/app/constants.mjs
@@ -1,9 +1,14 @@
-import type { ImageLoader } from "@webstudio-is/image";
-
+/**
+ * We use mjs extension as constants in this file is shared with the build script
+ * and we use `node --eval` to extract the constants.
+ */
export const assetBaseUrl = "/assets/";
export const imageBaseUrl = "/assets/";
-export const imageLoader: ImageLoader = ({ quality, src, width }) => {
+/**
+ * @type {import("@webstudio-is/image").ImageLoader}
+ */
+export const imageLoader = ({ quality, src, width }) => {
if (process.env.NODE_ENV !== "production") {
return imageBaseUrl + src;
}
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 5201592f45..f63a540444 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
@@ -24,7 +24,7 @@ import {
} from "../__generated__/[_route_with_symbols_]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
-import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.ts";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.mjs";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index 44c37675e1..5e7212e173 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -24,7 +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, imageLoader } from "~/constants.ts";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.mjs";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index 00fe53d91e..ea3da85c98 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -24,7 +24,7 @@ import {
} from "../__generated__/_index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
-import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.ts";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.mjs";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
diff --git a/fixtures/webstudio-remix-vercel/tsconfig.json b/fixtures/webstudio-remix-vercel/tsconfig.json
index b0ec020d3a..b93e0d4486 100644
--- a/fixtures/webstudio-remix-vercel/tsconfig.json
+++ b/fixtures/webstudio-remix-vercel/tsconfig.json
@@ -1,5 +1,5 @@
{
- "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
+ "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx", "**/*.mjs"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"isolatedModules": true,
@@ -10,6 +10,7 @@
"target": "ES2022",
"strict": true,
"allowJs": true,
+ "checkJs": true,
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true,
"baseUrl": ".",
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 433580ba1a..8772a5c0ab 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -32,6 +32,7 @@
"@webstudio-is/sdk-components-react-remix": "workspace:^",
"deepmerge": "^4.3.1",
"env-paths": "^3.0.0",
+ "execa": "^7.2.0",
"ora": "^7.0.1",
"p-limit": "^4.0.0",
"picocolors": "^1.0.0",
diff --git a/packages/cli/src/commands/init-flow.ts b/packages/cli/src/commands/init-flow.ts
index 5bd9aca6df..3c80d2341f 100644
--- a/packages/cli/src/commands/init-flow.ts
+++ b/packages/cli/src/commands/init-flow.ts
@@ -1,6 +1,5 @@
import { ensureFolderExists, isFileExists } from "../fs-utils";
-import { chdir, cwd, stdout as shellOutput } from "node:process";
-import { spawn } from "node:child_process";
+import { chdir, cwd } from "node:process";
import { join } from "node:path";
import ora from "ora";
import { link } from "./link";
@@ -9,6 +8,7 @@ import { build, buildOptions } from "./build";
import { prompt } from "../prompts";
import type { StrictYargsOptionsToInterface } from "./yargs-types";
import pc from "picocolors";
+import { $ } from "execa";
export const initFlow = async (
options: StrictYargsOptionsToInterface<typeof buildOptions>
@@ -68,10 +68,7 @@ export const initFlow = async (
if (shouldInstallDeps === true) {
const spinner = ora().start();
spinner.text = "Installing dependencies";
- const { stderr } = await exec("npm", ["install"]);
- if (stderr) {
- throw stderr;
- }
+ await $`npm install`;
spinner.succeed("Installed dependencies");
}
@@ -87,30 +84,3 @@ export const initFlow = async (
.join("\n")
);
};
-
-const exec = (
- command: string,
- args?: ReadonlyArray<string>
-): Promise<{ stdout: string; stderr: string }> => {
- return new Promise((resolve, reject) => {
- const process = spawn(command, args);
- let stdout = "";
- let stderr = "";
- process.on("error", reject);
- process.on("exit", (code) => {
- if (code !== 0) {
- reject({ stdout, stderr });
- } else {
- resolve({ stdout, stderr });
- }
- });
-
- process.stderr.setEncoding("utf8");
- process.stdout.setEncoding("utf8");
- process.stdout.on("data", (data) => {
- stdout += data;
- shellOutput.write(data);
- });
- process.stderr.on("error", (error) => (stderr += error));
- });
-};
diff --git a/packages/cli/src/config.ts b/packages/cli/src/config.ts
index 4d7a82ed3a..de2c170006 100644
--- a/packages/cli/src/config.ts
+++ b/packages/cli/src/config.ts
@@ -11,9 +11,6 @@ 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 = {
projectId: string;
};
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 2a94d4c0cd..ab93488309 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -38,10 +38,12 @@ import type { Data } from "@webstudio-is/http-client";
import * as baseComponentMetas from "@webstudio-is/sdk-components-react/metas";
import * as remixComponentMetas from "@webstudio-is/sdk-components-react-remix/metas";
import * as radixComponentMetas from "@webstudio-is/sdk-components-react-radix/metas";
-import { ASSETS_BASE, LOCAL_DATA_FILE } from "./config";
+import { LOCAL_DATA_FILE } from "./config";
import { ensureFileInPath, ensureFolderExists, loadJSONFile } from "./fs-utils";
import merge from "deepmerge";
import { createImageLoader } from "@webstudio-is/image";
+import { $ } from "execa";
+import { fileURLToPath } from "node:url";
const limit = pLimit(10);
@@ -74,9 +76,10 @@ type RemixRoutes = {
export const downloadAsset = async (
url: string,
name: string,
+ assetBaseUrl: string,
temporaryDir: string
) => {
- const assetPath = join("public", ASSETS_BASE, name);
+ const assetPath = join("public", assetBaseUrl, name);
const tempAssetPath = join(temporaryDir, name);
try {
@@ -132,13 +135,10 @@ const mergeJsonFiles = async (sourcePath: string, destinationPath: string) => {
};
const copyTemplates = async () => {
+ const currentPath = fileURLToPath(new URL(import.meta.url));
+
const templatesPath = normalize(
- join(
- dirname(new URL(import.meta.url).pathname),
- "..",
- "templates",
- "defaults"
- )
+ join(dirname(currentPath), "..", "templates", "defaults")
);
await cp(templatesPath, cwd(), {
@@ -171,6 +171,15 @@ export const prebuild = async (options: {
await copyTemplates();
+ const constantsJson =
+ await $`node --input-type=module --eval ${`import * as consts from './app/constants.mjs'; console.log(JSON.stringify(consts))`}`;
+
+ const constants = JSON.parse(constantsJson.stdout);
+ const assetBaseUrl =
+ "assetBaseUrl" in constants
+ ? (constants.assetBaseUrl as string)
+ : "/assets";
+
const siteData = await loadJSONFile<
Data & { user?: { email: string | null } }
>(LOCAL_DATA_FILE);
@@ -293,7 +302,9 @@ export const prebuild = async (options: {
});
assetsToDownload.push(
- limit(() => downloadAsset(imageSrc, asset.name, temporaryDir))
+ limit(() =>
+ downloadAsset(imageSrc, asset.name, assetBaseUrl, temporaryDir)
+ )
);
}
@@ -303,6 +314,7 @@ export const prebuild = async (options: {
downloadAsset(
`${assetBuildUrl}${asset.name}`,
asset.name,
+ assetBaseUrl,
temporaryDir
)
)
@@ -324,7 +336,7 @@ export const prebuild = async (options: {
const routeFileTemplate = await readFile(
normalize(
join(
- dirname(new URL(import.meta.url).pathname),
+ dirname(fileURLToPath(new URL(import.meta.url))),
"..",
"templates",
"route-template.tsx"
@@ -453,7 +465,7 @@ ${utilsExport}
const routeFileContent = routeFileTemplate.replace(
"../__generated__/index",
- join("../__generated__", fileName)
+ `../__generated__/${fileName}`
);
await ensureFileInPath(join(routesDir, fileName), routeFileContent);
@@ -470,7 +482,7 @@ ${utilsExport}
componentMetas,
},
{
- assetBaseUrl: ASSETS_BASE,
+ assetBaseUrl,
}
);
await ensureFileInPath(join(generatedDir, "index.css"), cssText);
diff --git a/packages/cli/templates/defaults/app/constants.ts b/packages/cli/templates/defaults/app/constants.mjs
similarity index 59%
rename from packages/cli/templates/defaults/app/constants.ts
rename to packages/cli/templates/defaults/app/constants.mjs
index 53c50668a5..5a92afbe55 100644
--- a/packages/cli/templates/defaults/app/constants.ts
+++ b/packages/cli/templates/defaults/app/constants.mjs
@@ -1,9 +1,14 @@
-import type { ImageLoader } from "@webstudio-is/image";
-
+/**
+ * We use mjs extension as constants in this file is shared with the build script
+ * and we use `node --eval` to extract the constants.
+ */
export const assetBaseUrl = "/assets/";
export const imageBaseUrl = "/assets/";
-export const imageLoader: ImageLoader = ({ quality, src, width }) => {
+/**
+ * @type {import("@webstudio-is/image").ImageLoader}
+ */
+export const imageLoader = ({ quality, src, width }) => {
if (process.env.NODE_ENV !== "production") {
return imageBaseUrl + src;
}
diff --git a/packages/cli/templates/defaults/tsconfig.json b/packages/cli/templates/defaults/tsconfig.json
index b0ec020d3a..b93e0d4486 100644
--- a/packages/cli/templates/defaults/tsconfig.json
+++ b/packages/cli/templates/defaults/tsconfig.json
@@ -1,5 +1,5 @@
{
- "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
+ "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx", "**/*.mjs"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"isolatedModules": true,
@@ -10,6 +10,7 @@
"target": "ES2022",
"strict": true,
"allowJs": true,
+ "checkJs": true,
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true,
"baseUrl": ".",
diff --git a/packages/cli/templates/route-template.tsx b/packages/cli/templates/route-template.tsx
index 28cb3c41d8..abea941062 100644
--- a/packages/cli/templates/route-template.tsx
+++ b/packages/cli/templates/route-template.tsx
@@ -24,7 +24,7 @@ import {
} from "../__generated__/index";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
-import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.ts";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.mjs";
export type PageData = Omit<Data, "build"> & {
build: Pick<Data["build"], "props" | "instances" | "dataSources">;
diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json
index 9ad982a38a..1ae963e7ff 100644
--- a/packages/cli/tsconfig.json
+++ b/packages/cli/tsconfig.json
@@ -1,9 +1,12 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
+ "include": ["**/*.ts", "**/*.tsx", "**/*.mjs"],
"compilerOptions": {
"module": "esnext",
+ "allowJs": true,
+ "checkJs": true,
"paths": {
- "~/constants.ts": ["./templates/defaults/app/constants.ts"]
+ "~/constants.mjs": ["./templates/defaults/app/constants.mjs"]
}
}
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2c8e75e46b..57e525c0f9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -538,6 +538,9 @@ importers:
env-paths:
specifier: ^3.0.0
version: 3.0.0
+ execa:
+ specifier: ^7.2.0
+ version: 7.2.0
ora:
specifier: ^7.0.1
version: 7.0.1