solidjs / solid-start

SolidStart, the Solid app framework
https://start.solidjs.com
MIT License
4.93k stars 371 forks source link

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

Open ian-pascoe opened 3 weeks ago

ian-pascoe commented 3 weeks ago

Duplicates

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 commented 3 weeks ago

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 commented 3 weeks ago

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