electron-vite / electron-vite-react

:electron: Electron + Vite + React + Sass boilerplate.
https://electron-vite.github.io
MIT License
1.94k stars 240 forks source link

OCR In Electron-vite using Tesseract via node-native-ocr #223

Open arvinjarvin opened 4 months ago

arvinjarvin commented 4 months ago

Trying to use OCR from frontend using node-native-ocr package

The output is just recognize isn't exported from node-native-ocr (in the dev console)

Also tried to add this to the optimizeDeps exclusions array in the electron.vite.config.ts

Still no luck

I note that electron-vite 'doesn't support nodeIntegration' - I have also tried to plugin using commonjsExternals, but now it says

require is not defined

in the frontend.

This will be an interesting one...

arvinjarvin commented 4 months ago

Content of electron.vite.config.ts

import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, bytecodePlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'
import commonjsExternals from 'vite-plugin-commonjs-externals'

let externals = ['node-native-ocr', 'babel-runtime', 'node-addon-api', 'prebuild-install']

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin(), bytecodePlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin(), bytecodePlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src')
      }
    },
    optimizeDeps: {
      exclude: externals
    },
    plugins: [react(), commonjsExternals({ externals })]
  }
})

Content of App.tsx relevant section:

recognize(my_img_here, (err: any, text: any)) => {
            if (err) {
              console.error(err)
            } else {
              console.log(text)
            }
          })

May have misplaced some brackets during transfer as I changed to my_img_here however rest assured the problem is not syntax.