Version 3.1.1 sets device then immediately sets it to undefined when built with angular cli with --prod flag.
See original GitHub issueAfter running ng serve
. The device is set and the scanner works as expected, but the following warning is logged on console:
Setting the same device is not allowed.
After running ng build --prod
. The same error is logged but the device is set and immediately after set to undefined. It shows the camera video window for a second or less and it disappears.
To reproduce: clone the demo app, run ng build --prod and start scanner
The expected behaviour should be, even if setting similar device, it should behave like it does on dev environment when you build for production environment
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: Ubuntu
- Browser: chrome
- Version 86
Smartphone (please complete the following information):
- Device: android
- OS: android
- Browser chrome
- Version 86
Additional context I don’t know why it alters the already set device in production mode and not in dev mode.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:23 (13 by maintainers)
Top GitHub Comments
When you try to build the master branch with --prod flag and build optimization enabled, the
js
produced byivy
build pipeline breaks some things. I don’t know the root cause of this but @odahcam has intimated it may be that “Angular compiler and zxing/browser are using different variations of the zxing/library builds”.I have vendored the scanner locally and I encountered three of the things that
ivy
breaks in the scanner. All three are in thebrowser-multi-format-continuous-reader.ts
file.The built
js
has a conflict in variable naming so you encounterReferenceError: Cannot access 'n' before initialization
to solve this I changedcontrols
variable name on line 41 ofbrowser-multi-format-continuous-reader.ts
file toctrls
now the file from line 38 looks like this:I encountered #361 (comment)
Cannot read property 'stop' of undefined
which was caused by usingcontrols.stop()
before controls is initialized on line 64 of the same file. Changing this tothis.scannerControls.stop()
fixed this issue.The third thing that
ivy
broke was that it stripped away the class names for the errors that are checked from line 51 to 60 of the file above:These class names are defined on
zxing/library
. For some reason these class names are all renamed toe
after --prod build which means that they are always handled as fatal errors and therefore the scanner stops. @odahcam tried to add akind
property on the library to do the error checking on the scanner but this was stripped away too. Luckily the most frequent Exception of the three,NotFoundException
which is thrown when a barcode is not found on the stream has an error message that survives the build pipeline. When you check for this by adding|| error.message.includes('No MultiFormat Readers were able to detect the code.')
on theif
conditions above. The scanner works properly. I can confirm that with these ugly fixes the scanner works with the latest angular version 11.0.0. These are not permanent fixes as you can see 1 and 3 do not address the root cause of why this is happening and 3 doesn’t check for the other two non-fatal errors since the library do not throw the error message for them. That said I’ve been working with these fixes with acceptable degree of success on the latest angular version with optimization enabled with cli --prod build.I had same issue. Rolling back to release 3.0.1 =
npm install @zxing/ngx-scanner@3.0.1
fixes it and seems to work fine with Angular 11. Of course this is a temporary measure.