@scaleway/use-growthbook 2.0.3 → 2.1.0

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.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1960](https://github.com/scaleway/scaleway-lib/pull/1960) [`d034b3c`](https://github.com/scaleway/scaleway-lib/commit/d034b3cda1cac30ce2ed4e95be5a2c79642f8ca4) Thanks [@Slashgear](https://github.com/Slashgear)! - add legacy main attribut for CommonJS export usage
8
+
9
+ ## 2.0.4
10
+
11
+ ### Patch Changes
12
+
13
+ - [#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
14
+
3
15
  ## 2.0.3
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;
@@ -1,50 +1,59 @@
1
- import { jsx as h } from "react/jsx-runtime";
2
- import { GrowthBookProvider as m, GrowthBook as a } from "@growthbook/growthbook-react";
3
- import { useMemo as w, useCallback as d, useEffect as i } from "react";
4
- const p = {
5
- autoRefresh: !1,
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { GrowthBookProvider, GrowthBook } from "@growthbook/growthbook-react";
3
+ import { useMemo, useCallback, useEffect } from "react";
4
+ const defaultLoadConfig = {
5
+ autoRefresh: false,
6
6
  timeout: 500,
7
- skipCache: !1
8
- }, A = ({
7
+ skipCache: false
8
+ };
9
+ const getGrowthBookInstance = ({
9
10
  config: {
10
- apiHost: c,
11
- clientKey: t,
12
- enableDevMode: r,
13
- backgroundSync: e,
14
- subscribeToChanges: s
11
+ apiHost,
12
+ clientKey,
13
+ enableDevMode,
14
+ backgroundSync,
15
+ subscribeToChanges
15
16
  },
16
- trackingCallback: u
17
- }) => new a({
18
- apiHost: c,
19
- clientKey: t,
20
- enableDevMode: r,
21
- trackingCallback: u,
22
- backgroundSync: e,
23
- subscribeToChanges: s
24
- }), x = ({
25
- children: c,
26
- config: t,
27
- trackingCallback: r,
28
- errorCallback: e,
29
- attributes: s,
30
- loadConfig: u
17
+ trackingCallback
18
+ }) => new GrowthBook({
19
+ apiHost,
20
+ clientKey,
21
+ enableDevMode,
22
+ trackingCallback,
23
+ backgroundSync,
24
+ subscribeToChanges
25
+ });
26
+ const AbTestProvider = ({
27
+ children,
28
+ config,
29
+ trackingCallback,
30
+ errorCallback,
31
+ attributes,
32
+ loadConfig
31
33
  }) => {
32
- const o = w(
33
- () => A({ config: t, trackingCallback: r }),
34
- [r, t]
35
- ), n = d(async () => {
36
- t.clientKey && await o.loadFeatures(u ?? p);
37
- }, [o, t, u]);
38
- return i(() => {
39
- n().catch(e);
40
- }, [n, e]), i(() => {
41
- const f = o.getAttributes();
42
- f !== s && o.setAttributes({
43
- ...f,
44
- ...s
45
- }).catch(e);
46
- }, [s, o, e]), /* @__PURE__ */ h(m, { growthbook: o, children: c });
34
+ const growthbook = useMemo(
35
+ () => getGrowthBookInstance({ config, trackingCallback }),
36
+ [trackingCallback, config]
37
+ );
38
+ const loadFeature = useCallback(async () => {
39
+ if (config.clientKey) {
40
+ await growthbook.loadFeatures(loadConfig ?? defaultLoadConfig);
41
+ }
42
+ }, [growthbook, config, loadConfig]);
43
+ useEffect(() => {
44
+ loadFeature().catch(errorCallback);
45
+ }, [loadFeature, errorCallback]);
46
+ useEffect(() => {
47
+ const currentAttributes = growthbook.getAttributes();
48
+ if (currentAttributes !== attributes) {
49
+ growthbook.setAttributes({
50
+ ...currentAttributes,
51
+ ...attributes
52
+ }).catch(errorCallback);
53
+ }
54
+ }, [attributes, growthbook, errorCallback]);
55
+ return /* @__PURE__ */ jsx(GrowthBookProvider, { growthbook, children });
47
56
  };
48
57
  export {
49
- x as AbTestProvider
58
+ AbTestProvider
50
59
  };
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.js CHANGED
@@ -1,17 +1,17 @@
1
- import { FeatureString as t, FeaturesReady as u, IfFeatureEnabled as o, useExperiment as s, useFeature as a, useFeatureIsOn as i, useFeatureValue as n, useGrowthBook as F, useGrowthBookSSR as m, withRunExperiment as p } from "@growthbook/growthbook-react";
2
- import { useAbTestAttributes as b } from "./useAbTestAttributes.js";
3
- import { AbTestProvider as d } 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
4
  export {
5
- d as AbTestProvider,
6
- t as FeatureString,
7
- u as FeaturesReady,
8
- o as IfFeatureEnabled,
9
- b as useAbTestAttributes,
10
- s as useExperiment,
11
- a as useFeature,
12
- i as useFeatureIsOn,
13
- n as useFeatureValue,
14
- F as useGrowthBook,
15
- m as useGrowthBookSSR,
16
- p as withRunExperiment
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
17
  };
@@ -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;
@@ -1,16 +1,17 @@
1
- import { useGrowthBook as r } from "@growthbook/growthbook-react";
2
- const i = () => {
3
- const t = r();
4
- if (t) {
5
- const e = t.getAttributes();
6
- return [e, (s) => t.setAttributes({
7
- ...e,
8
- ...s
9
- })];
1
+ import { useGrowthBook } from "@growthbook/growthbook-react";
2
+ const useAbTestAttributes = () => {
3
+ const growthBook = useGrowthBook();
4
+ if (growthBook) {
5
+ const attributes = growthBook.getAttributes();
6
+ const setAttributes = (newAttributes) => growthBook.setAttributes({
7
+ ...attributes,
8
+ ...newAttributes
9
+ });
10
+ return [attributes, setAttributes];
10
11
  }
11
12
  return [{}, () => {
12
13
  }];
13
14
  };
14
15
  export {
15
- i as useAbTestAttributes
16
+ useAbTestAttributes
16
17
  };
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@scaleway/use-growthbook",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "Utility package to expose AB test tool",
5
5
  "engines": {
6
6
  "node": ">=20.x"
7
7
  },
8
+ "main": "./dist/index.cjs",
8
9
  "sideEffects": false,
9
10
  "type": "module",
10
11
  "module": "./dist/index.js",
@@ -12,6 +13,7 @@
12
13
  "exports": {
13
14
  ".": {
14
15
  "types": "./dist/index.d.ts",
16
+ "require": "./dist/index.cjs",
15
17
  "default": "./dist/index.js"
16
18
  }
17
19
  },
@@ -32,7 +34,7 @@
32
34
  "@growthbook/growthbook-react": "0.26.0"
33
35
  },
34
36
  "devDependencies": {
35
- "react": "18.2.0"
37
+ "react": "18.3.1"
36
38
  },
37
39
  "peerDependencies": {
38
40
  "react": "18.x"
@@ -42,6 +44,8 @@
42
44
  "type:generate": "tsc --declaration -p tsconfig.build.json",
43
45
  "build": "vite build --config vite.config.ts && pnpm run type:generate",
44
46
  "build:profile": "npx vite-bundle-visualizer -c vite.config.ts",
45
- "lint": "eslint --report-unused-disable-directives --cache --cache-strategy content --ext ts,tsx ."
47
+ "lint": "eslint --report-unused-disable-directives --cache --cache-strategy content --ext ts,tsx .",
48
+ "test:unit": "vitest --run --config vite.config.ts",
49
+ "test:unit:coverage": "pnpm test:unit --coverage"
46
50
  }
47
51
  }
package/vite.config.ts CHANGED
@@ -1,6 +1,14 @@
1
- /* eslint-disable eslint-comments/disable-enable-pair */
2
- /* eslint-disable import/no-relative-packages */
3
1
  import { defineConfig, mergeConfig } from 'vite'
4
2
  import { defaultConfig } from '../../vite.config'
5
3
 
6
- export default mergeConfig(defineConfig(defaultConfig), {})
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')