Skip to content

Commit

Permalink
Convert Vite plugin adapter API to presets (#8620)
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Cattori <pcattori@gmail.com>
  • Loading branch information
markdalgleish and pcattori authored Jan 31, 2024
1 parent 7ddaa9c commit 44bac29
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .changeset/ninety-baboons-leave.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@remix-run/dev": patch
---

Vite: Add `adapter` option to support modifying the build output and/or development environment for different hosting providers.
Vite: Add `presets` option to ease integration with different platforms and tools.
5 changes: 5 additions & 0 deletions .changeset/quiet-adults-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Add `buildEnd` hook
36 changes: 24 additions & 12 deletions docs/future/vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ The following subset of Remix config options are supported:

The Vite plugin also accepts the following additional options:

#### adapter
#### buildDirectory

A function for adapting the build output and/or development environment for different hosting providers.
The path to the build directory, relative to the project root. Defaults to
`"build"`.

#### buildDirectory
#### buildEnd

The path to the build directory, relative to the project root. Defaults to `"build"`.
A function that is called after the full Remix build is complete.

#### manifest

Whether to write a `.remix/manifest.json` file to the build directory. Defaults to `false`.
Whether to write a `.remix/manifest.json` file to the build directory. Defaults
to `false`.

#### presets

An array of Remix config presets to ease integration with different platforms and tools.

#### serverBuildFile

Expand Down Expand Up @@ -116,19 +122,19 @@ wrangler pages dev ./build/client
```

While Vite provides a better development experience, Wrangler provides closer emulation of the Cloudflare environment by running your server code in [Cloudflare's `workerd` runtime][cloudflare-workerd] instead of Node.
To simulate the Cloudflare environment in Vite, Wrangler provides [Node proxies for resource bindings][wrangler-getbindingsproxy] which are automatically available when using the Remix Cloudflare adapter:
To simulate the Cloudflare environment in Vite, Wrangler provides [Node proxies for resource bindings][wrangler-getbindingsproxy] which are automatically available when using the Remix Cloudflare preset:

```ts filename=vite.config.ts lines=[3,10]
import {
unstable_vitePlugin as remix,
unstable_vitePluginAdapterCloudflare as cloudflare,
unstable_vitePluginPresetCloudflare as cloudflare,
} from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [
remix({
adapter: cloudflare(),
presets: [cloudflare()],
}),
],
});
Expand Down Expand Up @@ -443,19 +449,25 @@ The Remix Vite plugin only officially supports [Cloudflare Pages][cloudflare-pag

</docs-warning>

👉 **Add the Cloudflare adapter to your Vite config**
👉 **In your Vite config, add `"workerd"` and `"worker"` to Vite's
`ssr.resolve.externalConditions` option and add the Cloudflare Remix preset**

```ts filename=vite.config.ts lines=[3,10]
```ts filename=vite.config.ts lines=[3,8-12,15]
import {
unstable_vitePlugin as remix,
unstable_vitePluginAdapterCloudflare as cloudflare,
unstable_vitePluginPresetCloudflare as cloudflare,
} from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
ssr: {
resolve: {
externalConditions: ["workerd", "worker"],
},
},
plugins: [
remix({
adapter: cloudflare(),
presets: [cloudflare()],
}),
],
});
Expand Down
8 changes: 8 additions & 0 deletions integration/helpers/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ export const VITE_CONFIG = async (args: {
pluginOptions?: string;
vitePlugins?: string;
viteManifest?: boolean;
viteSsrResolveExternalConditions?: string[];
}) => {
let hmrPort = await getPort();
return String.raw`
import { defineConfig } from "vite";
import { unstable_vitePlugin as remix } from "@remix-run/dev";
export default defineConfig({
ssr: {
resolve: {
externalConditions: ${JSON.stringify(
args.viteSsrResolveExternalConditions ?? []
)},
},
},
server: {
port: ${args.port},
strictPort: true,
Expand Down
125 changes: 0 additions & 125 deletions integration/vite-adapter-test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion integration/vite-cloudflare-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ test.describe("Vite / cloudflare", async () => {
),
"vite.config.ts": await VITE_CONFIG({
port,
pluginOptions: `{ adapter: (await import("@remix-run/dev")).unstable_vitePluginAdapterCloudflare() }`,
viteSsrResolveExternalConditions: ["workerd", "worker"],
pluginOptions: `{ presets: [(await import("@remix-run/dev")).unstable_vitePluginPresetCloudflare()] }`,
}),
"functions/[[page]].ts": `
import { createPagesFunctionHandler } from "@remix-run/cloudflare-pages";
Expand Down
Loading

0 comments on commit 44bac29

Please sign in to comment.