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

Vite seems to be bundling dependencies it shouldn't? #11785

Closed
1 task
Nickersoft opened this issue Aug 20, 2024 · 5 comments
Closed
1 task

Vite seems to be bundling dependencies it shouldn't? #11785

Nickersoft opened this issue Aug 20, 2024 · 5 comments
Labels
needs repro Issue needs a reproduction

Comments

@Nickersoft
Copy link

Astro Info

Astro                    v4.14.2
Node                     v20.16.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/node
Integrations             @astrojs/react
                         @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I apologize in advance that I don't have a minimal reproduction of this, as I'm not sure what is actually causing the behavior to begin with (I do have a repro though).

I'm currently trying to build my Astro project inside a PNPM monorepo and am referencing a shared private monorepo package.

I'm only using the methods from it to prerender a few pages and JSON routes, however, Astro/Vite seems to be trying to bundle all of the code into dist/server/chunks, including some CJS dependencies that rely on __filename and result in the following error when building:

__filename is not defined in ES module scope

I don't have a firm grasp on how Astro actually prerenders routes, so I'm not sure what I can change to try to fix this. I originally opened an issue with the Vite team but they redirected me back here seeing Astro has a lot of moving parts and maybe the way the compiler works is causing this.

To reproduce, you just run pnpm install in my linked repo then run pnpm build from inside the www directory.

What's the expected result?

Expected to be able to use a local monorepo package and have it prerender routes without issue (or at lest transform the CJS).

Link to Minimal Reproducible Example

https://github.com/Nickersoft/code-snippets/tree/nickersoft/build-bug/www

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 20, 2024
@ematipico
Copy link
Member

ematipico commented Aug 20, 2024

The person from vite already did a brief analysis, which I advise you to follow.

You have some development dependencies inside dependecies. Move them into devDependecies.

Also, your index.ts of registry-tools is a barrel file, which means that you're exporting everything. Is it expected?

@Nickersoft
Copy link
Author

Hey @ematipico,

I appreciate the prompt response – to answer your first question, I believe it is expected? Otherwise I'd have to have deep path imports for every method I'd like to call, so I use an index.ts to export the methods I need in my Astro app and related projects.

I'm unfortunately in a bit of a tight spot, because the dependencies that were mentioned in the other thread – ones that are using dev dependencies like Prettier and TypeScript – I'm using to format and modify TypeScript ASTs on the server (prerendered JSON routes). So I would think they should be regular dependencies, unless they're dev in the sense that they're only required to prerender server routes during build-time?

@Nickersoft
Copy link
Author

So I did a bit more digging here and it looks like the error is because Vite is bundling ts-morph, which is a dependency of my shared library registry-tools. Adding ts-morph as a direct dependency of my Astro project allows it to build fine.

I sort of understand why this is happening, seeing registry-tools is a private library and its code is embedded in the Astro scripts (and therefore when it references ts-morph Astro doesn't know where to find it because it's not a direct dependency, so it bundles it too).

Apart from adding ts-morph to my dependencies, is there really any other kind of fix? I tried making ts-morph a peer dependency of my shared library instead, but it still errors. Just a bit curious as to why this happens and other strategies for avoiding it. Interestingly, ts-morph (a CJS-only package) is the only dependency this is happening for.

@ascorbic
Copy link
Contributor

Your repro no longer exists. Are you still having this problem?

@ascorbic ascorbic added needs repro Issue needs a reproduction and removed needs triage Issue needs to be triaged labels Aug 29, 2024
Copy link
Contributor

Hello @Nickersoft. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@ematipico ematipico closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue needs a reproduction
Projects
None yet
Development

No branches or pull requests

3 participants