Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Use ES Modules over CommonJS. #103

Merged
merged 1 commit into from
Sep 18, 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
25 changes: 25 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

## Migrating from v3 to v4

v4 changes the default export to the constructor. It also uses [ES Modules over CommonJS](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) syntax. We recommend to include `ftdomdelegate` using the es modules syntax.

If you used the `.Delegate` constructure update your import:

```diff
-const Delegate = require('ftdomdelegate').Delegate;
+import Delegate from 'ftdomdelegate';
let myDel = new Delegate(document.body);
```

If you used the previous default export, also update to use the constructor:
```diff
-const delegate = require('ftdomdelegate');
-let myDel = delegate(document.body);
+import Delegate from 'ftdomdelegate';
+let myDel = new Delegate(document.body);
```

However to use the CommonJS syntax, without a plugin like [babel-plugin-transform-es2015-modules-commonjs](https://babeljs.io/docs/en/babel-plugin-transform-es2015-modules-commonjs), add `.default`.

```diff
-const Delegate = require('ftdomdelegate').Delegate;
+const Delegate = require('ftdomdelegate').default;
let myDel = new Delegate(document.body);
```

## Migrating from v2 to v3

Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,16 @@ The easiest way is to include the following script tag and let [Polyfill.io](htt
<script src="https://cdn.polyfill.io/v2/polyfill.js?features=Event,Array.prototype.map,Function.prototype.bind,document.querySelector,Element.prototype.matches"></script>
```

## Usage ##
## Usage

The library is written in CommonJS and so can be `require` in.
To import ftdomdelegate:

```js
// If requiring the module via CommonJS, either:-
Delegate = require('ftdomdelegate').Delegate;
myDel = new Delegate(document.body);

// Or:-
delegate = require('ftdomdelegate');
myDel = delegate(document.body);
import Delegate from 'ftdomdelegate';
let myDel = new Delegate(document.body);
```

The script must be loaded prior to instantiating a Delegate object.

To instantiate Delegate on the `body` and listen to some events:
To instantiate `Delegate` on the `body` and listen to some events:

```js
function handleButtonClicks(event) {
Expand All @@ -70,7 +63,6 @@ document.addEventListener('DOMContentLoaded', function() {
// Listen to all touch move
// events that reach the body
delegate.on('touchmove', handleTouchMove);

});
```

Expand Down
Loading