Closed Symbitic closed 6 months ago
I'm not sure what the use case for this is. It's general practice to stub out packages in the build system rather than relying on the package to do it
Additionally why you would bundle this for browsers? SHould you not use the official navigator.bluetooth there instead of this library which is basically node.js?
Additionally why you would bundle this for browsers? SHould you not use the official navigator.bluetooth there instead of this library which is basically node.js?
That's the whole point. import { bluetooth } from "webbluetooth"
will import the SimpleBLE bindings on Node and navigator.bluetooth
in browsers, meaning you don't need to have separate files or packages for Node and browsers.
The specific use case I have in mind is Node-PoweredUp. It has this whole complex abstract adapter class and Noble and WebBluetooth implementations. With this, I will be able to refactor it to just use WebBluetooth directly and still work equally well on Node and browsers.
What build/packaging system do you use for node-poweredup?
node-poweredup doesn't have a build system besides tsc, but build systems shouldn't matter. Bundling is done at the application-level, not library level. The bundler (esbuild, rollup, webpack, etc) decides which file to use based on package.json. If the browser
field is defined, it uses that file if the target is the web browser. Otherwise, it uses the standard Node.js package resolution.
Just to be clear: there will be no changes for Node.js users. However, bundlers (webpack, vite, parcel, etc) and CDNs (esm.sh, unpkg, etc) will use the navigator.bluetooth wrapper when targeting browsers.
The only other feedback I have is that adding this suggests the whole of this library will work in a browser environment (e.g. the new
constructor). Perhaps we should document this isn't the case?
As feedback is relatively minor, I'm happy to merge this now.
I'm excited about this one. This adds a
browser
entry to package.json so that when bundling for browsers, it will automatically usenavigator.bluetooth
when importing this package.