Skip to content

Commit

Permalink
Merge pull request #68 from ibmtjbot/dev
Browse files Browse the repository at this point in the history
new packaging as a hybrid ES6 / CommonJS module
  • Loading branch information
jweisz authored Oct 21, 2021
2 parents 815e390 + 105e150 commit 4fe0263
Show file tree
Hide file tree
Showing 19 changed files with 4,527 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"extends": [
"airbnb-base"
],
"ignorePatterns": ["dist/**"],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 12,
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ out

# Nuxt.js build / generate output
.nuxt
dist
#dist

# Gatsby files
.cache/
Expand Down
4 changes: 2 additions & 2 deletions .jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"dictionaries": ["jsdoc"]
},
"source": {
"include": ["lib", "package.json", "README.md"],
"include": ["src", "package.json", "README.md"],
"includePattern": ".js$",
"excludePattern": "(node_modules/|docs)"
"excludePattern": "(node_modules/|docs|dist|__tests__)"
},
"plugins": [
"plugins/markdown"
Expand Down
10 changes: 6 additions & 4 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
## Migrating from 1.x to 2.x
There have been several significant changes to the 2.x release of TJBotLib that break compatability with the 1.x release. Please use this guide, the new [TJBot API docs](https://ibmtjbot.github.io/docs/tjbot/2.0.1/), and the [updated recipes](https://github.com/ibmtjbot/tjbot/tree/master/recipes) in the `tjbot` repository, to help you migrate your recipes to the new 2.x API.

### ES6 Module
TJBot is now packaged as an ES6 module, meaning it can be imported as follows:
### ES6 / CommonJS Hybrid Module
TJBotLib is now packaged as hybrid ES6 / CommonJS module, meaning it can be imported as follows:

import TJBot from 'tjbot';
import TJBot from 'tjbot'; // ES6

Because of this new packaging, TJBotLib requires Node 15.x.
const TJBot = require('tjbot').default; // CommonJS

Node 15.x+ is required to use TJBotLib as an ES6 module.

### `async`/`await` Semantics
TJBot now uses `async`/`await` semantics rather than `Promise` semantics. This change has resulted in much cleaner, and easier to understand code. Thus, functions that used to return promises (e.g. `tj.analyzeTone()`) should now be called with `await`.
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@ $ npm install --save tjbot

> 💡 Note: The TJBot library was developed for use on Raspberry Pi. It may be possible to develop and test portions of this library on other Linux-based systems (e.g. Ubuntu), but this usage is not officially supported.
2. Instantiate the `TJBot` object.
2. Import the TJBot library.

TJBot is packaged as both an ES6 and a CommonJS module (explained in [this guide](https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html)), which means you may import it using either the ES6 `import` statement or the CommonJS `require` method.

For ES6, import TJBot as follows:

```
import TJBot from 'tjbot';
```

For CommonJS, import TJBot as follows:

```
const TJBot = require('tjbot').default;
```

> 💡 Note: For CommonJS, the `TJBot` class is exported under a `.default` reference.
3. Instantiate the `TJBot` object.

```
const tj = new TJBot();
tj.initialize([TJBot.HARDWARE.LED_NEOPIXEL, TJBot.HARDWARE.SERVO, TJBot.HARDWARE.MICROPHONE, TJBot.HARDWARE.SPEAKER]);
```
Expand Down Expand Up @@ -123,7 +140,7 @@ The full list of capabilities can be accessed programatically via `TJBot.CAPABIL

## TJBot API

Please see [the API docs](https://ibmtjbot.github.io/docs/tjbot/2.0.1/) for documentation of the TJBot API.
Please see [the API docs](https://ibmtjbot.github.io/docs/tjbot/2.0.2/) for documentation of the TJBot API.

> 💡 Please see the [Migration Guide](MIGRATING.md) for guidance on migrating your code to the latest version of the TJBot API.
Expand Down
3 changes: 3 additions & 0 deletions dist/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Loading

0 comments on commit 4fe0263

Please sign in to comment.