# Bundler Usage

> Tips for using srvx with bundlers.

Typically `srvx` is to be imported like this.

```js
import { serve } from "srvx";
```

The import above automatically resolves the the correct entrypoint for each runtime. Node.js, Deno, Cloudflare, and Bun use [ESM conditions](https://nodejs.org/api/esm.html#resolution-algorithm-specification) to resolve the correct entrypoint.

Normally, when you are directly using `srvx` in your project without bundling it should work as expected.

## Using Explicit Imports

Instead of depending on ESM conditions, you can explicitly import `srvx` for specific runtime:

```js
import { serve } from "srvx/node";
import { serve } from "srvx/deno";
import { serve } from "srvx/bun";
import { serve } from "srvx/bunny";
import { serve } from "srvx/cloudflare";
```

## Using Bundlers

If srvx is being bundled (e.g. by [Rollup](https://rollupjs.org/) or [esbuild](https://esbuild.github.io/)),
the bundler also has to run the ESM resolution algorithm during bundling.
This means the `srvx` in the bundle will only work with one specific runtime (usually Node.js).

### External Dependency

The simplest way to avoid this is to set `srvx` as an [external dependency](https://rollupjs.org/configuration-options/#external) in your bundler.

<code-group>

```js [Rollup]
export default {
  //...
  external: ["srvx"],
};
```

```ts [esbuild]
import { build } from "esbuild";

await build({
  //...
  external: ["srvx"], // Add this
});
```

```bash [esbuild (CLI)]
esbuild main.ts \
    # ...
    --external:srvx # Add this
```

</code-group>

By doing this, srvx won't be included in the final bundle, it needs to be available at runtime.

### Conditions

Another approach is to set the ESM condition manually at bundle time.

<code-group>

```js [Rollup]
import resolve from "@rollup/plugin-node-resolve";

export default {
  //...
  plugins: [
    resolve({
      preferBuiltins: true,
      conditions: ["node"], // or "deno", "bun", "workerd", etc.
    }),
  ],
};
```

```ts [esbuild]
import { build } from "esbuild";

await build({
  //...
  conditions: ["node"], // or "deno", "bun", "workerd", etc.
});
```

```bash [esbuild (CLI)]
esbuild main.ts \
    # ...
    --conditions:node # or deno, bun, workerd, etc.
```

</code-group>

By doing this, the bundler will resolve the correct version on srvx for your runtime.
