Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node specific code been bundled only in production mode (tesseract.js) #2444

Closed
3 tasks done
yue4u opened this issue Mar 9, 2021 · 2 comments
Closed
3 tasks done
Labels
inconsistency Inconsistency between dev & build pending triage

Comments

@yue4u
Copy link
Contributor

yue4u commented Mar 9, 2021

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

It works fine when using tesseract.js as a dependency in development mode, but throws error in production build. I did some investigation and found it looks like the browser field in package.json does not map correctly in production build.

browser field: https://github.com/naptha/tesseract.js/blob/90466c3b5504a9220ba0ff91ccec22003f72cbd2/package.json#L29-L31
where it was loaded: https://github.com/naptha/tesseract.js/blob/90466c3b5504a9220ba0ff91ccec22003f72cbd2/src/createWorker.js#L14

This can be temporally fixed via patch-package to the tesseract.js package on the line where it switches import. Wondering if this could be fixed on the vite side or by adding extra configuration.

Reproduction

https://github.com/rainy-me/vite-tesseract-reproduction

System Info

  • vite version: v2.0.5
  • Operating System: Linux
  • Node version: v15.8.0
  • Package manager (npm/yarn/pnpm) and version: yarn 1.22.10

Logs (Optional if provided reproduction)

  1. Run vite or vite build with the --debug flag.
  2. Provide the error log here.
$ vite build --debug
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: '/home/yue/dev/vite-tesseract-reproduction',
  vite:config   base: '/',
  vite:config   mode: 'production',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge16', 'firefox60', 'chrome61', 'safari11' ],
  vite:config     polyfillDynamicImport: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500
  vite:config   },
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   resolve: { dedupe: undefined, alias: [ [Object] ] },
  vite:config   publicDir: '/home/yue/dev/vite-tesseract-reproduction/public',
  vite:config   command: 'build',
  vite:config   isProduction: true,
  vite:config   optimizeCacheDir: '/home/yue/dev/vite-tesseract-reproduction/node_modules/.vite',
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter'
  vite:config   ],
  vite:config   server: {},
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   },
  vite:config   createResolver: [Function: createResolver]
  vite:config } +2ms
vite v2.0.5 building for production...
✓ 72 modules transformed.
dist/assets/favicon.17e50649.svg   1.49kb
dist/index.html                    0.52kb
dist/assets/index.ccce2ca3.css     0.16kb / brotli: 0.10kb
dist/assets/index.07b4c97f.js      2.21kb / brotli: 1.51kb
dist/assets/vendor.6502411a.js     81.76kb / brotli: 24.03kb
@yongjun21
Copy link

Looks like there are a couple of issues related to how Vite handles the browser field in package.json.

#1979
#2320

@sodatea sodatea added the inconsistency Inconsistency between dev & build label Aug 4, 2021
@yue4u
Copy link
Contributor Author

yue4u commented Oct 15, 2021

After several updates(vite and other libs) the fix above doesn't work anymore and looks like vite is not bunlding dynamic imported worker or wasm?

Anyway I found another hacky way to fix this issue: directly importing them via ?url will do the trick, no need for patch-package anymore.

import { createWorker } from "tesseract.js";
import workerPath from "tesseract.js/dist/worker.min.js?url";
import corePath from "tesseract.js-core/tesseract-core.wasm.js?url";

createWorker({
  workerPath,
  corePath,
  // ..
})

I'm closing this issue since I'm not sure if the origin problem still exists.

@yue4u yue4u closed this as completed Oct 15, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
inconsistency Inconsistency between dev & build pending triage
Projects
None yet
Development

No branches or pull requests

3 participants