wrangler 0.0.0-e09abbc → 0.0.0-e0abf917

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.
Files changed (186) hide show
  1. package/README.md +24 -12
  2. package/bin/wrangler.js +26 -67
  3. package/config-schema.json +2900 -0
  4. package/package.json +224 -167
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +0 -4
  11. package/templates/init-tests/test-jest-new-worker.js +23 -0
  12. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +67 -0
  15. package/templates/middleware/loader-modules.ts +133 -0
  16. package/templates/middleware/loader-sw.ts +229 -0
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-pretty-error.ts +40 -0
  20. package/templates/middleware/middleware-scheduled.ts +15 -0
  21. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  22. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  23. package/templates/modules-watch-stub.js +4 -0
  24. package/templates/new-worker-scheduled.ts +1 -1
  25. package/templates/new-worker.js +1 -1
  26. package/templates/new-worker.ts +4 -1
  27. package/templates/pages-dev-pipeline.ts +32 -0
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +1 -1
  30. package/templates/pages-template-plugin.ts +51 -16
  31. package/templates/pages-template-worker.ts +48 -11
  32. package/templates/startDevWorker/InspectorProxyWorker.ts +661 -0
  33. package/templates/startDevWorker/ProxyWorker.ts +331 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +105 -0
  36. package/templates/tsconfig.json +5 -102
  37. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  38. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  39. package/wrangler-dist/ProxyWorker.js +240 -0
  40. package/wrangler-dist/ProxyWorker.js.map +6 -0
  41. package/wrangler-dist/cli.d.ts +23804 -148
  42. package/wrangler-dist/cli.js +167847 -117611
  43. package/wrangler-dist/wasm-sync.wasm +0 -0
  44. package/import_meta_url.js +0 -3
  45. package/miniflare-config-stubs/.env.empty +0 -0
  46. package/miniflare-config-stubs/package.empty.json +0 -1
  47. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  48. package/miniflare-dist/index.mjs +0 -5354
  49. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  50. package/src/__tests__/config-cache.test.ts +0 -42
  51. package/src/__tests__/configuration.test.ts +0 -3950
  52. package/src/__tests__/dev.test.tsx +0 -1203
  53. package/src/__tests__/guess-worker-format.test.ts +0 -85
  54. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  55. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  56. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  57. package/src/__tests__/helpers/mock-bin.ts +0 -36
  58. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  59. package/src/__tests__/helpers/mock-console.ts +0 -97
  60. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  61. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  62. package/src/__tests__/helpers/mock-istty.ts +0 -74
  63. package/src/__tests__/helpers/mock-kv.ts +0 -33
  64. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -250
  65. package/src/__tests__/helpers/mock-process.ts +0 -39
  66. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  67. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  68. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  69. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  70. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  71. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  72. package/src/__tests__/https-options.test.ts +0 -163
  73. package/src/__tests__/index.test.ts +0 -262
  74. package/src/__tests__/init.test.ts +0 -1992
  75. package/src/__tests__/jest.setup.ts +0 -120
  76. package/src/__tests__/kv.test.ts +0 -1671
  77. package/src/__tests__/logger.test.ts +0 -141
  78. package/src/__tests__/metrics.test.ts +0 -445
  79. package/src/__tests__/package-manager.test.ts +0 -353
  80. package/src/__tests__/pages.test.ts +0 -1261
  81. package/src/__tests__/parse.test.ts +0 -330
  82. package/src/__tests__/publish.test.ts +0 -6671
  83. package/src/__tests__/pubsub.test.ts +0 -367
  84. package/src/__tests__/r2.test.ts +0 -233
  85. package/src/__tests__/route.test.ts +0 -45
  86. package/src/__tests__/secret.test.ts +0 -492
  87. package/src/__tests__/tail.test.ts +0 -698
  88. package/src/__tests__/test-old-node-version.js +0 -31
  89. package/src/__tests__/user.test.ts +0 -143
  90. package/src/__tests__/whoami.test.tsx +0 -230
  91. package/src/__tests__/worker-namespace.test.ts +0 -327
  92. package/src/abort.d.ts +0 -3
  93. package/src/api/dev.ts +0 -86
  94. package/src/api/index.ts +0 -1
  95. package/src/bundle-reporter.tsx +0 -29
  96. package/src/bundle.ts +0 -236
  97. package/src/cfetch/index.ts +0 -121
  98. package/src/cfetch/internal.ts +0 -149
  99. package/src/cli.ts +0 -27
  100. package/src/config/README.md +0 -107
  101. package/src/config/config.ts +0 -251
  102. package/src/config/diagnostics.ts +0 -80
  103. package/src/config/environment.ts +0 -453
  104. package/src/config/index.ts +0 -238
  105. package/src/config/validation-helpers.ts +0 -584
  106. package/src/config/validation.ts +0 -1754
  107. package/src/config-cache.ts +0 -84
  108. package/src/create-worker-preview.ts +0 -255
  109. package/src/create-worker-upload-form.ts +0 -263
  110. package/src/dev/dev-vars.ts +0 -36
  111. package/src/dev/dev.tsx +0 -452
  112. package/src/dev/local.tsx +0 -373
  113. package/src/dev/remote.tsx +0 -386
  114. package/src/dev/use-esbuild.ts +0 -150
  115. package/src/dev.tsx +0 -593
  116. package/src/dialogs.tsx +0 -135
  117. package/src/durable.ts +0 -102
  118. package/src/entry.ts +0 -328
  119. package/src/environment-variables.ts +0 -35
  120. package/src/errors.ts +0 -11
  121. package/src/faye-websocket.d.ts +0 -6
  122. package/src/generate.ts +0 -33
  123. package/src/git-client.ts +0 -42
  124. package/src/global-wrangler-config-path.ts +0 -26
  125. package/src/https-options.ts +0 -127
  126. package/src/index.tsx +0 -1971
  127. package/src/init.ts +0 -549
  128. package/src/inspect.ts +0 -662
  129. package/src/intl-polyfill.d.ts +0 -139
  130. package/src/is-interactive.ts +0 -12
  131. package/src/kv.ts +0 -430
  132. package/src/logger.ts +0 -73
  133. package/src/metrics/index.ts +0 -4
  134. package/src/metrics/is-ci.ts +0 -14
  135. package/src/metrics/metrics-config.ts +0 -239
  136. package/src/metrics/metrics-dispatcher.ts +0 -95
  137. package/src/metrics/send-event.ts +0 -92
  138. package/src/miniflare-cli/README.md +0 -30
  139. package/src/miniflare-cli/assets.ts +0 -543
  140. package/src/miniflare-cli/enum-keys.ts +0 -17
  141. package/src/miniflare-cli/index.ts +0 -85
  142. package/src/miniflare-cli/request-context.ts +0 -40
  143. package/src/module-collection.ts +0 -258
  144. package/src/open-in-browser.ts +0 -17
  145. package/src/package-manager.ts +0 -219
  146. package/src/pages/build.tsx +0 -204
  147. package/src/pages/constants.ts +0 -7
  148. package/src/pages/deployments.tsx +0 -106
  149. package/src/pages/dev.tsx +0 -401
  150. package/src/pages/functions/buildPlugin.ts +0 -105
  151. package/src/pages/functions/buildWorker.ts +0 -151
  152. package/src/pages/functions/filepath-routing.test.ts +0 -234
  153. package/src/pages/functions/filepath-routing.ts +0 -189
  154. package/src/pages/functions/identifiers.ts +0 -78
  155. package/src/pages/functions/routes.ts +0 -151
  156. package/src/pages/index.tsx +0 -84
  157. package/src/pages/projects.tsx +0 -163
  158. package/src/pages/publish.tsx +0 -338
  159. package/src/pages/types.ts +0 -40
  160. package/src/pages/upload.tsx +0 -396
  161. package/src/pages/utils.ts +0 -12
  162. package/src/parse.ts +0 -286
  163. package/src/paths.ts +0 -26
  164. package/src/preview.ts +0 -31
  165. package/src/proxy.ts +0 -476
  166. package/src/publish.ts +0 -793
  167. package/src/pubsub/index.ts +0 -286
  168. package/src/pubsub/pubsub-commands.tsx +0 -623
  169. package/src/r2.ts +0 -50
  170. package/src/selfsigned.d.ts +0 -29
  171. package/src/sites.tsx +0 -372
  172. package/src/tail/filters.ts +0 -279
  173. package/src/tail/index.ts +0 -299
  174. package/src/tail/printing.ts +0 -67
  175. package/src/update-check.ts +0 -19
  176. package/src/user/choose-account.tsx +0 -69
  177. package/src/user/env-vars.ts +0 -46
  178. package/src/user/generate-auth-url.ts +0 -33
  179. package/src/user/generate-random-state.ts +0 -16
  180. package/src/user/index.ts +0 -3
  181. package/src/user/user.tsx +0 -1171
  182. package/src/whoami.tsx +0 -91
  183. package/src/worker-namespace.ts +0 -206
  184. package/src/worker.ts +0 -191
  185. package/src/zones.ts +0 -76
  186. package/templates/static-asset-facade.js +0 -49
