Sec-ant / barcode-detector

A Barcode Detection API polyfill that uses ZXing-C++ WebAssembly under the hood.
https://www.npmjs.com/package/barcode-detector/v/latest
MIT License
94 stars 8 forks source link

Error upon importing `barcode-detector/pure` #117

Open jp06 opened 6 days ago

jp06 commented 6 days ago

Not really a big blocker but just want to let you know that when I was gonna write a custom polyfilling logic with the pure import but I got this error on build with Vite:

│ $ tsc && vite build
│ vite v5.2.11 building for production...
│ ✓ 12213 modules transformed.
│ x Build failed in 10.57s
│ error during build:
│ Error: [commonjs--resolver] Missing "./dist/es" specifier in "barcode-detector" package
│     at e (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:47597:25)
│     at n (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:47597:627)
│     at o (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:47597:1297)
│     at resolveExportsOrImports (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:48287:20)
│     at resolveDeepImport (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:48306:31)
│     at tryNodeResolve (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:48031:20)
│     at Object.resolveId (file:///Users/<redacted>/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:47781:28)
│     at file:///Users/<redacted>/node_modules/rollup/dist/es/shared/node-entry.js:19778:40
│     at async PluginDriver.hookFirstAndGetPlugin (file:///Users/<redacted>/node_modules/rollup/dist/es/shared/node-entry.js:19678:28)
│     at async resolveId (file:///Users/<redacted>/node_modules/rollup/dist/es/shared/node-entry.js:18359:26)

I am able to import barcode-detector just fine.

I have this vite.json extended by the tsconfig.json:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "./react-library.json",
  "Display": "Vite",
  "moduleResolution": "node",
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ES2020"],
    "types": ["vite/client"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "forceConsistentCasingInFileNames": true
  }
}
Sec-ant commented 6 days ago

The commonjs resolver shouldn't be looking for "./dist/es". If you can provide a minimal repo to reproduce this error it would be of much help.

jp06 commented 6 days ago

I'll try later today, but a quick info is that my code is a private fork of this repo: https://github.com/bigcommerce/b2b-buyer-portal

Sec-ant commented 6 days ago

I think subpath exports are not supported by "moduleResolution": "node": https://stackoverflow.com/a/74538429 or it's very difficult to get it right. And the tsconfig document says You probably won’t need to use node10 in modern code.. Try changing it to bundler.

Additional links:

jp06 commented 5 days ago

Thanks, appreciate the help. I noticed that the moduleResolution is actually not even under the compilerOptions. Other import issues crop up though even if moduleResolution fixes thebarcode-detector/pure import , probably due to how it is set up, so I don't think I'll change it. In my case, the barcode-detector import already fine so I'll just use that.