Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Error while trying to bundle local workspace dependency #17902

Closed
7 tasks done
Nickersoft opened this issue Aug 19, 2024 · 2 comments
Closed
7 tasks done

Error while trying to bundle local workspace dependency #17902

Nickersoft opened this issue Aug 19, 2024 · 2 comments

Comments

@Nickersoft
Copy link

Describe the bug

Hey folks,

I know bugs in a similar vein have been reported here before, but the supposed fixes are working for me – I have a PNPM monorepo and am trying to build my Astro project using Vite while depending on another workspace ESM package.

This other package has some CJS dependencies that aren't being transformed by Vite during compilation.

Notably, I keep receiving the error:

__filename is not defined in ES module scope

Reproduction

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

Steps to reproduce

You can reproduce it via my source repo. Just clone it, run pnpm install in the root, then cd into the www directory and run pnpm run build. The error occurs while bundling the Astro website using a Vercel adapter. If this ends up being an Astro issue, happy to open an issue over there, though I have a hunch it's Vite.

System Info

Vite 5.4.1

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 97.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.16.0 - ~/.local/share/mise/installs/node/20/bin/node
    npm: 10.8.1 - ~/.local/share/mise/installs/node/20/bin/npm
    pnpm: 9.7.1 - ~/.local/share/mise/installs/node/20/bin/pnpm
    bun: 1.1.6 - ~/.local/share/mise/installs/bun/latest/bin/bun
    Watchman: 2024.08.12.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 123.1.64.113
    Safari: 17.5

Used Package Manager

pnpm

Logs

__filename is not defined in ES module scope
Stack trace:
at isFileSystemCaseSensitive (file:///Users/tjnickerson/Documents/GitHub/Nickersoft/code-snippets/www/.vercel/output/_functions/chunks/snippets_Dkrq05sR.mjs:8647:36)
at file:///Users/tjnickerson/Documents/GitHub/Nickersoft/code-snippets/www/.vercel/output/_functions/chunks/snippets_Dkrq05sR.mjs:8856:13
at file:///Users/tjnickerson/Documents/GitHub/Nickersoft/code-snippets/www/.vercel/output/functions/chunks/snippets_Dkrq05sR.mjs:192449:4
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async BuildPipeline.retrieveSsrEntry (file:///Users/tjnickerson/Documents/GitHub/Nickersoft/code-snippets/node_modules/.pnpm/[email protected]
@types[email protected][email protected][email protected]/node_modules/astro/dist/core/build/pipeline.js:237:15)

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Aug 20, 2024

The error occurs while bundling the Astro website using a Vercel adapter. If this ends up being an Astro issue, happy to open an issue over there, though I have a hunch it's Vite.

Unless you have a reproduction without Astro (and current reproduction is far from minimal for Asto I believe), it's usually better to open an issue there first. If Astro maintainers think this is a Vite issue, then they can redirect you here and that's more efficient flow.


Checking the reproduction, it looks like you didn't mention that the error is happening during pre-rendering:

 prerendering static routes 
__filename is not defined in ES module scope
  Stack trace:
    at isFileSystemCaseSensitive (file:///home/hiroshi/code/tmp/vite-17902-cjs/www/dist/server/chunks/snippets_B-C9ivQo.mjs:8648:36)

After following stacktrace, it looks like server bundle includes typescript, prettier, etc... and all the heavy dependenceis from one of your monorepo package registry-tools.

I thought ssr.external would at least make it work, but seeing some runtime error from prettier:

export default defineConfig({
...
  adapter: node({
    mode: 'standalone',
  }),
  vite: {
    ssr: {
      external: ["registry-tools"]
    }
  }
});
 prerendering static routes 
09:26:57 λ src/pages/registry/[type].json.ts
09:26:57   ├─ /registry/helpers.json (+4ms)
09:26:57   └─ /registry/snippets.json (+1ms)
09:26:57 λ src/pages/registry/[type]/[name].ts
09:26:57   ├─ /registry/helpers/alphabetical.json (+1ms)
09:26:57   ├─ /registry/helpers/alphabetical.ts (+1ms)
09:26:57   ├─ /registry/helpers/alphabetical.jsNo parser and no file path given, couldn't infer a parser.
  Stack trace:
    at normalizeFormatOptions (file:///home/hiroshi/code/tmp/vite-17902-cjs/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:20621:13)

Actually, I see a same error from astro dev when accessing http://localhost:4321/registry/helpers/alphabetical.js

09:29:45 [404] /registry/helpers/alphabetical 1ms
09:29:46 [ERROR] No parser and no file path given, couldn't infer a parser.
  Stack trace:
    at /home/hiroshi/code/tmp/vite-17902-cjs/www/home/hiroshi/code/tmp/vite-17902-cjs/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:20621:13
    [...] See full stack trace in the browser, or rerun with --verbose.

So this issue might be simply your application's issue.

Considering all this, it's hard to analyze it as a Vite issue, so I'd suggest opening an issue on Astro first and get some help there.

@Nickersoft
Copy link
Author

Hey @hi-ogawa! Thanks for the prompt response – that makes sense, and it is puzzling to me why all of that stuff is getting bundled. I assumed it might be Vite because Vite handle's Astro's bundling, but you're right, it could also be an issue with the adapter I'm using or something Astro-specific. Will ask over there!

@vitejs vitejs locked and limited conversation to collaborators Aug 20, 2024
@hi-ogawa hi-ogawa converted this issue into discussion #17904 Aug 20, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants