Skip to content

Commit 434b4e5

Browse files
mikechu-optimizelyMike Chu
andauthored
[FSSDK-9494] Quality of life updates picked from #210 (#212)
* Quality of life updates picked from #210 * Add extra EOF lf * Correct dep versions * Lint fixes * Upgrade tough-cookie for CVE fix * Remove direct dep [email protected] --------- Co-authored-by: Mike Chu <[email protected]>
1 parent 4d01f0d commit 434b4e5

File tree

9 files changed

+168
-134
lines changed

9 files changed

+168
-134
lines changed

.devcontainer/devcontainer.json

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
1-
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2-
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
31
{
4-
"name": "React SDK",
5-
6-
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
7-
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18-bullseye",
2+
"name": "React SDK",
83

9-
// Features to add to the dev container. More info: https://containers.dev/features.
10-
// "features": {},
4+
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18-bullseye",
115

12-
// Use 'forwardPorts' to make a list of ports inside the container available locally.
13-
// "forwardPorts": [],
6+
"postCreateCommand": "npm install -g npm && yarn install",
147

15-
// Use 'postCreateCommand' to run commands after the container is created.
16-
"postCreateCommand": "npm install -g npm && yarn install",
17-
18-
// Configure tool-specific properties.
19-
"customizations": {
20-
"vscode": {
21-
"extensions": [
22-
"dbaeumer.vscode-eslint",
23-
"eamodio.gitlens",
24-
"esbenp.prettier-vscode",
25-
"Gruntfuggly.todo-tree",
26-
"github.vscode-github-actions",
27-
"Orta.vscode-jest",
28-
"ms-vscode.test-adapter-converter"
29-
]
30-
}
31-
}
32-
33-
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
34-
// "remoteUser": "root"
8+
"customizations": {
9+
"vscode": {
10+
"extensions": [
11+
"dbaeumer.vscode-eslint",
12+
"eamodio.gitlens",
13+
"esbenp.prettier-vscode",
14+
"Gruntfuggly.todo-tree",
15+
"github.vscode-github-actions",
16+
"Orta.vscode-jest",
17+
"ms-vscode.test-adapter-converter"
18+
],
19+
"settings": {
20+
"files.eol": "\n"
21+
}
22+
}
23+
}
3524
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ lib
77
dist/
88
build/
99
.rpt2_cache
10+
.env

.vscode/settings.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"jest.autoRun": {
3-
"onStartup": [
4-
"all-tests"
5-
]
6-
},
7-
}
2+
"jest.autoRun": {
3+
"onStartup": ["all-tests"]
4+
},
5+
"jest.jestCommandLine": "./node_modules/.bin/jest",
6+
"jest.autoRevealOutput": "on-exec-error"
7+
}

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,44 @@ const WrappedMyComponent = withOptimizely(MyComp);
286286

287287
**_Note:_** The `optimizely` client object provided via `withOptimizely` is automatically associated with the `user` prop passed to the ancestor `OptimizelyProvider` - the `id` and `attributes` from that `user` object will be automatically forwarded to all appropriate SDK method calls. So, there is no need to pass the `userId` or `attributes` arguments when calling methods of the `optimizely` client object, unless you wish to use _different_ `userId` or `attributes` than those given to `OptimizelyProvider`.
288288

289+
## `useContext`
290+
291+
Any component under the `<OptimizelyProvider>` can access the Optimizely `ReactSDKClient` via the `OptimizelyContext` with `useContext`.
292+
293+
_arguments_
294+
- `OptimizelyContext : React.Context<OptimizelyContextInterface>` The Optimizely context initialized in a parent component (or App).
295+
296+
_returns_
297+
- Wrapped object:
298+
- `optimizely : ReactSDKClient` The client object which was passed to the `OptimizelyProvider`
299+
- `isServerSide : boolean` Value that was passed to the `OptimizelyProvider`
300+
- `timeout : number | undefined` The timeout which was passed to the `OptimizelyProvider`
301+
302+
### Example
303+
304+
```jsx
305+
import React, { useContext } from 'react';
306+
import { OptimizelyContext } from '@optimizely/react-sdk';
307+
308+
function MyComponent() {
309+
const { optimizely, isServerSide, timeout } = useContext(OptimizelyContext);
310+
const decision = optimizely.decide('my-feature');
311+
const onClick = () => {
312+
optimizely.track('signup-clicked');
313+
// rest of your click handling code
314+
};
315+
return (
316+
<>
317+
{ decision.enabled && <p>My feature is enabled</p> }
318+
{ !decision.enabled && <p>My feature is disabled</p> }
319+
{ decision.variationKey === 'control-variation' && <p>Current Variation</p> }
320+
{ decision.variationKey === 'experimental-variation' && <p>Better Variation</p> }
321+
<button onClick={onClick}>Sign Up!</button>
322+
</>
323+
);
324+
}
325+
```
326+
289327
### Tracking
290328

