Add a browser-only export for Web clients

This package has a single export that includes both NodeJS and browser modules. When a web project imports this library, the build generates a bunch of warnings, like this in vite:

[plugin vite:resolve] Module net has been externalized for browser compatibility...
[plugin vite:resolve] Module stream has been externalized for browser compatibility...
...

We can clean this up by creating a special browser-only export. Bundlers like Vite and Webpack know to choose the 'browser' export in projects that are web-only (not NodeJS).

This change adds a 'browser' export and updates the documentation to use browser-only examples.
This commit is contained in:
Michael Lynch 2025-12-29 16:41:42 -05:00
parent f6fea65063
commit 486a075b8e
5 changed files with 70 additions and 7 deletions

View file

@ -21,9 +21,11 @@ It can also be used in NodeJS to connect to MeshCore Companion devices over TCP/
npm install @liamcottle/meshcore.js
```
## Simple Example
## Simple Examples
```
### Enumerate Contacts (Node.js)
```javascript
import { TCPConnection, NodeJSSerialConnection } from "@liamcottle/meshcore.js";
// serial connections are supported by "companion_radio_usb" firmware
@ -53,6 +55,34 @@ connection.on("connected", async () => {
await connection.connect();
```
### Enumerate Contacts (Browser)
```html
<button id="connect-serial">Connect via Serial</button>
<script type="module">
import { WebBleConnection, WebSerialConnection } from "@liamcottle/meshcore.js";
// wait until connected
async function onConnected(connection) {
// we are now connected
console.log("connected!");
// log contacts
const contacts = await connection.getContacts();
for (const contact of contacts) {
console.log(`Contact: ${contact.advName}`);
}
}
document.getElementById("connect-serial").addEventListener("click", async () => {
const connection = await WebSerialConnection.open();
connection.on("connected", () => onConnected(connection));
});
</script>
```
## Examples
There's a few other examples scripts in the [examples](./examples) folder.