Skip to content

Commit

Permalink
Create a ESM build for node (#91)
Browse files Browse the repository at this point in the history
* Create a ESM build for node

It fixes the XMLHttpRequest is not defined error when "import"ing library from node.
We now have 2 ESM in the dist, one built for node, one build for the browser.
Quick reminder on why there are 4 files:

browser: archethic.js
browser import: archethic-browser.mjs
node import: archethic-node.mjs
node require: archethic.cjs

* Expose Archethic in the archethic.js

Add a small example to demonstrate how to use it

* Expose Archethic so we do not have to use .default in browser
  • Loading branch information
bchamagne committed Dec 22, 2022
1 parent 53c964a commit bb11acb
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 27 deletions.
21 changes: 20 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ esbuild.build({
define: {
global: 'window'
},
globalName: "archethic",
inject: ['./esbuild.inject.js'],
footer: {
js: "Archethic = archethic.default; Archethic.Utils = archethic.Utils; Archethic.Crypto = archethic.Crypto;"
}
}).catch(() => process.exit(1))

esbuild.build({
Expand All @@ -33,7 +37,22 @@ esbuild.build({
bundle: true,
minify: true,
sourcemap: true,
outfile: 'dist/archethic.mjs',
outfile: 'dist/archethic-browser.mjs',
inject: ['./esbuild.inject.js'],
format: 'esm'
}).catch(() => process.exit(1))

esbuild.build({
logLevel: "info",
entryPoints: ['index.js'],
bundle: true,
minify: true,
sourcemap: true,
outfile: 'dist/archethic-node.mjs',
inject: ['./esbuild.inject.js'],
format: 'esm',
platform: "node",
banner: {
js: "import { createRequire } from 'module';const require = createRequire(import.meta.url);"
},
}).catch(() => process.exit(1))
99 changes: 99 additions & 0 deletions dist/archethic-browser.mjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dist/archethic-browser.mjs.map

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions dist/archethic-node.mjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dist/archethic-node.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/archethic.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/archethic.cjs.map

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions dist/archethic.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/archethic.js.map

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions example/browserConnect/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">

<!-- This example demonstrates how to use the Archethic JavaScript SDK without any build system -->

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example: AE Browser Connect</title>
<!-- Load the SDK -->
<script src="../../dist/archethic.js"></script>
</head>

<body>
<h1>Connect to the Archethic via the JavaScript SDK</h1>

<form action="#" onsubmit="return connect(this);">
<label>Endpoint:<input name="endpoint" value="http://localhost:4000" /></label>
<button type="submit">Connect</button>
<span>
Status: <span id="status"></span>
</span>
</form>

<script type="text/javascript">
function displayStatus(status) {
document.getElementById("status").innerText = status
}

displayStatus("DISCONNECTED")

function connect(form) {
displayStatus("CONNECTING")

endpoint = form.querySelector("input[name=endpoint]").value

var promiseToConnect = new Archethic(endpoint).connect()

promiseToConnect
.then(() => {
displayStatus("CONNECTED")
})
.catch((a) => {
displayStatus(`DISCONNECTED (${a.message})`)
})

return false
}
</script>

</body>

</html>
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "archethic",
"version": "1.13.1",
"version": "1.13.2",
"description": "Archethic Javascript SDK",
"main": "index.js",
"module": "dist/archethic.mjs",
"module": "dist/archethic-browser.mjs",
"unpkg": "dist/archethic.js",
"jsdelivr": "dist/archethic.js",
"type": "module",
"exports": {
"import": "./dist/archethic.mjs",
"import": "./dist/archethic-node.mjs",
"require": "./dist/archethic.cjs"
},
"scripts": {
Expand Down Expand Up @@ -59,8 +59,9 @@
"ws": "^8.5.0"
},
"browser": {
"archethic": "./dist/archethic-browser.mjs",
"crypto": "crypto-browserify",
"stream": "stream-browserify",
"buffer-xor": "buffer-xor"
}
}
}

0 comments on commit bb11acb

Please sign in to comment.