@cloudflare/vite-plugin 0.0.0-e2f5756c2 → 0.0.0-e5dbedd78

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 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](#worker-environments) | [Migrating from `wrangler dev`](#migrating-from-wrangler-dev)
3
+ [Intro](#intro) | [Quick start](#quick-start) | [Tutorial](#tutorial) | [API](#api) | [Cloudflare environments](#cloudflare-environments) | [Debugging](#debugging) | [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
- ### Add the plugin to your Vite config
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 'vite'
31
- import { cloudflare } from '@cloudflare/vite-plugin'
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 = "my-worker"
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 (`vite dev`), build (`vite build`), preview (`vite preview`), and deploy (`wrangler deploy`) your application.
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 'vite'
92
- import react from '@vitejs/plugin-react'
93
- import { cloudflare } from '@cloudflare/vite-plugin'
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
@@ -121,6 +140,15 @@ The `directory` in the output configuration will automatically point to the clie
121
140
  > This output file is a snapshot of your configuration at the time of the build and is modified to reference your build artifacts.
122
141
  > It is the configuration that is used for preview and deployment.
123
142
 
143
+ #### Update the .gitignore file
144
+
145
+ Wrangler will use and/or generate temporary files that should not be stored in git. Add the following lines to the `.gitignore` file:
146
+
147
+ ```gitignore
148
+ .wrangler
149
+ .dev.vars
150
+ ```
151
+
124
152
  #### Run the development server
125
153
 
126
154
  Run `npm run dev` to verify that your application is working as expected.
@@ -181,22 +209,22 @@ The assets `binding` defined here will allow you to access the assets functional
181
209
  // api/index.ts
182
210
 
183
211
  interface Env {
184
- ASSETS: Fetcher
212
+ ASSETS: Fetcher;
185
213
  }
186
214
 
187
215
  export default {
188
216
  fetch(request, env) {
189
- const url = new URL(request.url)
217
+ const url = new URL(request.url);
190
218
 
191
- if (url.pathname.startsWith('/api/')) {
219
+ if (url.pathname.startsWith("/api/")) {
192
220
  return Response.json({
193
- name: 'Cloudflare',
194
- })
221
+ name: "Cloudflare",
222
+ });
195
223
  }
196
224
 
197
- return env.ASSETS.fetch(request)
225
+ return env.ASSETS.fetch(request);
198
226
  },
199
- } satisfies ExportedHandler<Env>
227
+ } satisfies ExportedHandler<Env>;
200
228
  ```
201
229
 
202
230
  The Worker above will be invoked for any request not matching a static asset.
@@ -211,14 +239,14 @@ Replace the file contents with the following code:
211
239
  ```tsx
212
240
  // src/App.tsx
213
241
 
214
- import { useState } from 'react'
215
- import reactLogo from './assets/react.svg'
216
- import viteLogo from '/vite.svg'
217
- import './App.css'
242
+ import { useState } from "react";
243
+ import reactLogo from "./assets/react.svg";
244
+ import viteLogo from "/vite.svg";
245
+ import "./App.css";
218
246
 
