@cloudflare/vite-plugin 0.0.0-f6cc0293d → 0.0.0-fbba583df
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -34
- package/dist/asset-workers/asset-worker.js +1305 -1584
- package/dist/asset-workers/router-worker.js +1191 -1488
- package/dist/index.d.ts +8 -1
- package/dist/index.js +7667 -517
- package/dist/runner-worker/index.js +15 -3
- package/package.json +21 -14
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# `@cloudflare/vite-plugin`
|
|
2
2
|
|
|
3
|
-
[Intro](#intro) | [Quick start](#quick-start) | [Tutorial](#tutorial) | [API](#api) | [Cloudflare environments](#
|
|
3
|
+
[Intro](#intro) | [Quick start](#quick-start) | [Tutorial](#tutorial) | [API](#api) | [Cloudflare environments](#cloudflare-environments) | [Migrating from `wrangler dev`](#migrating-from-wrangler-dev)
|
|
4
4
|
|
|
5
5
|
## Intro
|
|
6
6
|
|
|
@@ -16,23 +16,42 @@ Your Worker code runs inside [workerd](https://github.com/cloudflare/workerd), m
|
|
|
16
16
|
|
|
17
17
|
## Quick start
|
|
18
18
|
|
|
19
|
+
### Start with a basic `package.json`
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"name": "cloudflare-vite-quick-start",
|
|
24
|
+
"private": true,
|
|
25
|
+
"version": "0.0.0",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "vite",
|
|
29
|
+
"build": "vite build",
|
|
30
|
+
"preview": "vite preview"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
> [!NOTE]
|
|
36
|
+
> Ensure that you include `"type": "module"` in order to use ES modules by default.
|
|
37
|
+
|
|
19
38
|
### Install the dependencies
|
|
20
39
|
|
|
21
40
|
```sh
|
|
22
|
-
npm install @cloudflare/vite-plugin wrangler --save-dev
|
|
41
|
+
npm install vite @cloudflare/vite-plugin wrangler --save-dev
|
|
23
42
|
```
|
|
24
43
|
|
|
25
|
-
###
|
|
44
|
+
### Create your Vite config file and include the Cloudflare plugin
|
|
26
45
|
|
|
27
46
|
```ts
|
|
28
47
|
// vite.config.ts
|
|
29
48
|
|
|
30
|
-
import { defineConfig } from
|
|
31
|
-
import { cloudflare } from
|
|
49
|
+
import { defineConfig } from "vite";
|
|
50
|
+
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
32
51
|
|
|
33
52
|
export default defineConfig({
|
|
34
53
|
plugins: [cloudflare()],
|
|
35
|
-
})
|
|
54
|
+
});
|
|
36
55
|
```
|
|
37
56
|
|
|
38
57
|
### Create your Worker config file
|
|
@@ -40,7 +59,7 @@ export default defineConfig({
|
|
|
40
59
|
```toml
|
|
41
60
|
# wrangler.toml
|
|
42
61
|
|
|
43
|
-
name = "
|
|
62
|
+
name = "cloudflare-vite-quick-start"
|
|
44
63
|
compatibility_date = "2024-12-30"
|
|
45
64
|
main = "./src/index.ts"
|
|
46
65
|
```
|
|
@@ -52,12 +71,12 @@ main = "./src/index.ts"
|
|
|
52
71
|
|
|
53
72
|
export default {
|
|
54
73
|
fetch() {
|
|
55
|
-
return new Response(`Running in ${navigator.userAgent}!`)
|
|
74
|
+
return new Response(`Running in ${navigator.userAgent}!`);
|
|
56
75
|
},
|
|
57
|
-
}
|
|
76
|
+
};
|
|
58
77
|
```
|
|
59
78
|
|
|
60
|
-
You can now develop (`
|
|
79
|
+
You can now develop (`npm run dev`), build (`npm run build`), preview (`npm run preview`), and deploy (`npm exec wrangler deploy`) your application.
|
|
61
80
|
|
|
62
81
|
## Tutorial
|
|
63
82
|
|
|
@@ -88,13 +107,13 @@ npm install @cloudflare/vite-plugin wrangler --save-dev
|
|
|
88
107
|
```ts
|
|
89
108
|
// vite.config.ts
|
|
90
109
|
|
|
91
|
-
import { defineConfig } from
|
|
92
|
-
import react from
|
|
93
|
-
import { cloudflare } from
|
|
110
|
+
import { defineConfig } from "vite";
|
|
111
|
+
import react from "@vitejs/plugin-react";
|
|
112
|
+
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
94
113
|
|
|
95
114
|
export default defineConfig({
|
|
96
115
|
plugins: [react(), cloudflare()],
|
|
97
|
-
})
|
|
116
|
+
});
|
|
98
117
|
```
|
|
99
118
|
|
|
100
119
|
#### Create your Worker config file
|
|
@@ -181,22 +200,22 @@ The assets `binding` defined here will allow you to access the assets functional
|
|
|
181
200
|
// api/index.ts
|
|
182
201
|
|
|
183
202
|
interface Env {
|
|
184
|
-
ASSETS: Fetcher
|
|
203
|
+
ASSETS: Fetcher;
|
|
185
204
|
}
|
|
186
205
|
|
|
187
206
|
export default {
|
|
188
207
|
fetch(request, env) {
|
|
189
|
-
const url = new URL(request.url)
|
|
208
|
+
const url = new URL(request.url);
|
|
190
209
|
|
|
191
|
-
if (url.pathname.startsWith(
|
|
210
|
+
if (url.pathname.startsWith("/api/")) {
|
|
192
211
|
return Response.json({
|
|
193
|
-
name:
|
|
194
|
-
})
|
|
212
|
+
name: "Cloudflare",
|
|
213
|
+
});
|
|
195
214
|
}
|
|
196
215
|
|
|
197
|
-
return env.ASSETS.fetch(request)
|
|
216
|
+
return env.ASSETS.fetch(request);
|
|
198
217
|
},
|
|
199
|
-
} satisfies ExportedHandler<Env
|
|
218
|
+
} satisfies ExportedHandler<Env>;
|
|
200
219
|
```
|
|
201
220
|
|
|
202
221
|
The Worker above will be invoked for any request not matching a static asset.
|
|
@@ -211,14 +230,14 @@ Replace the file contents with the following code:
|
|
|
211
230
|
```tsx
|
|
212
231
|
// src/App.tsx
|
|
213
232
|
|
|
214
|
-
import { useState } from
|
|
215
|
-
import reactLogo from
|
|
216
|
-
import viteLogo from
|
|
217
|
-
import
|
|
233
|
+
import { useState } from "react";
|
|
234
|
+
import reactLogo from "./assets/react.svg";
|
|
235
|
+
import viteLogo from "/vite.svg";
|
|
236
|
+
import "./App.css";
|
|
218
237
|
|
|
219
238
|
function App() {
|
|
220
|
-
const [count, setCount] = useState(0)
|
|
221
|
-
const [name, setName] = useState(
|
|
239
|
+
const [count, setCount] = useState(0);
|
|
240
|
+
const [name, setName] = useState("unknown");
|
|
222
241
|
|
|
223
242
|
return (
|
|
224
243
|
<>
|
|
@@ -245,9 +264,9 @@ function App() {
|
|
|
245
264
|
<div className="card">
|
|
246
265
|
<button
|
|
247
266
|
onClick={() => {
|
|
248
|
-
fetch(
|
|
267
|
+
fetch("/api/")
|
|
249
268
|
.then((res) => res.json() as Promise<{ name: string }>)
|
|
250
|
-
.then((data) => setName(data.name))
|
|
269
|
+
.then((data) => setName(data.name));
|
|
251
270
|
}}
|
|
252
271
|
aria-label="get name"
|
|
253
272
|
>
|
|
@@ -261,10 +280,10 @@ function App() {
|
|
|
261
280
|
Click on the Vite and React logos to learn more
|
|
262
281
|
</p>
|
|
263
282
|
</>
|
|
264
|
-
)
|
|
283
|
+
);
|
|
265
284
|
}
|
|
266
285
|
|
|
267
|
-
export default App
|
|
286
|
+
export default App;
|
|
268
287
|
```
|
|
269
288
|
|
|
270
289
|
Now, if you click the button, it will display 'Name from API is: Cloudflare'.
|
|
@@ -310,12 +329,12 @@ The `cloudflare` plugin should be included in the Vite `plugins` array:
|
|
|
310
329
|
```ts
|
|
311
330
|
// vite.config.ts
|
|
312
331
|
|
|
313
|
-
import { defineConfig } from
|
|
314
|
-
import { cloudflare } from
|
|
332
|
+
import { defineConfig } from "vite";
|
|
333
|
+
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
315
334
|
|
|
316
335
|
export default defineConfig({
|
|
317
336
|
plugins: [cloudflare()],
|
|
318
|
-
})
|
|
337
|
+
});
|
|
319
338
|
```
|
|
320
339
|
|
|
321
340
|
It accepts an optional `PluginConfig` parameter.
|
|
@@ -447,6 +466,13 @@ The value of `MY_VAR` will therefore be `'Production var'`.
|
|
|
447
466
|
If you run `vite build --mode staging` then the 'staging' Vite mode will be used and the 'staging' Cloudflare environment will be selected.
|
|
448
467
|
The value of `MY_VAR` will therefore be `'Staging var'`.
|
|
449
468
|
|
|
469
|
+
## Secrets
|
|
470
|
+
|
|
471
|
+
Secrets can be provided to your Worker in local development using a [`.dev.vars`](https://developers.cloudflare.com/workers/configuration/secrets/#local-development-with-secrets) file. If you are using [Cloudflare Environments](#cloudflare-environments) then the relevant `.dev.vars` file will be selected. For example, `CLOUDFLARE_ENV=staging vite dev` will load `.dev.vars.staging` if it exists and fall back to `.dev.vars`.
|
|
472
|
+
|
|
473
|
+
> [!NOTE]
|
|
474
|
+
> The `vite build` command copies the relevant `.dev.vars[.env-name]` file to the output directory. This is only used when running `vite preview` and is not deployed with your Worker.
|
|
475
|
+
|
|
450
476
|
## Migrating from `wrangler dev`
|
|
451
477
|
|
|
452
478
|
Migrating from `wrangler dev` is a simple process and you can follow the instructions in the [Quick start](#quick-start) to get started.
|