Felix Eklöf
Felix Eklöf

Reputation: 3740

Bundle dependencies to portable Astro server

I have an Astro project with SSR rendering.

export default defineConfig({
  output: "server",
  adapter: node({
    mode: "standalone"
  })
});

When running npm run build, the server is outputted to ./dist/server/entry.mjs. I'm trying to find some way to bundle the dependencies into the server files so that the server folder will work independently of being placed next to the node_modules folder. Similar to how esbuild bundles for node.

And yes, I know that this is server code and that you don't have to bundle the code. But I have my reasons!

Upvotes: 1

Views: 462

Answers (2)

uwe
uwe

Reputation: 4081

For me the solution with noExternal worked, however I got errors then in dev mode. This works for me in production and development:

const isProduction = process.env.NODE_ENV === 'production';

const extra = isProduction
  ? {
      vite: {
        ssr: {
          noExternal: true,
        },
      },
    }
  : {};
  

export default defineConfig({
  output: 'server',
  adapter: node({
    mode: 'standalone',
  }),
  ...extra,
});

Upvotes: 0

Felix Eklöf
Felix Eklöf

Reputation: 3740

I found a solution. You can pass config to Vite via the Astro config. Setting vite.ssr.noExternal: true will bundle all dependencies.

import { defineConfig } from 'astro/config';
import node from "@astrojs/node";

export default defineConfig({
  output: "server",
  adapter: node({
    mode: "standalone"
  }),
  vite: {
    ssr: {
      noExternal: true
    }
  }
});

Upvotes: 1

Related Questions