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.

Add feature-flags package (#800)

Bogdan Chadkin2c8395b4d0c2
diff --git a/apps/designer/app/designer/features/sidebar-left/sidebar-left.tsx b/apps/designer/app/designer/features/sidebar-left/sidebar-left.tsx
index cbbc2639ae..367b93c87e 100644
--- a/apps/designer/app/designer/features/sidebar-left/sidebar-left.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/sidebar-left.tsx
@@ -1,15 +1,15 @@
import { useState } from "react";
-import { useSubscribe, type Publish } from "~/shared/pubsub";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import {
SidebarTabs,
SidebarTabsContent,
SidebarTabsList,
SidebarTabsTrigger,
} from "@webstudio-is/design-system";
+import { useSubscribe, type Publish } from "~/shared/pubsub";
import { useDragAndDropState } from "~/shared/nano-states";
import { panels } from "./panels";
import type { TabName } from "./types";
-import { isFeatureEnabled } from "~/shared/feature-flags";
import { useClientSettings } from "~/designer/shared/client-settings";
import { Flex } from "@webstudio-is/design-system";
import { theme } from "@webstudio-is/design-system";
diff --git a/apps/designer/app/designer/features/style-panel/shared/property-name.tsx b/apps/designer/app/designer/features/style-panel/shared/property-name.tsx
index 44243e46cc..d52bca4a54 100644
--- a/apps/designer/app/designer/features/style-panel/shared/property-name.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/property-name.tsx
@@ -1,4 +1,5 @@
import { useState } from "react";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import type { StyleProperty } from "@webstudio-is/css-data";
import { toValue } from "@webstudio-is/css-engine";
import {
@@ -16,7 +17,6 @@ import {
} from "@webstudio-is/design-system";
import { UndoIcon } from "@webstudio-is/icons";
import { utils } from "@webstudio-is/project";
-import { isFeatureEnabled } from "~/shared/feature-flags";
import { useBreakpoints, useRootInstance } from "~/shared/nano-states";
import { type StyleInfo, type StyleSource, getStyleSource } from "./style-info";
import { theme } from "@webstudio-is/design-system";
diff --git a/apps/designer/app/designer/features/topbar/menu/menu.tsx b/apps/designer/app/designer/features/topbar/menu/menu.tsx
index 9aba88814a..8bcaf2404f 100644
--- a/apps/designer/app/designer/features/topbar/menu/menu.tsx
+++ b/apps/designer/app/designer/features/topbar/menu/menu.tsx
@@ -1,5 +1,5 @@
import { useNavigate } from "react-router-dom";
-import { type Publish } from "~/shared/pubsub";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import {
DropdownMenu,
DropdownMenuContent,
@@ -15,6 +15,7 @@ import {
Box,
} from "@webstudio-is/design-system";
import { HamburgerMenuIcon, ChevronRightIcon } from "@webstudio-is/icons";
+import { type Publish } from "~/shared/pubsub";
import { ShortcutHint } from "./shortcut-hint";
import {
useIsShareDialogOpen,
@@ -27,7 +28,6 @@ import {
} from "~/shared/theme";
import { useClientSettings } from "~/designer/shared/client-settings";
import { dashboardPath } from "~/shared/router-utils";
-import { isFeatureEnabled } from "~/shared/feature-flags";
import { theme } from "@webstudio-is/design-system";
const menuItemCss = {
diff --git a/apps/designer/app/root.tsx b/apps/designer/app/root.tsx
index 6de81f2444..d5c157b0fe 100644
--- a/apps/designer/app/root.tsx
+++ b/apps/designer/app/root.tsx
@@ -1,11 +1,15 @@
// Our root outlet doesn't contain a layout because we have 2 types of documents: canvas and designer and we need to decide down the line which one to render, thre is no single root document.
import { TooltipProvider } from "@radix-ui/react-tooltip";
import { Outlet } from "@remix-run/react";
+import { setEnv } from "@webstudio-is/feature-flags";
import { withSentryRouteTracing } from "@sentry/remix";
import { ErrorBoundary } from "@sentry/remix";
import { OutletProps } from "react-router-dom";
+import env from "./shared/env";
import { PersistentFetcherProvider } from "./shared/fetcher";
+setEnv(env.FEATURES as string);
+
const RootWithErrorBoundary = (props: OutletProps) => (
<ErrorBoundary>
<TooltipProvider>
diff --git a/apps/designer/app/shared/theme/theme.ts b/apps/designer/app/shared/theme/theme.ts
index f933fd4a85..5ce48771ff 100644
--- a/apps/designer/app/shared/theme/theme.ts
+++ b/apps/designer/app/shared/theme/theme.ts
@@ -1,7 +1,7 @@
import { useLoaderData } from "@remix-run/react";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import { darkTheme } from "@webstudio-is/design-system";
import { restThemePath } from "~/shared/router-utils";
-import { isFeatureEnabled } from "../feature-flags";
import type { ColorScheme, ThemeSetting } from "./types";
// User selected theme setting.
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 4db8e9bb26..159adf66b8 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -39,6 +39,7 @@
"@webstudio-is/css-vars": "workspace:^",
"@webstudio-is/design-system": "workspace:^",
"@webstudio-is/design-tokens": "workspace:^",
+ "@webstudio-is/feature-flags": "workspace:^",
"@webstudio-is/fonts": "workspace:^",
"@webstudio-is/icons": "workspace:^",
"@webstudio-is/image": "workspace:^",
diff --git a/packages/feature-flags/LICENSE b/packages/feature-flags/LICENSE
new file mode 100644
index 0000000000..1f6edad9a7
--- /dev/null
+++ b/packages/feature-flags/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Webstudio
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/packages/feature-flags/package.json b/packages/feature-flags/package.json
new file mode 100644
index 0000000000..b07f595129
--- /dev/null
+++ b/packages/feature-flags/package.json
@@ -0,0 +1,30 @@
+{
+ "name": "@webstudio-is/feature-flags",
+ "version": "0.1.0",
+ "description": "Feature flags used to enable/disable a feature over env variable",
+ "author": "Webstudio <[email protected]>",
+ "homepage": "https://webstudio.is",
+ "type": "module",
+ "scripts": {
+ "typecheck": "tsc --noEmit",
+ "checks": "pnpm typecheck && pnpm lint",
+ "dev": "build-package --watch",
+ "build": "build-package",
+ "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "@webstudio-is/scripts": "workspace:^",
+ "@webstudio-is/tsconfig": "workspace:^"
+ },
+ "exports": "./lib/index.js",
+ "types": "src/index.ts",
+ "files": [
+ "lib/*",
+ "src/*",
+ "!*.test.*"
+ ],
+ "license": "MIT",
+ "private": false,
+ "sideEffects": false
+}
diff --git a/apps/designer/app/shared/feature-flags/feature.ts b/packages/feature-flags/src/feature.ts
similarity index 89%
rename from apps/designer/app/shared/feature-flags/feature.ts
rename to packages/feature-flags/src/feature.ts
index 36c2254674..ecc905fe41 100644
--- a/apps/designer/app/shared/feature-flags/feature.ts
+++ b/packages/feature-flags/src/feature.ts
@@ -1,5 +1,6 @@
import * as flags from "./flags";
-import env from "~/shared/env";
+
+let env = "";
type Name = keyof typeof flags;
@@ -17,6 +18,10 @@ const readLocal = (): Array<Name> => {
return [];
};
+export const setEnv = (features: string) => {
+ env = features;
+};
+
/**
* Returns true/false if the feature is turned on.
* A feature can be turned on:
@@ -25,11 +30,11 @@ const readLocal = (): Array<Name> => {
* - by setting it in the browser console: localStorage.features = 'something1, something2', browser defined flag will override server-side flag
*/
export const isFeatureEnabled = (name: Name): boolean => {
- if (env.FEATURES === "*") {
+ if (env === "*") {
return true;
}
const defaultValue = flags[name];
- const envValue = parse(env.FEATURES).includes(name);
+ const envValue = parse(env).includes(name);
const localValue = readLocal().includes(name);
// Any source can enable feature, first `true` value will result in enabling a feature.
// This also means you can't disable a feature if its already enabled in default value.
diff --git a/apps/designer/app/shared/feature-flags/flags.ts b/packages/feature-flags/src/flags.ts
similarity index 100%
rename from apps/designer/app/shared/feature-flags/flags.ts
rename to packages/feature-flags/src/flags.ts
diff --git a/apps/designer/app/shared/feature-flags/index.ts b/packages/feature-flags/src/index.ts
similarity index 100%
rename from apps/designer/app/shared/feature-flags/index.ts
rename to packages/feature-flags/src/index.ts
diff --git a/packages/feature-flags/tsconfig.json b/packages/feature-flags/tsconfig.json
new file mode 100644
index 0000000000..f1a42e1669
--- /dev/null
+++ b/packages/feature-flags/tsconfig.json
@@ -0,0 +1,11 @@
+{
+ "extends": "@webstudio-is/tsconfig/base.json",
+ "include": ["src"],
+ "compilerOptions": {
+ "module": "ES2020",
+ "target": "ES2020",
+ "baseUrl": ".",
+ "outDir": "./lib",
+ "rootDir": "./src"
+ }
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6b2d1dc68b..20766b59ee 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -87,6 +87,7 @@ importers:
'@webstudio-is/design-system': workspace:^
'@webstudio-is/design-tokens': workspace:^
'@webstudio-is/eslint-config-custom': workspace:^
+ '@webstudio-is/feature-flags': workspace:^
'@webstudio-is/fonts': workspace:^
'@webstudio-is/icons': workspace:^
'@webstudio-is/image': workspace:^
@@ -170,6 +171,7 @@ importers:
'@webstudio-is/css-vars': link:../../packages/css-vars
'@webstudio-is/design-system': link:../../packages/design-system
'@webstudio-is/design-tokens': link:../../packages/design-tokens
+ '@webstudio-is/feature-flags': link:../../packages/feature-flags
'@webstudio-is/fonts': link:../../packages/fonts
'@webstudio-is/icons': link:../../packages/icons
'@webstudio-is/image': link:../../packages/image
@@ -534,6 +536,14 @@ importers:
eslint-plugin-unicorn: [email protected]
typescript: 4.7.4
+ packages/feature-flags:
+ specifiers:
+ '@webstudio-is/scripts': workspace:^
+ '@webstudio-is/tsconfig': workspace:^
+ devDependencies:
+ '@webstudio-is/scripts': link:../scripts
+ '@webstudio-is/tsconfig': link:../tsconfig
+
packages/fonts:
specifiers:
'@jest/globals': ^29.3.1