vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
67.9k stars 6.11k forks source link

Error while trying to bundle local workspace dependency #17902

Closed Nickersoft closed 1 month ago

Nickersoft commented 1 month ago

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/nodemodules/.pnpm/astro@4.14.2@types+node@22.4.1_rollup@4.21.0_typescript@5.5.4/node_modules/astro/dist/core/build/pipeline.js:237:15)

Validations

hi-ogawa commented 1 month ago

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/prettier@3.3.3/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/prettier@3.3.3/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 commented 1 month ago

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!