安装
pnpm
pnpm add react-barcode-scannerPolyfill
ZBar
ZBar 为内置依赖,无需额外安装,WASM 约为 230 kB。暂不支持 aztec、data_matrix 和 pdf417;如需扫描这些格式,请使用 ZXing。
import 'react-barcode-scanner/polyfill'⚠️
ZBar 和 ZXing polyfill 请二选一。浏览器已有原生 BarcodeDetector 时,polyfill 不会覆盖它。
脚手架
Vite
建议使用最新版本的 Vite。如果 Vite 4 或更低版本无法预构建 ESM-only polyfill,可以将上游依赖排除在依赖优化之外:
export default defineConfig({
optimizeDeps: {
exclude: ['@preflower/barcode-detector-polyfill']
}
})🙏 感谢 @louispotok 对本章节的帮助
Last updated on