Skip to content

Commit

Permalink
Adapt documentation for ES module usage
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Sep 8, 2019
1 parent 412c661 commit c161e8e
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions README.md
Expand Up @@ -11,6 +11,7 @@ A simple, lightweight JavaScript API for handling cookies
* [Heavily](test) tested
* No dependency
* [Unobtrusive](#json) JSON support
* Supports ES modules
* Supports AMD/CommonJS
* [RFC 6265](https://tools.ietf.org/html/rfc6265) compliant
* Useful [Wiki](https://github.com/js-cookie/js-cookie/wiki)
Expand All @@ -22,35 +23,58 @@ A simple, lightweight JavaScript API for handling cookies

## Installation

#### NPM

JavaScript Cookie supports [npm](https://www.npmjs.com/package/js-cookie) under the name `js-cookie`.

```
$ npm install js-cookie --save
```

```javascript
import Cookies from "js.cookie.mjs";

Cookies.set('foo', 'bar');
```

### Direct download

Download the script [here](https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.js) and include it (unless you are packaging scripts somehow else):
The source comes as an ES module. If you download it [here](https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.mjs) directly, you must include it as such:

```html
<script src="/path/to/js.cookie.js"></script>
<script type="module" src="/path/to/js.cookie.mjs"></script>
```

Or include it via [jsDelivr CDN](https://www.jsdelivr.com/package/npm/js-cookie):
You can then import it:

```javascript
import Cookies from "/path/to/js.cookie.mjs";

Cookies.set('foo', 'bar');
```

*Not all browsers support ES modules natively yet*. For this reason the npm package/release
comes with both an ES module as well as an UMD module variant. Include the module along
with the fallback to account for this:

```html
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script type="module" src="/path/to/js.cookie.mjs"></script>
<script nomodule src="/path/to/js.cookie.js"></script>
```

**Do not include the script directly from GitHub (http://raw.github.com/...).** The file is being served as text/plain and as such being blocked
in Internet Explorer on Windows 7 for instance (because of the wrong MIME type). Bottom line: GitHub is not a CDN.
Note the different extensions: `.mjs` denotes an ES module.

### Package Managers
### CDN

JavaScript Cookie supports [npm](https://www.npmjs.com/package/js-cookie) under the name `js-cookie`.
Alternatively, include it via [jsDelivr CDN](https://www.jsdelivr.com/package/npm/js-cookie):

#### NPM
```
$ npm install js-cookie --save
```html
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
```

### Module Loaders

JavaScript Cookie can also be loaded as an AMD or CommonJS module.
**Never include the source directly from GitHub (http://raw.github.com/...).** The file
is being served as text/plain and as such may be blocked because of the wrong MIME type.
Bottom line: GitHub is not a CDN.

## Basic Usage

Expand Down Expand Up @@ -291,7 +315,7 @@ For vulnerability reports, send an e-mail to `jscookieproject at gmail dot com`
## Manual release steps

* Increment the "version" attribute of `package.json`
* Increment the version number in the `src/js.cookie.js` file
* Increment the version number in the `src/js.cookie.mjs` file
* If `major` bump, update jsDelivr CDN major version link on README
* Commit with the message "Release version x.x.x"
* Create version tag in git
Expand Down

0 comments on commit c161e8e

Please sign in to comment.