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

[Bug?]: Client bundle including imports from "use server" functions #1526

Open
2 tasks done
ian-pascoe opened this issue Jun 5, 2024 · 4 comments
Open
2 tasks done
Labels
bug Something isn't working vinxi related to vinxi

Comments

@ian-pascoe
Copy link
Contributor

ian-pascoe commented Jun 5, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Vinxi and Vite are bundling dependencies that are only used in "use server"; functions. For example:

import { Resource } from 'sst';

export function getPublicResources() {
  "use server";
  return {
    apiUrl: Resource.APIRouter.url
  } satisfies PublicResources;
}

Vite throws this error during client bundle:

[8:36:13 PM]  ERROR  ../../node_modules/sst/dist/resource.js (1:9): "env" is not exported by "__vite-browser-external", imported by "../../node_modules/sst/dist/resource.js".
file: .../Code/revelationsai/node_modules/sst/dist/resource.js:1:9

1: import { env } from "node:process";
            ^
2: const raw = {
3:     // @ts-expect-error,

Here is my app.config.ts:

import { defineConfig } from '@solidjs/start/config';
import { searchForWorkspaceRoot } from 'vite';
import { cjsInterop } from 'vite-plugin-cjs-interop';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
  middleware: './src/middleware.ts',
  server: {
    preset: 'aws-lambda'
  },
  vite: {
    plugins: [
      tsconfigPaths(),
      cjsInterop({
        dependencies: ['@clerk/clerk-js']
      })
    ],
    server: {
      fs: {
        allow: [searchForWorkspaceRoot(process.cwd())]
      }
    }
  }
});

Workaround

I can get it to bundle successfully by moving the server function to a separate file and put "use server"; at the top.

Expected behavior 🤔

"use server"; dependencies should only be included in ssr and server bundles

Steps to reproduce 🕹

Steps:

  1. In app.tsx create a server function that imports a server-only module like node:async_hooks
  2. Try to build the app

Context 🔦

This is completely blocking my project. I cannot have all my server code sectioned off with my desired design.

Your environment 🌎

No response

@ian-pascoe ian-pascoe added the bug Something isn't working label Jun 5, 2024
@ian-pascoe
Copy link
Contributor Author

ian-pascoe commented Jun 5, 2024

So I have narrowed it down to if I use "verbatimModuleSyntax": true that is causing the problem. As soon as I change it to false, my project builds with "use server" imports not being shipped to the client

@Brendonovich
Copy link
Contributor

Brendonovich commented Jun 8, 2024

Reproduction: https://stackblitz.com/edit/github-fk8exj-eru4op?file=src%2Fapp.tsx

verbatimModuleSyntax as explained by esbuild tells esbuild to not drop unused import statements, which is probably causing the server-only imports to not be removed from the client build.
I don't think rollup respects verbatimModuleSyntax so this seems to just be a dev-only problem.
Happens in build too, nevermind

@mdynnl
Copy link

mdynnl commented Nov 24, 2024

tldr; use dynamic imports so that build tools can analyze and tree-shake correctly
dropping this here to spread awareness

@lxsmnsyc
Copy link
Member

@mdynnl on top of that, if a package/module has side-effects, the bundler can potentially fail to remove the unused import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vinxi related to vinxi
Projects
None yet
Development

No branches or pull requests

5 participants