wrangler 0.0.0-eab9542 → 0.0.0-eaf71b86c

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