Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a UMD build to support older environments #81

Merged
merged 5 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
yarn.lock
.nyc_output
umd.js
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
"node": ">=10"
},
"scripts": {
"build": "rollup index.js --format=umd --name=ky --file=umd.js",
"prepublishOnly": "npm run build",
"test": "xo && nyc ava && tsd-check && npm run cypress:run",
"cypress:open": "cypress open",
"cypress:run": "cypress run"
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"umd.js"
],
"keywords": [
"fetch",
Expand Down Expand Up @@ -54,6 +57,7 @@
"esm": "^3.0.84",
"node-fetch": "^2.3.0",
"nyc": "^13.1.0",
"rollup": "^1.1.0",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
},
Expand Down
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ With plain `fetch`, it would be:
})();
```

In environments that do not support `import`, you can load `ky` in [UMD format](https://medium.freecodecamp.org/anatomy-of-js-module-systems-and-building-libraries-fadcd8dbd0e). For example, using `require()`:

```js
const ky = require('ky/umd').default;
```

With the UMD version, it's also easy to use `ky` [without a bundler](#how-do-i-use-this-without-a-bundler-like-webpack) or module system.

## API

Expand Down Expand Up @@ -328,6 +335,21 @@ import ky from 'https://cdn.jsdelivr.net/npm/ky@0.5.2/index.js';
</script>
```

Alternatively, you can use the [`umd.js`](umd.js) file with a traditional `<script>` tag (without `type="module"`), in which case `ky` will be a global.

```html
<!-- Replace the version number with the latest version -->
<script src="https://cdn.jsdelivr.net/npm/ky@0.5.2/umd.js">
<script>
(async () => {
const ky = ky.default;
const json = await ky('https://jsonplaceholder.typicode.com/todos/1').json();

console.log(json.title);
//=> 'delectus aut autem
})();
</script>
```

## Browser support

Expand Down