Skip to content

Commit b1d6891

Browse files
fix(build): Resume UMD bundle build, and restore docs (#152)
## Summary - Restore the `build-browser-umd` npm script, which produces dist/optimizely.browser.umd.min.js - Change the UMD lib name `optimizelyClient` -> `optimizelySdk`, since the `createInstance` client factory function may be one of several exports of the SDK, in the future. - (not a visible change) Change Markdown indentation of some bull points, to please my Markdown editor [Typora](https://typora.io/) This undoes one bit of #135. I hadn't realized that this asset _was_ documented, and does have some uses, like faster/easier prototyping in a browser. ## Test plan I made a foo.html with ```html <html> <head> <script src="./dist/optimizely.browser.umd.min.js"></script> <script> console.log('trying sdk'); window.optimizelySdk console.log('trying client'); window.optimizelyClient console.log('trying client'); window.optimizelyClient </script> </head> </html> ``` and it still works: ![screen shot 2018-08-20 at 2 59 47 pm](https://user-images.githubusercontent.com/20175438/44369024-b9d4b580-a489-11e8-9e2b-c8159917b68e.png)
1 parent 97b4aa2 commit b1d6891

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"version": "1.0.0",
44
"name": "optimizely-sdk-packages",
55
"scripts": {
6-
"postinstall": "lerna bootstrap",
6+
"postinstall": "lerna bootstrap && lerna run build",
7+
"build": "lerna run build",
78
"clean": "lerna run clean",
8-
"publish": "lerna publish",
9+
"publish": "npm run build && lerna publish",
910
"test": "lerna run test --stream",
1011
"test-xbrowser": "lerna run test-xbrowser --stream"
1112
},

packages/optimizely-sdk/CHANGELOG.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## Unreleased
8+
9+
### Deprecated
10+
* The UMD build of the SDK now assigns the SDK namespace object to `window.optimizelySdk` rather than to `window.optimizelyClient`. The old name still works, but on its first access a deprecation warning is logged to the console. The alias will be removed in the 3.0.0 release.
11+
112
## 2.1.1
213
June 19, 2018
314

packages/optimizely-sdk/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ This directory contains the source code for the JavaScript SDK, which is usable
1515
### Prerequisites
1616

1717
Ensure the SDK supports all of the platforms you're targeting. In particular, the SDK targets any ES5-compliant JavaScript environment. We officially support:
18-
- Node.js >= 4.0.0. By extension, environments like AWS Lambda, Google Cloud Functions, and Auth0 Webtasks are supported as well. Older Node.js releases likely work too (try `npm test` to validate for yourself), but are not formally supported.
19-
- [Web browsers](https://caniuse.com/#feat=es5)
18+
- Node.js >= 4.0.0. By extension, environments like AWS Lambda, Google Cloud Functions, and Auth0 Webtasks are supported as well. Older Node.js releases likely work too (try `npm test` to validate for yourself), but are not formally supported.
19+
- [Web browsers](https://caniuse.com/#feat=es5)
2020

2121
Other environments likely are compatible, too, but note that we don't officially support them:
22-
- Progressive Web Apps, WebViews, and hybrid mobile apps like those built with React Native and Apache Cordova.
23-
- [Cloudflare Workers](https://developers.cloudflare.com/workers/) and [Fly](https://fly.io/), both of which are powered by recent releases of V8.
24-
- Anywhere else you can think of that might embed a JavaScript engine. The sky is the limit; experiment everywhere! 🚀
22+
- Progressive Web Apps, WebViews, and hybrid mobile apps like those built with React Native and Apache Cordova.
23+
- [Cloudflare Workers](https://developers.cloudflare.com/workers/) and [Fly](https://fly.io/), both of which are powered by recent releases of V8.
24+
- Anywhere else you can think of that might embed a JavaScript engine. The sky is the limit; experiment everywhere! 🚀
2525

2626
Once you've validated that the SDK supports the platforms you're targeting, fetch the package from [NPM](https://www.npmjs.com/package/@optimizely/optimizely-sdk). Using `npm`:
2727

@@ -32,6 +32,14 @@ npm install --save @optimizely/optimizely-sdk
3232
### Usage
3333
See the Optimizely X Full Stack [developer documentation](http://developers.optimizely.com/server/reference/index.html) to learn how to set up your first JavaScript project and use the SDK.
3434

35+
The package's entry point is a CommonJS module, which can be used directly in environments which support it (e.g., Node.js, or loaded in a browser via Browserify or RequireJS). Additionally, you can include a standalone bundle of the SDK in your web page by fetching it from [unpkg](https://unpkg.com/):
36+
37+
```html
38+
<script src="https://unpkg.com/@optimizely/optimizely-sdk/dist/optimizely.browser.umd.js"></script>
39+
```
40+
41+
When evaluated, that bundle assigns the SDK's exports to `window.optimizelySdk`. If you wish to use the asset locally (for example, if unpkg is down), you can find it in your local copy of the package at dist/optimizely.browser.umd.min.js, or [here in GitHub](./dist/optimizely.browser.umd.min.js).
42+
3543
Regarding `EventDispatcher`s: In Node.js and browser environments, the default `EventDispatcher` is powered by the [`http/s`](https://nodejs.org/api/http.html) modules and by [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Browser_compatibility), respectively. In all other environments, you must supply your own `EventDispatcher`.
3644

3745
### Migrating from 1.x.x

packages/optimizely-sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"scripts": {
88
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive",
99
"test-xbrowser": "karma start karma.bs.conf.js --single-run",
10+
"build-browser-umd": "./scripts/build_browser_umd.sh",
1011
"lint": "eslint lib/**",
1112
"cover": "istanbul cover _mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js",
1213
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls",
13-
"prepublishOnly": "npm test && npm run test-xbrowser"
14+
"prepublishOnly": "npm test && npm run test-xbrowser && npm run build-browser-umd"
1415
},
1516
"repository": {
1617
"type": "git",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash -e
2+
3+
BUNDLE_NAME="optimizelySdk"
4+
5+
npx webpack -p lib/index.browser.js dist/optimizely.browser.umd.min.js \
6+
--output-library-target=umd \
7+
--output-library="$BUNDLE_NAME"
8+
9+
# Append some backwards-compatibility code to the bundle
10+
cat - >> dist/optimizely.browser.umd.min.js <<EOF
11+
12+
13+
window._optimizelyClientWarningGiven = false;
14+
15+
Object.defineProperty(window, 'optimizelyClient', {
16+
get: function () {
17+
if (!window._optimizelyClientWarningGiven) {
18+
console.warn('Accessing the SDK via window.optimizelyClient is deprecated; please use window.optimizelySdk instead. This alias will be dropped in 3.0.0.');
19+
window._optimizelyClientWarningGiven = true;
20+
}
21+
22+
return {
23+
createInstance: window.optimizelySdk.createInstance
24+
};
25+
}
26+
});
27+
EOF
28+

0 commit comments

Comments
 (0)