291329
Use the `withOptimizely` HoC for tracking.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.9.2",
44
"description": "React SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts",
55
"homepage": "https://github.com/optimizely/react-sdk",
6+
"repository": "https://github.com/optimizely/react-sdk",
67
"license": "Apache-2.0",
78
"module": "dist/react-sdk.es.js",
89
"types": "dist/index.d.ts",
@@ -33,7 +34,7 @@
3334
"access": "public"
3435
},
3536
"dependencies": {
36-
"@optimizely/optimizely-sdk": "^5.0.0-beta2",
37+
"@optimizely/optimizely-sdk": "5.0.0-beta2",
3738
"hoist-non-react-statics": "^3.3.0",
3839
"prop-types": "^15.6.2",
3940
"utility-types": "^2.1.0 || ^3.0.0"

scripts/build.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019, 2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,34 +14,35 @@
1414
* limitations under the License.
1515
*/
1616

17-
const fs = require("fs");
18-
const path = require("path");
19-
const execSync = require("child_process").execSync;
17+
const path = require('path');
18+
const execSync = require('child_process').execSync;
2019

21-
process.chdir(path.resolve(__dirname, ".."));
20+
process.chdir(path.resolve(__dirname, '..'));
2221

2322
function exec(command, extraEnv) {
2423
return execSync(command, {
25-
stdio: "inherit",
26-
env: Object.assign({}, process.env, extraEnv)
24+
stdio: 'inherit',
25+
env: Object.assign({}, process.env, extraEnv),
2726
});
2827
}
2928

3029
const packageName = 'react-sdk';
31-
const umdName = 'optimizelyReactSdk'
32-
33-
console.log("\nBuilding ES modules...");
30+
const umdName = 'optimizelyReactSdk';
3431

32+
console.log('\nBuilding ES modules...');
3533
exec(`./node_modules/.bin/rollup -c scripts/config.js -f es -o dist/${packageName}.es.js`);
3634

37-
console.log("\nBuilding CommonJS modules...");
38-
35+
console.log('\nBuilding CommonJS modules...');
3936
exec(`./node_modules/.bin/rollup -c scripts/config.js -f cjs -o dist/${packageName}.js`);
4037

41-
console.log("\nBuilding UMD modules...");
42-
43-
exec(`./node_modules/.bin/rollup -c scripts/config.js -f umd -o dist/${packageName}.umd.js --name ${umdName}`, {EXTERNALS: 'forBrowsers', BUILD_ENV: 'production' });
44-
45-
console.log("\nBuilding SystemJS modules...");
46-
47-
exec(`./node_modules/.bin/rollup -c scripts/config.js -f system -o dist/${packageName}.system.js`, {EXTERNALS: 'forBrowsers', BUILD_ENV: 'production' });
38+
console.log('\nBuilding UMD modules...');
39+
exec(`./node_modules/.bin/rollup -c scripts/config.js -f umd -o dist/${packageName}.umd.js --name ${umdName}`, {
40+
EXTERNALS: 'forBrowsers',
41+
BUILD_ENV: 'production',
42+
});
43+
44+
console.log('\nBuilding SystemJS modules...');
45+
exec(`./node_modules/.bin/rollup -c scripts/config.js -f system -o dist/${packageName}.system.js`, {
46+
EXTERNALS: 'forBrowsers',
47+
BUILD_ENV: 'production',
48+
});

scripts/config.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019, 2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,23 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
const typescript = require('rollup-plugin-typescript2')
18-
const commonjs = require('@rollup/plugin-commonjs')
19-
const replace = require('@rollup/plugin-replace')
20-
const { nodeResolve } = require('@rollup/plugin-node-resolve')
21-
const { uglify } = require('rollup-plugin-uglify')
17+
const typescript = require('rollup-plugin-typescript2');
18+
const commonjs = require('@rollup/plugin-commonjs');
19+
const replace = require('@rollup/plugin-replace');
20+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
21+
const { uglify } = require('rollup-plugin-uglify');
2222

23-
const packageDeps = require('../package.json').dependencies || {}
24-
const packagePeers = require('../package.json').peerDependencies || {}
23+
const packageDeps = require('../package.json').dependencies || {};
24+
const packagePeers = require('../package.json').peerDependencies || {};
2525

2626
function getExternals(externals) {
2727
let externalLibs;
28-
if(externals === 'forBrowsers') {
29-
externalLibs = ['react']
28+
if (externals === 'forBrowsers') {
29+
externalLibs = ['react'];
3030
} else {
31-
externalLibs = (externals === 'peers')
32-
? Object.keys(packagePeers)
33-
: Object.keys(packageDeps).concat(Object.keys(packagePeers))
31+
externalLibs =
32+
externals === 'peers' ? Object.keys(packagePeers) : Object.keys(packageDeps).concat(Object.keys(packagePeers));
3433
}
3534
externalLibs.push('crypto');
3635
return externalLibs;
@@ -40,40 +39,40 @@ function getPlugins(env, externals) {
4039
const plugins = [
4140
nodeResolve({
4241
browser: externals === 'forBrowsers',
43-
preferBuiltins: externals !== 'forBrowsers'
42+
preferBuiltins: externals !== 'forBrowsers',
4443
}),
4544
commonjs({
4645
include: /node_modules/,
4746
}),
48-
]
47+
];
4948

5049
if (env) {
5150
plugins.push(
5251
replace({
5352
'process.env.NODE_ENV': JSON.stringify(env),
54-
}),
55-
)
53+
})
54+
);
5655
}
5756

58-
plugins.push(typescript())
57+
plugins.push(typescript());
5958

6059
if (env === 'production') {
61-
plugins.push(uglify())
60+
plugins.push(uglify());
6261
}
6362

64-
return plugins
63+
return plugins;
6564
}
6665

6766
const config = {
6867
input: 'src/index.ts',
6968
output: {
7069
globals: {
7170
react: 'React',
72-
crypto: 'crypto'
71+
crypto: 'crypto',
7372
},
7473
},
7574
external: getExternals(process.env.EXTERNALS),
7675
plugins: getPlugins(process.env.BUILD_ENV, process.env.EXTERNALS),
77-
}
76+
};
7877

79-
module.exports = config
78+
module.exports = config;

0 commit comments

Comments
 (0)