219
247
  function App() {
220
- const [count, setCount] = useState(0)
221
- const [name, setName] = useState('unknown')
248
+ const [count, setCount] = useState(0);
249
+ const [name, setName] = useState("unknown");
222
250
 
223
251
  return (
224
252
  <>
@@ -245,9 +273,9 @@ function App() {
245
273
  <div className="card">
246
274
  <button
247
275
  onClick={() => {
248
- fetch('/api/')
276
+ fetch("/api/")
249
277
  .then((res) => res.json() as Promise<{ name: string }>)
250
- .then((data) => setName(data.name))
278
+ .then((data) => setName(data.name));
251
279
  }}
252
280
  aria-label="get name"
253
281
  >
@@ -261,10 +289,10 @@ function App() {
261
289
  Click on the Vite and React logos to learn more
262
290
  </p>
263
291
  </>
264
- )
292
+ );
265
293
  }
266
294
 
267
- export default App
295
+ export default App;
268
296
  ```
269
297
 
270
298
  Now, if you click the button, it will display 'Name from API is: Cloudflare'.
@@ -310,12 +338,12 @@ The `cloudflare` plugin should be included in the Vite `plugins` array:
310
338
  ```ts
311
339
  // vite.config.ts
312
340
 
313
- import { defineConfig } from 'vite'
314
- import { cloudflare } from '@cloudflare/vite-plugin'
341
+ import { defineConfig } from "vite";
342
+ import { cloudflare } from "@cloudflare/vite-plugin";
315
343
 
316
344
  export default defineConfig({
317
345
  plugins: [cloudflare()],
318
- })
346
+ });
319
347
  ```
320
348
 
321
349
  It accepts an optional `PluginConfig` parameter.
@@ -346,6 +374,10 @@ It accepts an optional `PluginConfig` parameter.
346
374
  All requests are routed through your entry Worker.
347
375
  During the build, each Worker is output to a separate subdirectory of `dist`.
348
376
 
377
+ - `inspectorPort?: number | false`
378
+
379
+ Optional inspector port to use for debugging your workers, for more details on debugging see the [devtools section](#devtools). Can be set to `false` to disable the debugging inspector altogether. Defaults to `9229`.
380
+
349
381
  > [!NOTE]
350
382
  > When running `wrangler deploy`, only your main (entry) Worker will be deployed.
351
383
  > If using multiple Workers, each must be deployed individually.
@@ -447,6 +479,69 @@ The value of `MY_VAR` will therefore be `'Production var'`.
447
479
  If you run `vite build --mode staging` then the 'staging' Vite mode will be used and the 'staging' Cloudflare environment will be selected.
448
480
  The value of `MY_VAR` will therefore be `'Staging var'`.
449
481
 
482
+ ## Secrets
483
+
484
+ 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`.
485
+
486
+ > [!NOTE]
487
+ > 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.
488
+
489
+ ## Debugging
490
+
491
+ The Cloudflare Vite plugin allows you to conveniently debug your Worker code during local development.
492
+
493
+ By default the inspector port used by the plugin is `9229`, which can be customized by providing a different port to the plugin's `inspectorPort` option.
494
+
495
+ There are two recommended ways of doing so, which we'll explore in this section.
496
+
497
+ ### Devtools
498
+
499
+ When running `vite dev` or `vite preview` a `/__debug` route in your local server will be made available which gives you access to [Cloudflare's implementation](/packages/chrome-devtools-patches) of [Chrome's DevTools](https://developer.chrome.com/docs/devtools/overview).
500
+
501
+ Navigating to this route will open a devtools tab for each of the workers in your application (Note: in case of multiple workers you might need to allow your browser to open pop-ups).
502
+
503
+ Once the tab or tabs are open, you can make a request to your application and start debugging your workers' code.
504
+
505
+ Note: If you're not interested in debugging all your workers you can close the tabs of the workers you don't want to debug.
506
+
507
+ ### VS Code
508
+
509
+ To setup VS Code for breakpoint debugging for your application, you will need to create a `.vscode/launch.json` file that contains a configuration following this structure:
510
+
511
+ ```json
512
+ {
513
+ "configurations": [
514
+ {
515
+ "name": "<NAME_OF_WORKER>",
516
+ "type": "node",
517
+ "request": "attach",
518
+ "websocketAddress": "ws://localhost:9229/<NAME_OF_WORKER>",
519
+ "resolveSourceMapLocations": null,
520
+ "attachExistingChildren": false,
521
+ "autoAttachChildProcesses": false,
522
+ "sourceMaps": true
523
+ }
524
+ ],
525
+ "compounds": [
526
+ {
527
+ "name": "Debug All Workers",
528
+ "configurations": ["<NAME_OF_WORKER>"],
529
+ "stopAll": true
530
+ }
531
+ ]
532
+ }
533
+ ```
534
+
535
+ Where, `<NAME_OF_WORKER>` indicates the name of your worker as specified in your Wrangler configuration.
536
+
537
+ Note: if you customized your `inspectorPort` you need to use that port in the `websocketAddress` field.
538
+
539
+ If you have more than one worker you need add a configuration in the `configurations` field for each one and then include the configuration name in the `configurations` array in the compound configuration.
540
+
541
+ Once your `launch.json` file is ready, after running `vite dev` or `vite preview` you can select **Debug All Workers** at the top of the **Run & Debug** panel to attach debuggers to all the various Workers. Then you can add breakpoints to your code and start debugging.
542
+
543
+ Note: You can also manually select the configurations to run (e.g. **Debug Worker1**) to filter which workers you want to debug.
544
+
450
545
  ## Migrating from `wrangler dev`
451
546
 
452
547
  Migrating from `wrangler dev` is a simple process and you can follow the instructions in the [Quick start](#quick-start) to get started.