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

Development mode transpiles correctly while build doesn't. #5759

Closed
7 tasks done
MathiasDeWeerdt opened this issue Nov 19, 2021 · 10 comments
Closed
7 tasks done

Development mode transpiles correctly while build doesn't. #5759

MathiasDeWeerdt opened this issue Nov 19, 2021 · 10 comments
Labels
bug: upstream Bug in a dependency of Vite inconsistency Inconsistency between dev & build

Comments

@MathiasDeWeerdt
Copy link

Describe the bug

I am using the following dependency: https://github.com/bitcoinjs/bip39

This loads bip39 and works perfectly when running yarn dev(I did have to use some work arounds to get it running), when building it breaks.

I'd like to understand why this works under development but not when I build the entire project. It seems to be related to commonjs because bip39 uses require. This is perfectly transpiled when using development but breaks when build.

Reproduction

Miminal reproduction: https://github.com/jimbertools/vite-bip39
This loads bip39 and works perfectly when running yarn dev(I did have to use some work arounds to get it running), when building it breaks.

System Info

╰─❯ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers


npx: installed 1 in 0.904s

  System:
    OS: Linux 5.13 Pop!_OS 20.04 LTS
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 23.09 GB / 30.98 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 14.18.1 - /usr/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 6.14.15 - /usr/bin/npm
  Browsers:
    Chrome: 96.0.4664.45
    Firefox: 94.0

Used Package Manager

yarn

Logs

No response

Validations

@Mahdi-Esbati
Copy link

Mahdi-Esbati commented Nov 23, 2021

Similarly, I have encountered the same problem.
In my case, the package that is causing this issue is: dagre which is a legacy commonJS package and is no longer maintained.

My app works perfectly in development mode but breaks in production when it is build by rollup with the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'Graph')
    at vendor.dec94905.js:503

This issue only appears in version 2.6.3 and up. It does not appear in version 2.6.2.
I believe this is related to this pull request: #5173

@ygj6
Copy link
Member

ygj6 commented Dec 3, 2021

This may be an problem of @rollup/plugin-commonjs instead of vite. FYI, I created a minimal replica rollup project using bip39, it also built successfully but failed to run in the browser. https://stackblitz.com/edit/web-platform-pxjx8j?file=src/main.js

The reason for the inconsistency between dev and build is that in dev, vite uses esbuild to bundle the CJS to ESM, while in build, vite uses rollup & @rollup/plugin-commonjs

@ygj6 ygj6 added the bug: upstream Bug in a dependency of Vite label Dec 3, 2021
@Niputi Niputi added the inconsistency Inconsistency between dev & build label Dec 3, 2021
@agileago
Copy link

+1

@MichaelFroeschen
Copy link

This may be an problem of @rollup/plugin-commonjs instead of vite. FYI, I created a minimal replica rollup project using bip39, it also built successfully but failed to run in the browser. https://stackblitz.com/edit/web-platform-pxjx8j?file=src/main.js

The reason for the inconsistency between dev and build is that in dev, vite uses esbuild to bundle the CJS to ESM, while in build, vite uses rollup & @rollup/plugin-commonjs

Has an issue at @rollup/plugin-commonjs already been created? I do not have the insight to create a sufficient issue (and unfortunately not enough time to get into it) otherwise i would create it.

@FPG-Alan
Copy link

FPG-Alan commented Feb 10, 2022

Similarly, I have encountered the same problem. In my case, the package that is causing this issue is: dagre which is a legacy commonJS package and is no longer maintained.

My app works perfectly in development mode but breaks in production when it is build by rollup with the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'Graph')
    at vendor.dec94905.js:503

This issue only appears in version 2.6.3 and up. It does not appear in version 2.6.2. I believe this is related to this pull request: #5173

Rollup commonjs plugin set ignoreTryCatch default as true at version 21.0.0(use safe default value for ignoreTryCatch), and vite upgrade commonjs plugin to this version at it's 2.6.3 version(#5173), maybe this is the cause of the problem.

Try to set ignoreTryCatch to false in vite.config and the problem maybe solved:

defineConfig({
  ...
  build: {
    commonjsOptions: {
      ignoreTryCatch: false,
    }
  }
  ...
})


Some libraries, including the one I use, may use require to import other libraries and wrap them in try catch statements, commonjs plugin under version 21.0.0 will translate this code to ESM by default but the new version will not.

@MichaelFroeschen
Copy link

Thanks, That actually fixed it for me 😄

@alexamy
Copy link

alexamy commented Feb 23, 2022

Thanks, @FPG-Alan, it works for resolving problem with dagre package.

@ranyefet
Copy link

ranyefet commented May 9, 2022

I had the same issue and this flag fixed it.

@plutda
Copy link

plutda commented May 22, 2022

it works for dagre "Cannot read property 'Graph' of undefined" problem

dhess added a commit to hackworthltd/primer-app that referenced this issue Jun 25, 2022
Judging from vitejs/vite#5759, this should
fix the Chromatic issue we've been having with dagre, and if so, will
also fix a problem we *would* have had with a production build of our
own app once we tried to render React Flow trees.
dhess added a commit to hackworthltd/primer-app that referenced this issue Jun 25, 2022
Judging from vitejs/vite#5759, this should
fix the Chromatic issue we've been having with dagre, and if so, will
also fix a problem we *would* have had with a production build of our
own app once we tried to render React Flow trees.
dhess added a commit to hackworthltd/primer-app that referenced this issue Jun 25, 2022
Judging from vitejs/vite#5759, this should
fix the Chromatic issue we've been having with dagre, and if so, will
also fix a problem we *would* have had with a production build of our
own app once we tried to render React Flow trees.
dhess added a commit to hackworthltd/primer-app that referenced this issue Jun 25, 2022
This commit fixes the Chromatic issue we've been having with dagre. It
also preemptively applies the same workaround to the `primer-app`
package, as it's likely we'd have had the same issue for production
builds of the app.

For details, see:

vitejs/vite#5759
dhess added a commit to hackworthltd/primer-app that referenced this issue Jun 25, 2022
This commit fixes the Chromatic issue we've been having with dagre. It
also preemptively applies the same workaround to the `primer-app`
package, as it's likely we'd have had the same issue for production
builds of the app.

For details, see:

vitejs/vite#5759
@bluwy
Copy link
Member

bluwy commented Jun 26, 2022

Vite 3 now uses esbuild for builds, and I can confirm the repro works now with Vite 3.0.0-beta.2

@bluwy bluwy closed this as completed Jun 26, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite inconsistency Inconsistency between dev & build
Projects
None yet
Development

No branches or pull requests