wrangler 0.0.0-af8b845 → 0.0.0-b53cbc8
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/package.json +6 -8
- package/src/__tests__/index.test.ts +65 -30
- package/src/__tests__/mock-cfetch.js +6 -13
- package/src/api/form_data.ts +9 -26
- package/src/api/inspect.ts +0 -11
- package/src/api/preview.ts +28 -23
- package/src/api/worker.ts +1 -1
- package/src/config.ts +0 -2
- package/src/dev.tsx +75 -35
- package/src/dialogs.tsx +2 -2
- package/src/index.tsx +53 -37
- package/src/kv.tsx +2 -2
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +586 -80
- package/src/publish.ts +9 -17
- package/src/user.tsx +1 -1
- package/wrangler-dist/cli.js +53883 -66440
- package/wrangler-dist/cli.js.map +3 -3
- package/src/util/fetch.ts +0 -74
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-b53cbc8",
|
|
4
4
|
"author": "wrangler@cloudflare.com",
|
|
5
5
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
6
6
|
"bin": {
|
|
@@ -39,10 +39,7 @@
|
|
|
39
39
|
"@cloudflare/pages-functions-compiler": "0.3.8",
|
|
40
40
|
"esbuild": "0.14.1",
|
|
41
41
|
"miniflare": "2.0.0-rc.3",
|
|
42
|
-
"semiver": "^1.1.0"
|
|
43
|
-
"serve": "^13.0.2",
|
|
44
|
-
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
|
45
|
-
"@esbuild-plugins/node-modules-polyfill": "^0.1.2"
|
|
42
|
+
"semiver": "^1.1.0"
|
|
46
43
|
},
|
|
47
44
|
"optionalDependencies": {
|
|
48
45
|
"fsevents": "~2.3.2"
|
|
@@ -55,15 +52,14 @@
|
|
|
55
52
|
"@types/signal-exit": "^3.0.1",
|
|
56
53
|
"@types/ws": "^8.2.1",
|
|
57
54
|
"@types/yargs": "^17.0.7",
|
|
55
|
+
"chokidar": "^3.5.2",
|
|
58
56
|
"clipboardy": "^3.0.0",
|
|
59
57
|
"command-exists": "^1.2.9",
|
|
60
58
|
"execa": "^6.0.0",
|
|
61
|
-
"express": "^4.17.1",
|
|
62
59
|
"finalhandler": "^1.1.2",
|
|
63
60
|
"find-up": "^6.2.0",
|
|
64
61
|
"formdata-node": "^4.3.1",
|
|
65
62
|
"http-proxy": "^1.18.1",
|
|
66
|
-
"http-proxy-middleware": "^2.0.1",
|
|
67
63
|
"ink": "^3.2.0",
|
|
68
64
|
"ink-select-input": "^4.2.1",
|
|
69
65
|
"ink-table": "^3.0.0",
|
|
@@ -75,8 +71,10 @@
|
|
|
75
71
|
"serve-static": "^1.14.1",
|
|
76
72
|
"signal-exit": "^3.0.6",
|
|
77
73
|
"tmp-promise": "^3.0.3",
|
|
74
|
+
"undici": "^4.11.1",
|
|
78
75
|
"ws": "^8.3.0",
|
|
79
|
-
"yargs": "^17.3.0"
|
|
76
|
+
"yargs": "^17.3.0",
|
|
77
|
+
"react-error-boundary": "^3.1.4"
|
|
80
78
|
},
|
|
81
79
|
"files": [
|
|
82
80
|
"src",
|
|
@@ -3,12 +3,9 @@ import * as fsp from "node:fs/promises";
|
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import * as TOML from "@iarna/toml";
|
|
5
5
|
import { main } from "../index";
|
|
6
|
-
|
|
7
|
-
import { setMock, unsetAllMocks } from "../cfetch";
|
|
6
|
+
import { setMock, unsetAllMocks } from "./mock-cfetch";
|
|
8
7
|
|
|
9
|
-
jest.mock("../cfetch", () =>
|
|
10
|
-
return jest.requireActual("./mock-cfetch");
|
|
11
|
-
});
|
|
8
|
+
jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
|
|
12
9
|
|
|
13
10
|
async function w(cmd: void | string, options?: { tap: boolean }) {
|
|
14
11
|
const tapped = options?.tap ? tap() : undefined;
|
|
@@ -44,31 +41,69 @@ function tap() {
|
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
describe("wrangler", () => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
44
|
+
describe("no command", () => {
|
|
45
|
+
it("should display a list of available commands", async () => {
|
|
46
|
+
const { stdout, stderr } = await w(undefined, { tap: true });
|
|
47
|
+
|
|
48
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
49
|
+
"wrangler
|
|
50
|
+
|
|
51
|
+
Commands:
|
|
52
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
53
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
54
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
55
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
56
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
57
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
58
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
59
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
60
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
61
|
+
|
|
62
|
+
Flags:
|
|
63
|
+
-c, --config Path to .toml configuration file [string]
|
|
64
|
+
-h, --help Show help [boolean]
|
|
65
|
+
-v, --version Show version number [boolean]
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
69
|
+
`);
|
|
70
|
+
|
|
71
|
+
expect(stderr).toEqual("");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("invalid command", () => {
|
|
76
|
+
it("should display an error", async () => {
|
|
77
|
+
const { stdout, stderr } = await w("invalid-command", { tap: true });
|
|
78
|
+
|
|
79
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
80
|
+
"wrangler
|
|
81
|
+
|
|
82
|
+
Commands:
|
|
83
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
84
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
85
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
86
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
87
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
88
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
89
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
90
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
91
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
92
|
+
|
|
93
|
+
Flags:
|
|
94
|
+
-c, --config Path to .toml configuration file [string]
|
|
95
|
+
-h, --help Show help [boolean]
|
|
96
|
+
-v, --version Show version number [boolean]
|
|
97
|
+
|
|
98
|
+
Options:
|
|
99
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
100
|
+
`);
|
|
101
|
+
|
|
102
|
+
expect(stderr).toMatchInlineSnapshot(`
|
|
103
|
+
"
|
|
104
|
+
Unknown command: invalid-command."
|
|
105
|
+
`);
|
|
106
|
+
});
|
|
72
107
|
});
|
|
73
108
|
|
|
74
109
|
describe("init", () => {
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// This file mocks ../cfetch.ts
|
|
2
2
|
// so we can insert whatever responses we want from it
|
|
3
3
|
|
|
4
|
-
const pathToRegexp = require("path-to-regexp");
|
|
4
|
+
const { pathToRegexp } = require("path-to-regexp");
|
|
5
5
|
// TODO: add jsdoc style types here
|
|
6
6
|
|
|
7
7
|
// type MockHandler = (resource: string, init?: RequestInit) => any; // TODO: use a generic here
|
|
8
8
|
|
|
9
9
|
let mocks = [];
|
|
10
10
|
|
|
11
|
-
function mockCfetch(resource, init) {
|
|
11
|
+
export function mockCfetch(resource, init) {
|
|
12
12
|
for (const { regexp, handler } of mocks) {
|
|
13
13
|
if (regexp.test(resource)) {
|
|
14
14
|
return handler(resource, init); // should we have some kind of fallthrough system? we'll see.
|
|
@@ -17,7 +17,7 @@ function mockCfetch(resource, init) {
|
|
|
17
17
|
throw new Error(`no mocks found for ${resource}`);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function setMock(resource, handler) {
|
|
20
|
+
export function setMock(resource, handler) {
|
|
21
21
|
const mock = {
|
|
22
22
|
resource,
|
|
23
23
|
handler,
|
|
@@ -29,18 +29,11 @@ function setMock(resource, handler) {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
function unsetAllMocks() {
|
|
32
|
+
export function unsetAllMocks() {
|
|
33
33
|
mocks = [];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const CF_API_BASE_URL =
|
|
36
|
+
export const CF_API_BASE_URL =
|
|
37
37
|
process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
__esModule: true,
|
|
41
|
-
default: mockCfetch,
|
|
42
|
-
mockCfetch,
|
|
43
|
-
setMock,
|
|
44
|
-
unsetAllMocks,
|
|
45
|
-
CF_API_BASE_URL,
|
|
46
|
-
});
|
|
39
|
+
export default mockCfetch;
|
package/src/api/form_data.ts
CHANGED
|
@@ -113,32 +113,15 @@ export function toFormData(worker: CfWorkerInit): FormData {
|
|
|
113
113
|
bindings.push(binding);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
};
|
|
126
|
-
if (compatibility_date) {
|
|
127
|
-
// @ts-expect-error - we should type metadata
|
|
128
|
-
metadata.compatibility_date = compatibility_date;
|
|
129
|
-
}
|
|
130
|
-
if (compatibility_flags) {
|
|
131
|
-
// @ts-expect-error - we should type metadata
|
|
132
|
-
metadata.compatibility_flags = compatibility_flags;
|
|
133
|
-
}
|
|
134
|
-
if (usage_model) {
|
|
135
|
-
// @ts-expect-error - we should type metadata
|
|
136
|
-
metadata.usage_model = usage_model;
|
|
137
|
-
}
|
|
138
|
-
if (migrations) {
|
|
139
|
-
// @ts-expect-error - we should type metadata
|
|
140
|
-
metadata.migrations = migrations;
|
|
141
|
-
}
|
|
116
|
+
// TODO: this object should be typed
|
|
117
|
+
const metadata = {
|
|
118
|
+
...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }),
|
|
119
|
+
bindings,
|
|
120
|
+
...(compatibility_date && { compatibility_date }),
|
|
121
|
+
...(compatibility_flags && { compatibility_flags }),
|
|
122
|
+
...(usage_model && { usage_model }),
|
|
123
|
+
...(migrations && { migrations }),
|
|
124
|
+
};
|
|
142
125
|
|
|
143
126
|
formData.set("metadata", JSON.stringify(metadata));
|
|
144
127
|
|
package/src/api/inspect.ts
CHANGED
|
@@ -140,17 +140,6 @@ export type DtEvent = DtLogEvent | DtExceptionEvent;
|
|
|
140
140
|
*/
|
|
141
141
|
export type DtListener = (event: DtEvent) => void;
|
|
142
142
|
|
|
143
|
-
interface DtProtocolRequest<T> {
|
|
144
|
-
id: number;
|
|
145
|
-
method: string;
|
|
146
|
-
params?: T;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
interface DtProtocolResponse<T> {
|
|
150
|
-
id: number;
|
|
151
|
-
result: T;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
143
|
/**
|
|
155
144
|
* A DevTools inspector that listens to logs and debug events from a Worker.
|
|
156
145
|
*
|
package/src/api/preview.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import cfetch from "../cfetch";
|
|
2
|
-
import { fetchJson } from "../util/fetch";
|
|
3
2
|
import { toFormData } from "./form_data";
|
|
4
3
|
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
4
|
+
import fetch from "node-fetch";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A preview mode.
|
|
@@ -9,7 +9,7 @@ import type { CfAccount, CfWorkerInit } from "./worker";
|
|
|
9
9
|
* * If true, then using a `workers.dev` subdomain.
|
|
10
10
|
* * Otherwise, a list of routes under a single zone.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* A preview token.
|
|
@@ -60,17 +60,17 @@ async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
|
|
|
60
60
|
? `/zones/${zoneId}/workers/edge-preview`
|
|
61
61
|
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
62
62
|
|
|
63
|
-
const { exchange_url
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const { host } = new URL(
|
|
63
|
+
const { exchange_url } = await cfetch<{ exchange_url: string }>(initUrl);
|
|
64
|
+
const { inspector_websocket, token } = (await (
|
|
65
|
+
await fetch(exchange_url)
|
|
66
|
+
).json()) as { inspector_websocket: string; token: string };
|
|
67
|
+
const { host } = new URL(inspector_websocket);
|
|
68
68
|
const query = `cf_workers_preview_token=${token}`;
|
|
69
69
|
|
|
70
70
|
return {
|
|
71
71
|
value: token,
|
|
72
72
|
host,
|
|
73
|
-
inspectorUrl: new URL(`${
|
|
73
|
+
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
|
|
74
74
|
prewarmUrl: new URL(
|
|
75
75
|
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
76
76
|
),
|
|
@@ -96,27 +96,32 @@ export async function previewToken(
|
|
|
96
96
|
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
|
|
97
97
|
|
|
98
98
|
const { accountId, zoneId } = account;
|
|
99
|
-
const scriptId = zoneId ? randomId() : host.split(".")[0];
|
|
99
|
+
const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
|
|
100
100
|
const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
101
101
|
|
|
102
|
-
const mode = zoneId
|
|
102
|
+
const mode: CfPreviewMode = zoneId
|
|
103
|
+
? { routes: ["*/*"] }
|
|
104
|
+
: { workers_dev: true };
|
|
105
|
+
|
|
103
106
|
const formData = toFormData(worker);
|
|
104
107
|
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
105
108
|
|
|
106
|
-
const { preview_token
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
);
|
|
109
|
+
const { preview_token } = await cfetch<{ preview_token: string }>(url, {
|
|
110
|
+
method: "POST",
|
|
111
|
+
// @ts-expect-error TODO: fix this
|
|
112
|
+
body: formData,
|
|
113
|
+
headers: {
|
|
114
|
+
"cf-preview-upload-config-token": value,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
117
118
|
return {
|
|
118
|
-
value:
|
|
119
|
-
|
|
119
|
+
value: preview_token,
|
|
120
|
+
// TODO: verify this works with zoned workers
|
|
121
|
+
host:
|
|
122
|
+
worker.name && !zoneId
|
|
123
|
+
? `${worker.name}.${host.split(".").slice(1).join(".")}`
|
|
124
|
+
: host,
|
|
120
125
|
inspectorUrl,
|
|
121
126
|
prewarmUrl,
|
|
122
127
|
};
|
package/src/api/worker.ts
CHANGED
package/src/config.ts
CHANGED
|
@@ -81,7 +81,6 @@ type Env = {
|
|
|
81
81
|
site?: Site;
|
|
82
82
|
jsx_factory?: string; // inherited
|
|
83
83
|
jsx_fragment?: string; // inherited
|
|
84
|
-
polyfill_node?: boolean; //inherited
|
|
85
84
|
// we should use typescript to parse cron patterns
|
|
86
85
|
triggers?: { crons: Cron[] }; // inherited
|
|
87
86
|
vars?: Vars;
|
|
@@ -108,7 +107,6 @@ export type Config = {
|
|
|
108
107
|
webpack_config?: string; // inherited
|
|
109
108
|
jsx_factory?: string; // inherited
|
|
110
109
|
jsx_fragment?: string; // inherited
|
|
111
|
-
polyfill_node?: boolean; //inherited
|
|
112
110
|
vars?: Vars;
|
|
113
111
|
migrations?: DOMigration[];
|
|
114
112
|
durable_objects?: { bindings: DurableObject[] };
|
package/src/dev.tsx
CHANGED
|
@@ -22,8 +22,8 @@ import commandExists from "command-exists";
|
|
|
22
22
|
import assert from "assert";
|
|
23
23
|
import { getAPIToken } from "./user";
|
|
24
24
|
import fetch from "node-fetch";
|
|
25
|
-
import
|
|
26
|
-
import
|
|
25
|
+
import makeModuleCollector from "./module-collection";
|
|
26
|
+
import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
|
|
27
27
|
|
|
28
28
|
type CfScriptFormat = void | "modules" | "service-worker";
|
|
29
29
|
|
|
@@ -42,10 +42,9 @@ type Props = {
|
|
|
42
42
|
compatibilityDate: void | string;
|
|
43
43
|
compatibilityFlags: void | string[];
|
|
44
44
|
usageModel: void | "bundled" | "unbound";
|
|
45
|
-
polyfillNode: void | boolean;
|
|
46
45
|
};
|
|
47
46
|
|
|
48
|
-
|
|
47
|
+
function Dev(props: Props): JSX.Element {
|
|
49
48
|
if (props.public && props.format === "service-worker") {
|
|
50
49
|
throw new Error(
|
|
51
50
|
"You cannot use the service worker format with a `public` directory."
|
|
@@ -61,7 +60,6 @@ export function Dev(props: Props): JSX.Element {
|
|
|
61
60
|
staticRoot: props.public,
|
|
62
61
|
jsxFactory: props.jsxFactory,
|
|
63
62
|
jsxFragment: props.jsxFragment,
|
|
64
|
-
polyfillNode: props.polyfillNode,
|
|
65
63
|
});
|
|
66
64
|
if (bundle && bundle.type === "commonjs" && !props.format && props.public) {
|
|
67
65
|
throw new Error(
|
|
@@ -173,7 +171,7 @@ function Remote(props: {
|
|
|
173
171
|
name: props.name,
|
|
174
172
|
bundle: props.bundle,
|
|
175
173
|
format: props.format,
|
|
176
|
-
modules: [],
|
|
174
|
+
modules: props.bundle ? props.bundle.modules : [],
|
|
177
175
|
accountId: props.accountId,
|
|
178
176
|
apiToken: props.apiToken,
|
|
179
177
|
variables: props.variables,
|
|
@@ -236,6 +234,7 @@ function useLocalWorker(props: {
|
|
|
236
234
|
}
|
|
237
235
|
|
|
238
236
|
console.log("⎔ Starting a local server...");
|
|
237
|
+
// TODO: just use execa for this
|
|
239
238
|
local.current = spawn("node", [
|
|
240
239
|
"--experimental-vm-modules",
|
|
241
240
|
"--inspect",
|
|
@@ -296,6 +295,17 @@ function useLocalWorker(props: {
|
|
|
296
295
|
}
|
|
297
296
|
});
|
|
298
297
|
|
|
298
|
+
local.current.on("exit", (code) => {
|
|
299
|
+
if (code !== 0) {
|
|
300
|
+
console.error(`miniflare process exited with code ${code}`);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
local.current.on("error", (error: Error) => {
|
|
305
|
+
console.error(`miniflare process failed to spawn`);
|
|
306
|
+
console.error(error);
|
|
307
|
+
});
|
|
308
|
+
|
|
299
309
|
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
300
310
|
console.log("⎔ Shutting down local server.");
|
|
301
311
|
local.current?.kill();
|
|
@@ -303,7 +313,9 @@ function useLocalWorker(props: {
|
|
|
303
313
|
});
|
|
304
314
|
}
|
|
305
315
|
|
|
306
|
-
startLocalWorker()
|
|
316
|
+
startLocalWorker().catch((err) => {
|
|
317
|
+
console.error("local worker:", err);
|
|
318
|
+
});
|
|
307
319
|
|
|
308
320
|
return () => {
|
|
309
321
|
if (local.current) {
|
|
@@ -320,6 +332,7 @@ function useLocalWorker(props: {
|
|
|
320
332
|
|
|
321
333
|
function useTmpDir(): string | void {
|
|
322
334
|
const [directory, setDirectory] = useState<DirectoryResult>();
|
|
335
|
+
const handleError = useErrorHandler();
|
|
323
336
|
useEffect(() => {
|
|
324
337
|
let dir: DirectoryResult;
|
|
325
338
|
async function create() {
|
|
@@ -329,15 +342,22 @@ function useTmpDir(): string | void {
|
|
|
329
342
|
return;
|
|
330
343
|
} catch (err) {
|
|
331
344
|
console.error("failed to create tmp dir");
|
|
332
|
-
console.error(err);
|
|
333
345
|
throw err;
|
|
334
346
|
}
|
|
335
347
|
}
|
|
336
|
-
create()
|
|
348
|
+
create().catch((err) => {
|
|
349
|
+
// we want to break here
|
|
350
|
+
// we can't do much without a temp dir anyway
|
|
351
|
+
handleError(err);
|
|
352
|
+
});
|
|
337
353
|
return () => {
|
|
338
|
-
dir.cleanup()
|
|
354
|
+
dir.cleanup().catch(() => {
|
|
355
|
+
// extremely unlikely,
|
|
356
|
+
// but it's 2021 after all
|
|
357
|
+
console.error("failed to cleanup tmp dir");
|
|
358
|
+
});
|
|
339
359
|
};
|
|
340
|
-
}, []);
|
|
360
|
+
}, [handleError]);
|
|
341
361
|
return directory?.path;
|
|
342
362
|
}
|
|
343
363
|
|
|
@@ -347,6 +367,7 @@ type EsbuildBundle = {
|
|
|
347
367
|
entry: string;
|
|
348
368
|
type: "esm" | "commonjs";
|
|
349
369
|
exports: string[];
|
|
370
|
+
modules: CfModule[];
|
|
350
371
|
};
|
|
351
372
|
|
|
352
373
|
function useEsbuild(props: {
|
|
@@ -355,26 +376,16 @@ function useEsbuild(props: {
|
|
|
355
376
|
staticRoot: void | string;
|
|
356
377
|
jsxFactory: string | void;
|
|
357
378
|
jsxFragment: string | void;
|
|
358
|
-
polyfillNode: boolean | void;
|
|
359
379
|
}): EsbuildBundle | void {
|
|
360
|
-
const {
|
|
361
|
-
entry,
|
|
362
|
-
destination,
|
|
363
|
-
staticRoot,
|
|
364
|
-
jsxFactory,
|
|
365
|
-
jsxFragment,
|
|
366
|
-
polyfillNode,
|
|
367
|
-
} = props;
|
|
380
|
+
const { entry, destination, staticRoot, jsxFactory, jsxFragment } = props;
|
|
368
381
|
const [bundle, setBundle] = useState<EsbuildBundle>();
|
|
369
382
|
useEffect(() => {
|
|
370
383
|
let result: esbuild.BuildResult;
|
|
371
384
|
async function build() {
|
|
372
385
|
if (!destination) return;
|
|
386
|
+
const moduleCollector = makeModuleCollector();
|
|
373
387
|
result = await esbuild.build({
|
|
374
388
|
entryPoints: [entry],
|
|
375
|
-
define: {
|
|
376
|
-
...(polyfillNode && { global: "globalThis" }),
|
|
377
|
-
},
|
|
378
389
|
bundle: true,
|
|
379
390
|
outdir: destination,
|
|
380
391
|
metafile: true,
|
|
@@ -383,12 +394,11 @@ function useEsbuild(props: {
|
|
|
383
394
|
loader: {
|
|
384
395
|
".js": "jsx",
|
|
385
396
|
},
|
|
386
|
-
plugins: polyfillNode
|
|
387
|
-
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
|
|
388
|
-
: undefined,
|
|
389
397
|
...(jsxFactory && { jsxFactory }),
|
|
390
398
|
...(jsxFragment && { jsxFragment }),
|
|
391
399
|
external: ["__STATIC_CONTENT_MANIFEST"],
|
|
400
|
+
conditions: ["worker", "browser"],
|
|
401
|
+
plugins: [moduleCollector.plugin],
|
|
392
402
|
// TODO: import.meta.url
|
|
393
403
|
watch: {
|
|
394
404
|
async onRebuild(error) {
|
|
@@ -412,13 +422,19 @@ function useEsbuild(props: {
|
|
|
412
422
|
path: chunks[0],
|
|
413
423
|
type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
|
|
414
424
|
exports: chunks[1].exports,
|
|
425
|
+
modules: moduleCollector.modules,
|
|
415
426
|
});
|
|
416
427
|
}
|
|
417
|
-
build()
|
|
428
|
+
build().catch((_err) => {
|
|
429
|
+
// esbuild already logs errors to stderr
|
|
430
|
+
// and we don't want to end the process
|
|
431
|
+
// on build errors anyway
|
|
432
|
+
// so this is a no-op error handler
|
|
433
|
+
});
|
|
418
434
|
return () => {
|
|
419
435
|
result?.stop();
|
|
420
436
|
};
|
|
421
|
-
}, [entry, destination, staticRoot, jsxFactory, jsxFragment
|
|
437
|
+
}, [entry, destination, staticRoot, jsxFactory, jsxFragment]);
|
|
422
438
|
return bundle;
|
|
423
439
|
}
|
|
424
440
|
|
|
@@ -486,7 +502,7 @@ function useWorker(props: {
|
|
|
486
502
|
|
|
487
503
|
const content = await readFile(bundle.path, "utf-8");
|
|
488
504
|
const init: CfWorkerInit = {
|
|
489
|
-
name
|
|
505
|
+
name,
|
|
490
506
|
main: {
|
|
491
507
|
name: path.basename(bundle.path),
|
|
492
508
|
type: format || bundle.type === "esm" ? "esm" : "commonjs",
|
|
@@ -518,7 +534,11 @@ function useWorker(props: {
|
|
|
518
534
|
);
|
|
519
535
|
console.log(`⬣ Listening at http://localhost:${port}`);
|
|
520
536
|
}
|
|
521
|
-
start()
|
|
537
|
+
start().catch((err) => {
|
|
538
|
+
// we want to log the error, but not end the process
|
|
539
|
+
// since it could recover after the developer fixes whatever's wrong
|
|
540
|
+
console.error("remote worker:", err);
|
|
541
|
+
});
|
|
522
542
|
}, [
|
|
523
543
|
name,
|
|
524
544
|
bundle,
|
|
@@ -662,12 +682,14 @@ function useTunnel(toggle: boolean) {
|
|
|
662
682
|
});
|
|
663
683
|
|
|
664
684
|
const hostName = await findTunnelHostname();
|
|
665
|
-
clipboardy.write(hostName);
|
|
685
|
+
await clipboardy.write(hostName);
|
|
666
686
|
console.log(`⬣ Sharing at ${hostName}, copied to clipboard.`);
|
|
667
687
|
}
|
|
668
688
|
}
|
|
669
689
|
|
|
670
|
-
startTunnel()
|
|
690
|
+
startTunnel().catch((err) => {
|
|
691
|
+
console.error("tunnel:", err);
|
|
692
|
+
});
|
|
671
693
|
|
|
672
694
|
return () => {
|
|
673
695
|
if (tunnel.current) {
|
|
@@ -689,14 +711,14 @@ function useHotkeys(initial: useHotkeysInitialState, port: number) {
|
|
|
689
711
|
// UGH, we should put port in context instead
|
|
690
712
|
const [toggles, setToggles] = useState(initial);
|
|
691
713
|
useInput(
|
|
692
|
-
(
|
|
714
|
+
async (
|
|
693
715
|
input,
|
|
694
716
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
695
717
|
key
|
|
696
718
|
) => {
|
|
697
719
|
switch (input) {
|
|
698
720
|
case "b": // open browser
|
|
699
|
-
open(
|
|
721
|
+
await open(
|
|
700
722
|
`http://localhost:${port}/`
|
|
701
723
|
// {
|
|
702
724
|
// app: {
|
|
@@ -706,7 +728,7 @@ function useHotkeys(initial: useHotkeysInitialState, port: number) {
|
|
|
706
728
|
);
|
|
707
729
|
break;
|
|
708
730
|
case "d": // toggle inspector
|
|
709
|
-
open(
|
|
731
|
+
await open(
|
|
710
732
|
`https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`
|
|
711
733
|
// {
|
|
712
734
|
// app: {
|
|
@@ -734,3 +756,21 @@ function useHotkeys(initial: useHotkeysInitialState, port: number) {
|
|
|
734
756
|
);
|
|
735
757
|
return toggles;
|
|
736
758
|
}
|
|
759
|
+
|
|
760
|
+
function ErrorFallback(props: {
|
|
761
|
+
error: Error;
|
|
762
|
+
resetErrorBoundary: () => void;
|
|
763
|
+
}) {
|
|
764
|
+
useEffect(() => {
|
|
765
|
+
console.error(props.error);
|
|
766
|
+
process.exit(1);
|
|
767
|
+
});
|
|
768
|
+
return (
|
|
769
|
+
<Box>
|
|
770
|
+
<Text>Something went wrong:</Text>
|
|
771
|
+
<Text>{props.error.message}</Text>
|
|
772
|
+
</Box>
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
export default withErrorBoundary(Dev, { FallbackComponent: ErrorFallback });
|
package/src/dialogs.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import { Box, Text, useInput, render } from "ink";
|
|
|
4
4
|
import TextInput from "ink-text-input";
|
|
5
5
|
type ConfirmProps = {
|
|
6
6
|
text: string;
|
|
7
|
-
onConfirm: (answer: boolean) => void
|
|
7
|
+
onConfirm: (answer: boolean) => void;
|
|
8
8
|
};
|
|
9
9
|
function Confirm(props: ConfirmProps) {
|
|
10
10
|
useInput((input: string) => {
|
|
@@ -43,7 +43,7 @@ type PromptProps = {
|
|
|
43
43
|
onSubmit: (text: string) => void;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
function Prompt(props: PromptProps) {
|
|
47
47
|
const [value, setValue] = useState("");
|
|
48
48
|
|
|
49
49
|
return (
|