@scaleway/use-growthbook 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scaleway/use-growthbook
2
2
 
3
+ ## 2.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1953](https://github.com/scaleway/scaleway-lib/pull/1953) [`1fddf05`](https://github.com/scaleway/scaleway-lib/commit/1fddf0515851908b094f983b05b3d87af8eef433) Thanks [@philibea](https://github.com/philibea)! - add cjs build
8
+
9
+ ## 2.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - [#1943](https://github.com/scaleway/scaleway-lib/pull/1943) [`e726def`](https://github.com/scaleway/scaleway-lib/commit/e726def8e0cb4593f800f9acecca51b173ae907a) Thanks [@philibea](https://github.com/philibea)! - Migration from rollup to vite
14
+
3
15
  ## 2.0.2
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,21 @@
1
+ import type { GrowthBook as GBType } from '@growthbook/growthbook-react'
2
+ import type { ReactNode } from 'react'
3
+ import { vi } from 'vitest'
4
+
5
+ export const getAttributes = vi.fn<[], ReturnType<GBType['getAttributes']>>()
6
+ export const setAttributes = vi.fn<[], ReturnType<GBType['setAttributes']>>(
7
+ () => Promise.resolve(),
8
+ )
9
+
10
+ export const GrowthBook = vi.fn(() => ({
11
+ loadFeatures: vi.fn<[], ReturnType<GBType['loadFeatures']>>(),
12
+ getAttributes,
13
+ setAttributes,
14
+ }))
15
+ export const GrowthBookProvider = ({ children }: { children: ReactNode }) =>
16
+ children
17
+
18
+ export const useGrowthBook = vi.fn()
19
+
20
+ console.debug('GrowthBook Mock', GrowthBookProvider)
21
+ // export { GrowthBook, GrowthBookProvider, useGrowthBook }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ const growthbookReact = require("@growthbook/growthbook-react");
5
+ const react = require("react");
6
+ const defaultLoadConfig = {
7
+ autoRefresh: false,
8
+ timeout: 500,
9
+ skipCache: false
10
+ };
11
+ const getGrowthBookInstance = ({
12
+ config: {
13
+ apiHost,
14
+ clientKey,
15
+ enableDevMode,
16
+ backgroundSync,
17
+ subscribeToChanges
18
+ },
19
+ trackingCallback
20
+ }) => new growthbookReact.GrowthBook({
21
+ apiHost,
22
+ clientKey,
23
+ enableDevMode,
24
+ trackingCallback,
25
+ backgroundSync,
26
+ subscribeToChanges
27
+ });
28
+ const AbTestProvider = ({
29
+ children,
30
+ config,
31
+ trackingCallback,
32
+ errorCallback,
33
+ attributes,
34
+ loadConfig
35
+ }) => {
36
+ const growthbook = react.useMemo(
37
+ () => getGrowthBookInstance({ config, trackingCallback }),
38
+ [trackingCallback, config]
39
+ );
40
+ const loadFeature = react.useCallback(async () => {
41
+ if (config.clientKey) {
42
+ await growthbook.loadFeatures(loadConfig ?? defaultLoadConfig);
43
+ }
44
+ }, [growthbook, config, loadConfig]);
45
+ react.useEffect(() => {
46
+ loadFeature().catch(errorCallback);
47
+ }, [loadFeature, errorCallback]);
48
+ react.useEffect(() => {
49
+ const currentAttributes = growthbook.getAttributes();
50
+ if (currentAttributes !== attributes) {
51
+ growthbook.setAttributes({
52
+ ...currentAttributes,
53
+ ...attributes
54
+ }).catch(errorCallback);
55
+ }
56
+ }, [attributes, growthbook, errorCallback]);
57
+ return /* @__PURE__ */ jsxRuntime.jsx(growthbookReact.GrowthBookProvider, { growthbook, children });
58
+ };
59
+ exports.AbTestProvider = AbTestProvider;
@@ -0,0 +1,12 @@
1
+ import { type ReactNode } from 'react';
2
+ import type { Attributes, ErrorCallback, LoadConfig, ToolConfig, TrackingCallback } from './types';
3
+ type AbTestProviderProps = {
4
+ children: ReactNode;
5
+ config: ToolConfig;
6
+ trackingCallback: TrackingCallback;
7
+ errorCallback: ErrorCallback;
8
+ attributes: Attributes;
9
+ loadConfig?: LoadConfig;
10
+ };
11
+ export declare const AbTestProvider: ({ children, config, trackingCallback, errorCallback, attributes, loadConfig, }: AbTestProviderProps) => import("react").JSX.Element;
12
+ export {};
@@ -1,7 +1,6 @@
1
- import { GrowthBookProvider, GrowthBook } from '@growthbook/growthbook-react';
2
- import { useMemo, useCallback, useEffect } from 'react';
3
- import { jsx } from 'react/jsx-runtime';
4
-
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { GrowthBookProvider, GrowthBook } from "@growthbook/growthbook-react";
3
+ import { useMemo, useCallback, useEffect } from "react";
5
4
  const defaultLoadConfig = {
6
5
  autoRefresh: false,
7
6
  timeout: 500,
@@ -32,10 +31,10 @@ const AbTestProvider = ({
32
31
  attributes,
33
32
  loadConfig
34
33
  }) => {
35
- const growthbook = useMemo(() => getGrowthBookInstance({
36
- config,
37
- trackingCallback
38
- }), [trackingCallback, config]);
34
+ const growthbook = useMemo(
35
+ () => getGrowthBookInstance({ config, trackingCallback }),
36
+ [trackingCallback, config]
37
+ );
39
38
  const loadFeature = useCallback(async () => {
40
39
  if (config.clientKey) {
41
40
  await growthbook.loadFeatures(loadConfig ?? defaultLoadConfig);
@@ -53,10 +52,8 @@ const AbTestProvider = ({
53
52
  }).catch(errorCallback);
54
53
  }
55
54
  }, [attributes, growthbook, errorCallback]);
56
- return jsx(GrowthBookProvider, {
57
- growthbook: growthbook,
58
- children: children
59
- });
55
+ return /* @__PURE__ */ jsx(GrowthBookProvider, { growthbook, children });
56
+ };
57
+ export {
58
+ AbTestProvider
60
59
  };
61
-
62
- export { AbTestProvider };
package/dist/index.cjs ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const growthbookReact = require("@growthbook/growthbook-react");
4
+ const useAbTestAttributes = require("./useAbTestAttributes.cjs");
5
+ const AbTestProvider = require("./AbTestProvider.cjs");
6
+ Object.defineProperty(exports, "FeatureString", {
7
+ enumerable: true,
8
+ get: () => growthbookReact.FeatureString
9
+ });
10
+ Object.defineProperty(exports, "FeaturesReady", {
11
+ enumerable: true,
12
+ get: () => growthbookReact.FeaturesReady
13
+ });
14
+ Object.defineProperty(exports, "IfFeatureEnabled", {
15
+ enumerable: true,
16
+ get: () => growthbookReact.IfFeatureEnabled
17
+ });
18
+ Object.defineProperty(exports, "useExperiment", {
19
+ enumerable: true,
20
+ get: () => growthbookReact.useExperiment
21
+ });
22
+ Object.defineProperty(exports, "useFeature", {
23
+ enumerable: true,
24
+ get: () => growthbookReact.useFeature
25
+ });
26
+ Object.defineProperty(exports, "useFeatureIsOn", {
27
+ enumerable: true,
28
+ get: () => growthbookReact.useFeatureIsOn
29
+ });
30
+ Object.defineProperty(exports, "useFeatureValue", {
31
+ enumerable: true,
32
+ get: () => growthbookReact.useFeatureValue
33
+ });
34
+ Object.defineProperty(exports, "useGrowthBook", {
35
+ enumerable: true,
36
+ get: () => growthbookReact.useGrowthBook
37
+ });
38
+ Object.defineProperty(exports, "useGrowthBookSSR", {
39
+ enumerable: true,
40
+ get: () => growthbookReact.useGrowthBookSSR
41
+ });
42
+ Object.defineProperty(exports, "withRunExperiment", {
43
+ enumerable: true,
44
+ get: () => growthbookReact.withRunExperiment
45
+ });
46
+ exports.useAbTestAttributes = useAbTestAttributes.useAbTestAttributes;
47
+ exports.AbTestProvider = AbTestProvider.AbTestProvider;
package/dist/index.d.ts CHANGED
@@ -1,29 +1,5 @@
1
- import { Context, GrowthBook } from '@growthbook/growthbook-react';
2
- export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, withRunExperiment } from '@growthbook/growthbook-react';
3
- import * as react from 'react';
4
- import { ReactNode } from 'react';
5
-
6
- type Attributes = Record<string, string | number | undefined>;
7
- /**
8
- * @param {boolean} [autoRefresh] - false.
9
- * @param {number} [timeout] - 500.
10
- * @param {boolean} [skipCache] - false.
11
- */
12
- type LoadConfig = NonNullable<Parameters<GrowthBook['loadFeatures']>[0]>;
13
- type ToolConfig = Pick<Context, 'apiHost' | 'clientKey' | 'enableDevMode' | 'backgroundSync' | 'subscribeToChanges'>;
14
- type TrackingCallback = NonNullable<Context['trackingCallback']>;
15
- type ErrorCallback = (error: Error | string) => void | null;
16
-
17
- declare const useAbTestAttributes: () => [Attributes, (attributes: Attributes) => void];
18
-
19
- type AbTestProviderProps = {
20
- children: ReactNode;
21
- config: ToolConfig;
22
- trackingCallback: TrackingCallback;
23
- errorCallback: ErrorCallback;
24
- attributes: Attributes;
25
- loadConfig?: LoadConfig;
26
- };
27
- declare const AbTestProvider: ({ children, config, trackingCallback, errorCallback, attributes, loadConfig, }: AbTestProviderProps) => react.JSX.Element;
28
-
29
- export { AbTestProvider, type ErrorCallback, type ToolConfig, type TrackingCallback, useAbTestAttributes };
1
+ export { useExperiment, useFeature, withRunExperiment, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, } from '@growthbook/growthbook-react';
2
+ export { FeatureString, FeaturesReady, IfFeatureEnabled, } from '@growthbook/growthbook-react';
3
+ export { useAbTestAttributes } from './useAbTestAttributes';
4
+ export { AbTestProvider } from './AbTestProvider';
5
+ export type { TrackingCallback, ErrorCallback, ToolConfig } from './types';
package/dist/index.js CHANGED
@@ -1,3 +1,17 @@
1
- export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, withRunExperiment } from '@growthbook/growthbook-react';
2
- export { useAbTestAttributes } from './useAbTestAttributes.js';
3
- export { AbTestProvider } from './AbTestProvider.js';
1
+ import { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, withRunExperiment } from "@growthbook/growthbook-react";
2
+ import { useAbTestAttributes } from "./useAbTestAttributes.js";
3
+ import { AbTestProvider } from "./AbTestProvider.js";
4
+ export {
5
+ AbTestProvider,
6
+ FeatureString,
7
+ FeaturesReady,
8
+ IfFeatureEnabled,
9
+ useAbTestAttributes,
10
+ useExperiment,
11
+ useFeature,
12
+ useFeatureIsOn,
13
+ useFeatureValue,
14
+ useGrowthBook,
15
+ useGrowthBookSSR,
16
+ withRunExperiment
17
+ };
@@ -0,0 +1,11 @@
1
+ import type { Context, GrowthBook } from '@growthbook/growthbook-react';
2
+ export type Attributes = Record<string, string | number | undefined>;
3
+ /**
4
+ * @param {boolean} [autoRefresh] - false.
5
+ * @param {number} [timeout] - 500.
6
+ * @param {boolean} [skipCache] - false.
7
+ */
8
+ export type LoadConfig = NonNullable<Parameters<GrowthBook['loadFeatures']>[0]>;
9
+ export type ToolConfig = Pick<Context, 'apiHost' | 'clientKey' | 'enableDevMode' | 'backgroundSync' | 'subscribeToChanges'>;
10
+ export type TrackingCallback = NonNullable<Context['trackingCallback']>;
11
+ export type ErrorCallback = (error: Error | string) => void | null;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const growthbookReact = require("@growthbook/growthbook-react");
4
+ const useAbTestAttributes = () => {
5
+ const growthBook = growthbookReact.useGrowthBook();
6
+ if (growthBook) {
7
+ const attributes = growthBook.getAttributes();
8
+ const setAttributes = (newAttributes) => growthBook.setAttributes({
9
+ ...attributes,
10
+ ...newAttributes
11
+ });
12
+ return [attributes, setAttributes];
13
+ }
14
+ return [{}, () => {
15
+ }];
16
+ };
17
+ exports.useAbTestAttributes = useAbTestAttributes;
@@ -0,0 +1,5 @@
1
+ import type { Attributes } from './types';
2
+ export declare const useAbTestAttributes: () => [
3
+ Attributes,
4
+ (attributes: Attributes) => void
5
+ ];
@@ -1,16 +1,17 @@
1
- import { useGrowthBook } from '@growthbook/growthbook-react';
2
-
1
+ import { useGrowthBook } from "@growthbook/growthbook-react";
3
2
  const useAbTestAttributes = () => {
4
3
  const growthBook = useGrowthBook();
5
4
  if (growthBook) {
6
5
  const attributes = growthBook.getAttributes();
7
- const setAttributes = newAttributes => growthBook.setAttributes({
6
+ const setAttributes = (newAttributes) => growthBook.setAttributes({
8
7
  ...attributes,
9
8
  ...newAttributes
10
9
  });
11
10
  return [attributes, setAttributes];
12
11
  }
13
- return [{}, () => {}];
12
+ return [{}, () => {
13
+ }];
14
+ };
15
+ export {
16
+ useAbTestAttributes
14
17
  };
15
-
16
- export { useAbTestAttributes };
package/package.json CHANGED
@@ -1,16 +1,21 @@
1
1
  {
2
2
  "name": "@scaleway/use-growthbook",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Utility package to expose AB test tool",
5
- "type": "module",
6
5
  "engines": {
7
6
  "node": ">=20.x"
8
7
  },
8
+ "sideEffects": false,
9
+ "type": "module",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
9
12
  "exports": {
10
- "types": "./dist/index.d.ts",
11
- "default": "./dist/index.js"
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "require": "./dist/index.cjs",
16
+ "default": "./dist/index.js"
17
+ }
12
18
  },
13
- "sideEffects": false,
14
19
  "publishConfig": {
15
20
  "access": "public"
16
21
  },
@@ -32,5 +37,14 @@
32
37
  },
33
38
  "peerDependencies": {
34
39
  "react": "18.x"
40
+ },
41
+ "scripts": {
42
+ "typecheck": "tsc --noEmit",
43
+ "type:generate": "tsc --declaration -p tsconfig.build.json",
44
+ "build": "vite build --config vite.config.ts && pnpm run type:generate",
45
+ "build:profile": "npx vite-bundle-visualizer -c vite.config.ts",
46
+ "lint": "eslint --report-unused-disable-directives --cache --cache-strategy content --ext ts,tsx .",
47
+ "test:unit": "vitest --run --config vite.config.ts",
48
+ "test:unit:coverage": "pnpm test:unit --coverage"
35
49
  }
36
50
  }
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": false,
5
+ "emitDeclarationOnly": true,
6
+ "rootDir": "src",
7
+ "outDir": "dist"
8
+ },
9
+ "exclude": [
10
+ "*.config.ts",
11
+ "*.setup.ts",
12
+ "**/__tests__",
13
+ "**/__mocks__",
14
+ "src/**/*.test.tsx"
15
+ ]
16
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["src/**/*.ts", "src/**/*.tsx", "*.config.ts"]
4
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { defineConfig, mergeConfig } from 'vite'
2
+ import { defaultConfig } from '../../vite.config'
3
+
4
+ export default mergeConfig(defineConfig(defaultConfig), {
5
+ build: {
6
+ lib: {
7
+ formats: ['es', 'cjs'],
8
+ },
9
+ },
10
+ test: {
11
+ environment: 'jsdom',
12
+ setupFiles: ['./vitest.setup.ts'],
13
+ },
14
+ })
@@ -0,0 +1,12 @@
1
+ import * as matchers from '@testing-library/jest-dom/matchers'
2
+ import '@testing-library/jest-dom/vitest'
3
+ import { cleanup } from '@testing-library/react'
4
+ import { afterEach, expect, vi } from 'vitest'
5
+
6
+ expect.extend(matchers)
7
+
8
+ afterEach(() => {
9
+ cleanup()
10
+ })
11
+
12
+ vi.mock('@growthbook/growthbook-react')