question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

library mode: Vite doesn't appear to transform cjs deps correctly

See original GitHub issue

Describe the bug

I’m using Vite to bundle my library. When I bundle my library to es format, a transitive dep (that is only in cjs format) doesn’t appear to be transformed correctly. Here’s the setup:

my_library depends on @xstate/react/fsm, which depends on use-sync-external-store (cjs only).

It seems that use-sync-external-store isn’t being correctly parsed/transformed by Vite, and is causing crashes.

Reproduction

https://stackblitz.com/edit/vitejs-vite-czfvzb?file=lib.js,package.json,vite.config.js,dist%2F_virtual%2Fwith-selector.js&terminal=dev

Steps to reproduce

Using StackBlitz above:

  • run npm run build
  • See contents of build/lib.js
  • Follow import to "build/node_modules/@xstate/react/es/fsm.js"
    • if you can’t see node_modules in StackBlitz, then the important lines from that are:
import { w as withSelector } from "../../../../_virtual/with-selector.js";
...
var storeSnapshot = withSelector.exports.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);

  • see contents of dist/_virtual/with-selector.js and note that exports are empty, so the line above fails. Here’s what that file looks like for reference:
var withSelector = { exports: {} };
export {
  withSelector as w
};

I think this appears to be an issue with Vite, but I wouldn’t mind being corrected if I just don’t have something setup correctly or if my assumptions are wrong. Thanks for your time.

System Info

System:
    OS: macOS 13.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 167.80 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /opt/dev/sh/nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.15.0 - /opt/dev/sh/nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 107.0.5304.110
    Firefox Developer Edition: 107.0
    Safari: 16.1
    Safari Technology Preview: 16.4

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Nov 11, 2022

This seems to reproduce with rollup + @rollup/plugin-commonjs + @rollup/plugin-node-resolve. https://stackblitz.com/edit/rollup-template-1kqybv?file=rollup.config.js

So I think this enhancement request will be in rollup/plugins repo instead of here.

1reaction
frehnercommented, Nov 11, 2022

Thank you so much for your help.

It would be a great update if Vite could change how it emits those files so that they don’t depend on side effects, but the workaround for now is doable.

We can probably close this issue, unless you want to keep it open and change it to be that enhancement request. Or we can close this and I can open a separate issue asking for that.

Which would you prefer?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building for Production - Vite
Library Mode ​​ If the package.json does not contain "type": "module" , Vite will generate different file extensions for Node.js compatibility. . js...
Read more >
SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Author your app or library using ES2015 modules, then efficiently bundle them up into a single file for use in browsers and Node.js....
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
CJS scripts use require() and module.exports; ESM scripts use import… ... of dependencies, until eventually it finds a script that doesn't ...
Read more >
Adding Vite to Your Existing Web App | CSS-Tricks
While I could have probably stopped using those first two plugins years ago, it doesn't really matter since, as I'm sure you've guessed,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found