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

Exclude server endpoints from Vite's dependency optimization #9859

Closed
1 task
pilcrowonpaper opened this issue Jan 29, 2024 · 3 comments · Fixed by #10143
Closed
1 task

Exclude server endpoints from Vite's dependency optimization #9859

pilcrowonpaper opened this issue Jan 29, 2024 · 3 comments · Fixed by #10143
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@pilcrowonpaper
Copy link
Contributor

pilcrowonpaper commented Jan 29, 2024

Astro Info

Astro                    v4.2.6
Node                     v20.2.0
System                   macOS (x64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/node
Integrations             none

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

No response

Describe the Bug

With Astro's current configuration, Vite optimizes all dependencies imported in src/**. That causes issues when you import packages that use .node files such as @node-rs/argon2:

✘ [ERROR] No loader is configured for ".node" files: node_modules/.pnpm/@node-rs+argon2-darwin-x64@1.7.0/node_modules/@node-rs/argon2-darwin-x64/argon2.darwin-x64.node

    node_modules/.pnpm/@node-rs+argon2@1.7.0/node_modules/@node-rs/argon2/index.js:159:36:
      159 │             nativeBinding = require('@node-rs/argon2-darwin-x64')
          ╵                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] No loader is configured for ".node" files: node_modules/.pnpm/@node-rs+bcrypt-darwin-x64@1.9.0/node_modules/@node-rs/bcrypt-darwin-x64/bcrypt.darwin-x64.node

    node_modules/.pnpm/@node-rs+bcrypt@1.9.0/node_modules/@node-rs/bcrypt/binding.js:153:36:
      153 │             nativeBinding = require('@node-rs/bcrypt-darwin-x64')

We should probably exclude at least server endpoints, which we know won't be used in the client, from Vite's optimization. This should lead to better performance as well.

What's the expected result?

Server only modules won't be optimized.

Link to Minimal Reproducible Example

https://github.com/pilcrowOnPaper/astro-deps-optimization-repro

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 Jan 29, 2024
@pilcrowonpaper
Copy link
Contributor Author

In the future, I'd love to see a way to mark modules as server-only, like .server.ts in SvelteKit

@bluwy bluwy added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Jan 29, 2024
@bluwy bluwy self-assigned this Jan 29, 2024
@pixelmund
Copy link

pixelmund commented Feb 9, 2024

@pilcrowonpaper

Currently running into some issues using oslo with astro & cloudflare pages

It only happens when i run pnpm build.

[ERROR] Could not resolve "fs"

    node_modules/.pnpm/@node-rs+argon2@1.7.0/node_modules/@node-rs/argon2/index.js:5:45:
      5 │ const { existsSync, readFileSync } = require('fs')
        ╵                                              ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "path"

    node_modules/.pnpm/@node-rs+argon2@1.7.0/node_modules/@node-rs/argon2/index.js:6:25:
      6 │ const { join } = require('path')
        ╵                          ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "fs"

    node_modules/.pnpm/@node-rs+bcrypt@1.9.0/node_modules/@node-rs/bcrypt/binding.js:5:45:
      5 │ const { existsSync, readFileSync } = require('fs')
        ╵                                              ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "path"

    node_modules/.pnpm/@node-rs+bcrypt@1.9.0/node_modules/@node-rs/bcrypt/binding.js:6:25:
      6 │ const { join } = require('path')
        ╵                          ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] No loader is configured for ".node" files: node_modules/.pnpm/@node-rs+argon2-darwin-arm64@1.7.0/node_modules/@node-rs/argon2-darwin-arm64/argon2.darwin-arm64.node

    node_modules/.pnpm/@node-rs+argon2@1.7.0/node_modules/@node-rs/argon2/index.js:173:36:
      173 │             nativeBinding = require('@node-rs/argon2-darwin-arm64')
          ╵                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] No loader is configured for ".node" files: node_modules/.pnpm/@node-rs+bcrypt-darwin-arm64@1.9.0/node_modules/@node-rs/bcrypt-darwin-arm64/bcrypt.darwin-arm64.node

    node_modules/.pnpm/@node-rs+bcrypt@1.9.0/node_modules/@node-rs/bcrypt/binding.js:165:36:
      165 │             nativeBinding = require('@node-rs/bcrypt-darwin-arm64')
          ╵                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Could not resolve "fs"
  Stack trace:
    at failureErrorWithLog (/Users/julian/Desktop/DEVELOPMENT/projects/form/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1651:15)
    at runOnEndCallbacks (/Users/julian/Desktop/DEVELOPMENT/projects/form/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1486:45)
    at /Users/julian/Desktop/DEVELOPMENT/projects/form/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1086:16
    at handleIncomingPacket (/Users/julian/Desktop/DEVELOPMENT/projects/form/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:764:9)
    at Socket.emit (node:events:518:28)

i've already added oslo to my optimizeDeps exclude:

  vite: {
    optimizeDeps: {
      exclude: ["oslo", "@node-rs/argon2", "@node-rs/bcrypt"],
    }
  },

Do you know how to fix this?

@pilcrowonpaper
Copy link
Contributor Author

@pixelmund Open a new issue on Oslo's repo. FYI you can't use Oslo's password module in non-Node.js runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants