-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
library mode: Vite doesn't appear to transform cjs deps correctly #10866
Comments
I wasn't able to reproduce with the following steps.
// ./dist/node_modules/@xstate/react/es/fsm.js
import "../../../react/index.js";
import "../../../use-sync-external-store/shim/with-selector.js";
globalThis && globalThis.__read || function(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
} catch (error) {
e = { error };
} finally {
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
} finally {
if (e)
throw e.error;
}
}
return ar;
};
//# sourceMappingURL=fsm.js.map // ./dist/node_modules/use-sync-external-store/shim/with-selector.js
import { w as withSelector } from "../../../_virtual/with-selector.js";
import { __require as requireWithSelector_production_min } from "../cjs/use-sync-external-store-shim/with-selector.production.min.js";
import { __require as requireWithSelector_development } from "../cjs/use-sync-external-store-shim/with-selector.development.js";
(function(module) {
if (process.env.NODE_ENV === "production") {
module.exports = requireWithSelector_production_min();
} else {
module.exports = requireWithSelector_development();
}
})(withSelector);
//# sourceMappingURL=with-selector.js.map It doesn't have the content you described. |
You're absolutely right, sorry that I missed that. The output from StackBlitz looks different than the output for my library, and I have no idea why at the moment. I'm trying to reproduce it; I'll just close this issue and open a new one if I need to. Sorry, and thank you! |
Ok actually I was able to reproduce it outside of StackBlitz, so I'll just reopen this issue if that's ok. (I can open a new one if you prefer) How to reproduce:
Note that I committed the built files to the repo, so you don't technically need to run anything to see the output. For example, you will now see that the built that causes things to break when it reaches this line below. I have yet to figure out why StackBlitz wouldn't output things this way, but this example repo does. |
@frehner Your repository seems to be private. |
Ack, I'm so sorry. Should be fixed. |
I think the output is correct.
|
Is that true of the I'll get a reproduction with NextJS as an example up soon |
Ah, I mistakenly read the Assuming that the library you are talking about is hydrogen-ui, I guess this |
Yup, talking about hydrogen-ui! Here's a PR / repo of things failing in NextJS with these settings Shopify/hydrogen-react#64
Is... there something that should be included in there that I'm missing? Are there built files that should be included? [edit] but you're right, by removing it NextJS works! So there must be some built files that I need to add to it? |
Since it's not feasible to completely remove I think by changing it to: {
"sideEffects": [
"dist/dev/node_modules/use-sync-external-store/shim/with-selector.mjs",
"dist/prod/node_modules/use-sync-external-store/shim/with-selector.mjs",
"dist/dev/node_modules/use-sync-external-store/shim/with-selector.js",
"dist/prod/node_modules/use-sync-external-store/shim/with-selector.js"
],
} that will cover it for now? Not a very scalable solution as we'll have to track things down manually if they crop up, but works for now I think. |
At least,
I'm not sure how much Webpack can tree shake but I guess yes. That will cover. If you don't want to check whether the bundled package has a side effect (as they aren't in control), I think changing to this is reasonable. {
"sideEffects": [
"dist/*/node_modules/**/*"
],
} |
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? |
See vitejs/vite#10866 for more details
* include xstate in builds * Fix NextJS issues by including files with sideeffects See vitejs/vite#10866 for more details
This seems to reproduce with rollup + @rollup/plugin-commonjs + @rollup/plugin-node-resolve. So I think this enhancement request will be in rollup/plugins repo instead of here. |
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 onuse-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:
npm run build
build/lib.js
"build/node_modules/@xstate/react/es/fsm.js"
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: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
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: