From f457da26e17459e6651251b90450158851db465f Mon Sep 17 00:00:00 2001 From: Krisztian Kemenes Date: Tue, 2 Jan 2024 01:32:35 +0200 Subject: [PATCH] Fix package in the browser --- .changeset/fresh-balloons-move.md | 5 +++++ package.json | 4 ++-- src/helpers/USBUtils.ts | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .changeset/fresh-balloons-move.md diff --git a/.changeset/fresh-balloons-move.md b/.changeset/fresh-balloons-move.md new file mode 100644 index 0000000..1cf0115 --- /dev/null +++ b/.changeset/fresh-balloons-move.md @@ -0,0 +1,5 @@ +--- +"label-printer": patch +--- + +Make package not include `usb` in the browser diff --git a/package.json b/package.json index 866de72..23bf572 100644 --- a/package.json +++ b/package.json @@ -24,14 +24,14 @@ "license": "ISC", "repository": "https://github.com/kemkriszt/raw-thermal-print.git", "devDependencies": { + "@changesets/cli": "^2.27.1", "@types/jest": "^29.5.11", "jest": "^29.7.0", "ts-jest": "^29.1.1", "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", "tsup": "^8.0.1", - "typescript": "^5.3.3", - "@changesets/cli": "^2.27.1" + "typescript": "^5.3.3" }, "dependencies": { "@types/w3c-web-usb": "^1.0.10", diff --git a/src/helpers/USBUtils.ts b/src/helpers/USBUtils.ts index ffa6ee5..24f9a48 100644 --- a/src/helpers/USBUtils.ts +++ b/src/helpers/USBUtils.ts @@ -1,4 +1,3 @@ -import { WebUSB } from "usb"; import StringUtils from "./StringUtils"; const unsupportedUsbError = "usb-unsupported" @@ -12,14 +11,15 @@ let usbAgent: USB const getUSB = async (): Promise => { if(usbAgent) return usbAgent - if(typeof navigator !== "undefined") { + if(typeof window !== "undefined") { if(navigator.usb) { usbAgent = navigator.usb } else { throw unsupportedUsbError } } else { - const { WebUSB } = await import("usb") + // TODO: Check how to avoid eval + const { WebUSB } = eval("require")("usb") usbAgent = new WebUSB({allowAllDevices: true}) } @@ -45,7 +45,7 @@ export const getDevices = async (): Promise => { */ export const requestDevice = async (): Promise => { const agent = await getUSB() - const device = await agent.requestDevice() + const device = await agent.requestDevice({filters: []}) if(device) { return new UsbDevice(device) } else {