Skip to content

Commit

Permalink
Merge pull request #126 from ltonetwork/esm-module
Browse files Browse the repository at this point in the history
Build project as both ESM and CommonJS
  • Loading branch information
jasny authored Jul 21, 2023
2 parents c8152de + ff40531 commit fe4e5f4
Show file tree
Hide file tree
Showing 61 changed files with 353 additions and 306 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dist/
node_modules/
lib/
node_modules/
tmp-browser/
tmp-node/
test.ts
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const lto = new LTO('T');
const account = lto.account();

const seed = 'satisfy sustain shiver skill betray mother appear pupil coconut weasel firm top puzzle monkey seek';
const accountFromSeed = lto.account({seed: seed});
const accountFromSeed = lto.account({ seed });

lto.transfer(account, recipient, 100_00000000);
lto.massTransfer(account, [{recipient: recipient1, amount: 100_00000000}, {recipient: recipient2, amount: 50_00000000}]);
Expand Down Expand Up @@ -60,31 +60,28 @@ useful if you want to use the library in a browser environment.

You can download the bundle from the [GitHub releases page](https://github.com/ltonetwork/lto-api.js/releases).

The library is bundles as a UMD module. This means you can use it in the browser as a global variable, or you can
The library is bundles as a UMD module. This means you can use it in the browser as global variable `LTO`, or you can
import it as a module in your JavaScript code.

### Browser

```html
<script src="lto.js"></script>
<script>
const { LTO } = window.LTO;
const lto = new LTO('T');
const lto = LTO.connect('T');
const account = lto.account();
console.log(account.address);
lto.anchor(account, new LTO.Binary('test').hash())
.then(tx => console.log(tx.id));
</script>
```

### Troubleshooting

Global variable `LTO` is an object with all exported classes and functions. It contains the `LTO` class,
which is the main class of the library. If you try to do `new LTO()` you will get the error:
Global variable `LTO` is an object with all exported classes and functions. If you try to do `new LTO()` you will get
the error:

TypeError: LTO is not a constructor

Make sure you do

```js
const { LTO } = window.LTO;
```
Use `LTO.connect()` instead.
86 changes: 0 additions & 86 deletions bin/test-lto-identity-builder.js

This file was deleted.

140 changes: 0 additions & 140 deletions interfaces.d.ts

This file was deleted.

66 changes: 63 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,80 @@
"scripts": {
"clean": "rm -rf ./lib ./dist",
"build": "webpack --mode production",
"compile": "tsc -p ./tsconfig.json",
"precompile": "rm -rf ./lib",
"compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json",
"postcompile": "node ./scripts/esm-postcompile.js ./lib/esm",
"test": "mocha --require ts-node/register 'test/**/*.spec.ts'",
"lint": "eslint src --ext .ts,.tsx",
"lint-fix": "eslint src --ext .ts,.tsx --fix",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
},
"author": "LTO Network",
"license": "MIT",
"main": "lib/index.js",
"files": [
"lib",
"src",
"interfaces.d.ts"
"src/types/interfaces.d.ts"
],
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js",
"default": "./lib/esm/index.js"
},
"./accounts": {
"types": "./lib/types/accounts/index.d.ts",
"require": "./lib/cjs/accounts/index.js",
"import": "./lib/esm/accounts/index.js",
"default": "./lib/esm/accounts/index.js"
},
"./errors": {
"types": "./lib/types/errors/index.d.ts",
"require": "./lib/cjs/errors/index.js",
"import": "./lib/esm/errors/index.js",
"default": "./lib/esm/errors/index.js"
},
"./events": {
"types": "./lib/types/events/index.d.ts",
"require": "./lib/cjs/events/index.js",
"import": "./lib/esm/events/index.js",
"default": "./lib/esm/events/index.js"
},
"./identities": {
"types": "./lib/types/identities/index.d.ts",
"require": "./lib/cjs/identities/index.js",
"import": "./lib/esm/identities/index.js",
"default": "./lib/esm/identities/index.js"
},
"./messages": {
"types": "./lib/types/messages/index.d.ts",
"require": "./lib/cjs/messages/index.js",
"import": "./lib/esm/messages/index.js",
"default": "./lib/esm/messages/index.js"
},
"./node": {
"types": "./lib/types/node/index.d.ts",
"require": "./lib/cjs/node/index.js",
"import": "./lib/esm/node/index.js",
"default": "./lib/esm/node/index.js"
},
"./transactions": {
"types": "./lib/types/transactions/index.d.ts",
"require": "./lib/cjs/transactions/index.js",
"import": "./lib/esm/transactions/index.js",
"default": "./lib/esm/transactions/index.js"
},
"./utils": {
"types": "./lib/types/utils/index.d.ts",
"require": "./lib/cjs/utils/index.js",
"import": "./lib/esm/utils/index.js",
"default": "./lib/esm/utils/index.js"
},
"./types": "./lib/types/index.d.ts",
"./constants": "./lib/constants.js",
"./package.json": "./package.json"
},
"dependencies": {
"@noble/curves": "^1.0.0",
"@noble/hashes": "^1.3.0",
Expand Down
Loading

0 comments on commit fe4e5f4

Please sign in to comment.