library mode: Vite doesn't appear to transform cjs deps correctly
See original GitHub issueDescribe 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
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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 10 months ago
- Reactions:1
- Comments:13 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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?