kit icon indicating copy to clipboard operation
kit copied to clipboard

isSsrBuild on Vite is false for server build

Open khromov opened this issue 1 year ago • 2 comments

Describe the bug

isSsrBuild is incorrectly false for the server build and client build. This prevents us from applying certain Vite configuration only to server/client builds, blocking for example https://github.com/sveltejs/kit/issues/12581

Reproduction

https://github.com/khromov/isSsrBuild-bug-repro

Run npm run build, the output is:

isSsrBuild false
vite v5.4.1 building SSR bundle for production...
✓ 79 modules transformed.
isSsrBuild false
vite v5.4.1 building for production...
✓ 61 modules transformed.

Logs

No response

System Info

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M2
    Memory: 36.36 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.1.0 - ~/.nvm/versions/node/v22.1.0/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v22.1.0/bin/npm
    pnpm: 9.7.1 - ~/.nvm/versions/node/v22.1.0/bin/pnpm
    bun: 1.1.4 - ~/.bun/bin/bun
  Browsers:
    Chrome: 127.0.6533.120
    Chrome Canary: 129.0.6621.0
    Safari: 17.6
    Safari Technology Preview: 18.0
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.2.4 
    @sveltejs/kit: ^2.0.0 => 2.5.22 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.1 
    svelte: ^4.2.7 => 4.2.18 
    vite: ^5.0.3 => 5.4.1

Severity

serious, but I can work around it

Additional Information

No response

khromov avatar Aug 17 '24 20:08 khromov

See also #9544. This might arguably be a duplicate of that.

Conduitry avatar Aug 17 '24 20:08 Conduitry

@Conduitry Thanks for linking the issue, couldn't find it while searching. It seems like a workaround for now could be to do an inline Vite plugin that looks like below (based on your code). It will then allow you to apply selective configuration (for example for manualChunks) only for the client build:

import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";

export default defineConfig(() => {
  const config = {
    plugins: [
      sveltekit(),
      {
        name: 'ssr-config',
        config(config) {
            if (!config?.build?.ssr) {
              config.build.rollupOptions = config.build.rollupOptions || {};
              config.build.rollupOptions.output = {
                ...config.build.rollupOptions.output,
                manualChunks: (id) => {
                  return "my-app";
                }
              }
            }
            return config;
        },
      },
    ],
  };

  return config;
});

khromov avatar Aug 17 '24 20:08 khromov

Closing as a duplicate of #9544. It's essentially the same gotcha that needs to be documented.

Conduitry avatar Sep 09 '24 23:09 Conduitry