@@ -1,386 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import React, { useState, useEffect, useRef } from "react";
4
- import { useErrorHandler } from "react-error-boundary";
5
- import { printBundleSize } from "../bundle-reporter";
6
- import {
7
- createPreviewSession,
8
- createWorkerPreview,
9
- } from "../create-worker-preview";
10
- import useInspector from "../inspect";
11
- import { logger } from "../logger";
12
- import { usePreviewServer } from "../proxy";
13
- import { syncAssets } from "../sites";
14
- import {
15
- ChooseAccount,
16
- getAccountChoices,
17
- requireApiToken,
18
- saveAccountToCache,
19
- } from "../user";
20
- import type { Route } from "../config/environment";
21
- import type {
22
- CfPreviewToken,
23
- CfPreviewSession,
24
- } from "../create-worker-preview";
25
- import type { AssetPaths } from "../sites";
26
- import type { ChooseAccountItem } from "../user";
27
- import type {
28
- CfModule,
29
- CfWorkerInit,
30
- CfScriptFormat,
31
- CfAccount,
32
- CfWorkerContext,
33
- } from "../worker";
34
- import type { EsbuildBundle } from "./use-esbuild";
35
-
36
- export function Remote(props: {
37
- name: string | undefined;
38
- bundle: EsbuildBundle | undefined;
39
- format: CfScriptFormat | undefined;
40
- isWorkersSite: boolean;
41
- assetPaths: AssetPaths | undefined;
42
- port: number;
43
- ip: string;
44
- localProtocol: "https" | "http";
45
- inspect: boolean;
46
- inspectorPort: number;
47
- accountId: string | undefined;
48
- bindings: CfWorkerInit["bindings"];
49
- compatibilityDate: string;
50
- compatibilityFlags: string[] | undefined;
51
- usageModel: "bundled" | "unbound" | undefined;
52
- env: string | undefined;
53
- legacyEnv: boolean | undefined;
54
- zone: string | undefined;
55
- host: string | undefined;
56
- routes: Route[] | undefined;
57
- onReady?: (() => void) | undefined;
58
- }) {
59
- const [accountId, setAccountId] = useState(props.accountId);
60
- const accountChoicesRef = useRef<Promise<ChooseAccountItem[]>>();
61
- const [accountChoices, setAccountChoices] = useState<ChooseAccountItem[]>();
62
-
63
- const previewToken = useWorker({
64
- name: props.name,
65
- bundle: props.bundle,
66
- format: props.format,
67
- modules: props.bundle ? props.bundle.modules : [],
68
- accountId,
69
- bindings: props.bindings,
70
- assetPaths: props.assetPaths,
71
- isWorkersSite: props.isWorkersSite,
72
- port: props.port,
73
- compatibilityDate: props.compatibilityDate,
74
- compatibilityFlags: props.compatibilityFlags,
75
- usageModel: props.usageModel,
76
- env: props.env,
77
- legacyEnv: props.legacyEnv,
78
- zone: props.zone,
79
- host: props.host,
80
- routes: props.routes,
81
- onReady: props.onReady,
82
- });
83
-
84
- usePreviewServer({
85
- previewToken,
86
- assetDirectory: props.isWorkersSite
87
- ? undefined
88
- : props.assetPaths?.assetDirectory,
89
- localProtocol: props.localProtocol,
90
- localPort: props.port,
91
- ip: props.ip,
92
- });
93
-
94
- useInspector({
95
- inspectorUrl:
96
- props.inspect && previewToken
97
- ? previewToken.inspectorUrl.href
98
- : undefined,
99
- port: props.inspectorPort,
100
- logToTerminal: true,
101
- });
102
-
103
- const errorHandler = useErrorHandler();
104
-
105
- // This effect handles the async step of fetching the available accounts for the current user.
106
- // If only one account is available then it is just used by calling `setAccountId()`.
107
- useEffect(() => {
108
- if (
109
- accountChoicesRef.current !== undefined ||
110
- props.accountId !== undefined
111
- ) {
112
- return;
113
- }
114
- accountChoicesRef.current = getAccountChoices();
115
- accountChoicesRef.current.then(
116
- (accounts) => {
117
- if (accounts.length === 1) {
118
- saveAccountToCache({
119
- id: accounts[0].id,
120
- name: accounts[0].name,
121
- });
122
- setAccountId(accounts[0].id);
123
- } else {
124
- setAccountChoices(accounts);
125
- }
126
- },
127
- (err) => {
128
- errorHandler(err);
129
- }
130
- );
131
- });
132
-
133
- // If we have not already chosen an account and there are multiple accounts available
134
- // allow the users to select one.
135
- return accountId === undefined && accountChoices !== undefined ? (
136
- <ChooseAccount
137
- accounts={accountChoices}
138
- onSelect={(selectedAccount) => {
139
- saveAccountToCache(selectedAccount);
140
- setAccountId(selectedAccount.id);
141
- }}
142
- onError={(err) => errorHandler(err)}
143
- ></ChooseAccount>
144
- ) : null;
145
- }
146
-
147
- export function useWorker(props: {
148
- name: string | undefined;
149
- bundle: EsbuildBundle | undefined;
150
- format: CfScriptFormat | undefined;
151
- modules: CfModule[];
152
- accountId: string | undefined;
153
- bindings: CfWorkerInit["bindings"];
154
- assetPaths: AssetPaths | undefined;
155
- isWorkersSite: boolean;
156
- port: number;
157
- compatibilityDate: string | undefined;
158
- compatibilityFlags: string[] | undefined;
159
- usageModel: "bundled" | "unbound" | undefined;
160
- env: string | undefined;
161
- legacyEnv: boolean | undefined;
162
- zone: string | undefined;
163
- host: string | undefined;
164
- routes: Route[] | undefined;
165
- onReady: (() => void) | undefined;
166
- }): CfPreviewToken | undefined {
167
- const {
168
- name,
169
- bundle,
170
- format,
171
- modules,
172
- accountId,
173
- bindings,
174
- assetPaths,
175
- compatibilityDate,
176
- compatibilityFlags,
177
- usageModel,
178
- port,
179
- onReady,
180
- } = props;
181
- const [session, setSession] = useState<CfPreviewSession | undefined>();
182
- const [token, setToken] = useState<CfPreviewToken | undefined>();
183
-
184
- // This is the most reliable way to detect whether
185
- // something's "happened" in our system; We make a ref and
186
- // mark it once we log our initial message. Refs are vars!
187
- const startedRef = useRef(false);
188
-
189
- // This effect sets up the preview session
190
- useEffect(() => {
191
- const abortController = new AbortController();
192
- async function start() {
193
- if (accountId === undefined) {
194
- return;
195
- }
196
-
197
- const workerAccount: CfAccount = {
198
- accountId,
199
- apiToken: requireApiToken(),
200
- };
201
-
202
- const workerCtx: CfWorkerContext = {
203
- env: props.env,
204
- legacyEnv: props.legacyEnv,
205
- zone: props.zone,
206
- host: props.host,
207
- routes: props.routes,
208
- };
209
-
210
- setSession(
211
- await createPreviewSession(
212
- workerAccount,
213
- workerCtx,
214
- abortController.signal
215
- )
216
- );
217
- }
218
- start().catch((err) => {
219
- // we want to log the error, but not end the process
220
- // since it could recover after the developer fixes whatever's wrong
221
- if ((err as { code: string }).code !== "ABORT_ERR") {
222
- logger.error("Error while creating remote dev session:", err);
223
- }
224
- });
225
-
226
- return () => {
227
- abortController.abort();
228
- };
229
- }, [
230
- accountId,
231
- props.env,
232
- props.host,
233
- props.legacyEnv,
234
- props.routes,
235
- props.zone,
236
- ]);
237
-
238
- // This effect uses the session to upload the worker and create a preview
239
- useEffect(() => {
240
- const abortController = new AbortController();
241
- async function start() {
242
- if (accountId === undefined) {
243
- return;
244
- }
245
- if (session === undefined) {
246
- return;
247
- }
248
- setToken(undefined); // reset token in case we're re-running
249
-
250
- if (!bundle || !format) return;
251
-
252
- if (!startedRef.current) {
253
- startedRef.current = true;
254
- } else {
255
- logger.log("⎔ Detected changes, restarted server.");
256
- }
257
-
258
- const content = await readFile(bundle.path, "utf-8");
259
-
260
- // TODO: For Dev we could show the reporter message in the interactive box.
261
- void printBundleSize(
262
- { name: path.basename(bundle.path), content: content },
263
- modules
264
- );
265
-
266
- const assets = await syncAssets(
267
- accountId,
268
- // When we're using the newer service environments, we wouldn't
269
- // have added the env name on to the script name. However, we must
270
- // include it in the kv namespace name regardless (since there's no
271
- // concept of service environments for kv namespaces yet).
272
- name + (!props.legacyEnv && props.env ? `-${props.env}` : ""),
273
- props.isWorkersSite ? assetPaths : undefined,
274
- true,
275
- false
276
- ); // TODO: cancellable?
277
-
278
- const init: CfWorkerInit = {
279
- name,
280
- main: {
281
- name: path.basename(bundle.path),
282
- type: format === "modules" ? "esm" : "commonjs",
283
- content,
284
- },
285
- modules: modules.concat(
286
- assets.manifest
287
- ? {
288
- name: "__STATIC_CONTENT_MANIFEST",
289
- content: JSON.stringify(assets.manifest),
290
- type: "text",
291
- }
292
- : []
293
- ),
294
- bindings: {
295
- ...bindings,
296
- kv_namespaces: (bindings.kv_namespaces || []).concat(
297
- assets.namespace
298
- ? { binding: "__STATIC_CONTENT", id: assets.namespace }
299
- : []
300
- ),
301
- text_blobs: {
302
- ...bindings.text_blobs,
303
- ...(assets.manifest &&
304
- format === "service-worker" && {
305
- __STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
306
- }),
307
- },
308
- },
309
- migrations: undefined, // no migrations in dev
310
- compatibility_date: compatibilityDate,
311
- compatibility_flags: compatibilityFlags,
312
- usage_model: usageModel,
313
- };
314
-
315
- const workerAccount: CfAccount = {
316
- accountId,
317
- apiToken: requireApiToken(),
318
- };
319
-
320
- const workerCtx: CfWorkerContext = {
321
- env: props.env,
322
- legacyEnv: props.legacyEnv,
323
- zone: props.zone,
324
- host: props.host,
325
- routes: props.routes,
326
- };
327
-
328
- setToken(
329
- await createWorkerPreview(
330
- init,
331
- workerAccount,
332
- workerCtx,
333
- session,
334
- abortController.signal
335
- )
336
- );
337
- onReady?.();
338
- }
339
- start().catch((err) => {
340
- // we want to log the error, but not end the process
341
- // since it could recover after the developer fixes whatever's wrong
342
- if ((err as { code: string }).code !== "ABORT_ERR") {
343
- // instead of logging the raw API error to the user,
344
- // give them friendly instructions
345
- // for error 10063 (workers.dev subdomain required)
346
- if (err.code === 10063) {
347
- const errorMessage =
348
- "Error: You need to register a workers.dev subdomain before running the dev command in remote mode";
349
- const solutionMessage =
350
- "You can either enable local mode by pressing l, or register a workers.dev subdomain here:";
351
- const onboardingLink = `https://dash.cloudflare.com/${accountId}/workers/onboarding`;
352
- logger.error(
353
- `${errorMessage}\n${solutionMessage}\n${onboardingLink}`
354
- );
355
- } else {
356
- logger.error("Error on remote worker:", err);
357
- }
358
- }
359
- });
360
-
361
- return () => {
362
- abortController.abort();
363
- };
364
- }, [
365
- name,
366
- bundle,
367
- format,
368
- accountId,
369
- port,
370
- assetPaths,
371
- props.isWorkersSite,
372
- compatibilityDate,
373
- compatibilityFlags,
374
- usageModel,
375
- bindings,
376
- modules,
377
- props.env,
378
- props.legacyEnv,
379
- props.zone,
380
- props.host,
381
- props.routes,
382
- session,
383
- onReady,
384
- ]);
385
- return token;
386
- }
@@ -1,150 +0,0 @@
1
- import assert from "node:assert";
2
- import { watch } from "chokidar";
3
- import { useApp } from "ink";
4
- import { useState, useEffect } from "react";
5
- import { bundleWorker } from "../bundle";
6
- import { logger } from "../logger";
7
- import type { Config } from "../config";
8
- import type { Entry } from "../entry";
9
- import type { CfModule } from "../worker";
10
- import type { WatchMode } from "esbuild";
11
-
12
- export type EsbuildBundle = {
13
- id: number;
14
- path: string;
15
- entry: Entry;
16
- type: "esm" | "commonjs";
17
- modules: CfModule[];
18
- };
19
-
20
- export function useEsbuild({
21
- entry,
22
- destination,
23
- jsxFactory,
24
- jsxFragment,
25
- rules,
26
- serveAssetsFromWorker,
27
- tsconfig,
28
- minify,
29
- nodeCompat,
30
- define,
31
- noBundle,
32
- }: {
33
- entry: Entry;
34
- destination: string | undefined;
35
- jsxFactory: string | undefined;
36
- jsxFragment: string | undefined;
37
- rules: Config["rules"];
38
- define: Config["define"];
39
- serveAssetsFromWorker: boolean;
40
- tsconfig: string | undefined;
41
- minify: boolean | undefined;
42
- nodeCompat: boolean | undefined;
43
- noBundle: boolean;
44
- }): EsbuildBundle | undefined {
45
- const [bundle, setBundle] = useState<EsbuildBundle>();
46
- const { exit } = useApp();
47
- useEffect(() => {
48
- let stopWatching: (() => void) | undefined = undefined;
49
-
50
- function updateBundle() {
51
- // nothing really changes here, so let's increment the id
52
- // to change the return object's identity
53
- setBundle((previousBundle) => {
54
- assert(
55
- previousBundle,
56
- "Rebuild triggered with no previous build available"
57
- );
58
- return { ...previousBundle, id: previousBundle.id + 1 };
59
- });
60
- }
61
-
62
- const watchMode: WatchMode = {
63
- async onRebuild(error) {
64
- if (error) logger.error("Watch build failed:", error);
65
- else {
66
- updateBundle();
67
- }
68
- },
69
- };
70
-
71
- async function build() {
72
- if (!destination) return;
73
-
74
- const {
75
- resolvedEntryPointPath,
76
- bundleType,
77
- modules,
78
- stop,
79
- }: Awaited<ReturnType<typeof bundleWorker>> = noBundle
80
- ? {
81
- modules: [],
82
- resolvedEntryPointPath: entry.file,
83
- bundleType: entry.format === "modules" ? "esm" : "commonjs",
84
- stop: undefined,
85
- }
86
- : await bundleWorker(entry, destination, {
87
- serveAssetsFromWorker,
88
- jsxFactory,
89
- jsxFragment,
90
- rules,
91
- watch: watchMode,
92
- tsconfig,
93
- minify,
94
- nodeCompat,
95
- define,
96
- checkFetch: true,
97
- });
98
-
99
- // Capture the `stop()` method to use as the `useEffect()` destructor.
100
- stopWatching = stop;
101
-
102
- // if "noBundle" is true, then we need to manually watch the entry point and
103
- // trigger "builds" when it changes
104
- if (noBundle) {
105
- const watcher = watch(entry.file, {
106
- persistent: true,
107
- }).on("change", async (_event) => {
108
- updateBundle();
109
- });
110
-
111
- stopWatching = () => {
112
- watcher.close();
113
- };
114
- }
115
-
116
- setBundle({
117
- id: 0,
118
- entry,
119
- path: resolvedEntryPointPath,
120
- type: bundleType,
121
- modules,
122
- });
123
- }
124
-
125
- build().catch((err) => {
126
- // If esbuild fails on first run, we want to quit the process
127
- // since we can't recover from here
128
- // related: https://github.com/evanw/esbuild/issues/1037
129
- exit(err);
130
- });
131
-
132
- return () => {
133
- stopWatching?.();
134
- };
135
- }, [
136
- entry,
137
- destination,
138
- jsxFactory,
139
- jsxFragment,
140
- serveAssetsFromWorker,
141
- rules,
142
- tsconfig,
143
- exit,
144
- noBundle,
145
- minify,
146
- nodeCompat,
147
- define,
148
- ]);
149
- return bundle;
150
- }