Skip to content

Commit 8f0223a

Browse files
committed
fix: fix dev and publish
We introduced some bugs in recent PRs: - In #196, we broke being able to pass an entrypoint directly to the cli. In this PR, I just reverted that fix. I'll reopen #78 and we'll tackle it again later. (cc @jgentes) - In #215, we broke being able to publish a script by just passing `--latest` or `--compatibility-data` in the cli. This PR fixes that by reading the correct argument when choosing whether to publish. - In #247, we broke how we made requests by passing headers to requests. This PR reverts the changes made in `cfetch/internal.ts`. (cc @petebacondarwin) - In #244, we broke `dev` and it would immediately crash. This PR fixes the reference in `dev.tsx` that was breaking. (cc @petebacondarwin)
1 parent 6c13ff9 commit 8f0223a

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

.changeset/strong-flies-add.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: fix `dev` and `publish`
6+
7+
We introduced some bugs in recent PRs
8+
9+
- In https://github.com/cloudflare/wrangler2/pull/196, we broke being able to pass an entrypoint directly to the cli. In this PR, I just reverted that fix. I'll reopen https://github.com/cloudflare/wrangler2/issues/78 and we'll tackle it again later. (cc @jgentes)
10+
- In https://github.com/cloudflare/wrangler2/pull/215, we broke being able to publish a script by just passing `--latest` or `--compatibility-data` in the cli. This PR fixes that by reading the correct argument when choosing whether to publish.
11+
- In https://github.com/cloudflare/wrangler2/pull/247, we broke how we made requests by passing headers to requests. This PR reverts the changes made in `cfetch/internal.ts`. (cc @petebacondarwin)
12+
- In https://github.com/cloudflare/wrangler2/pull/244, we broke `dev` and it would immediately crash. This PR fixes the reference in `dev.tsx` that was breaking. (cc @petebacondarwin)

packages/wrangler/src/cfetch/internal.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fetch, { Headers } from "node-fetch";
2-
import type { RequestInit } from "node-fetch";
1+
import fetch from "node-fetch";
2+
import type { RequestInit, HeadersInit } from "node-fetch";
33
import { getAPIToken, loginOrRefreshIfRequired } from "../user";
44

55
export const CF_API_BASE_URL =
@@ -21,7 +21,7 @@ export async function fetchInternal<ResponseType>(
2121
): Promise<ResponseType> {
2222
await requireLoggedIn();
2323
const apiToken = requireApiToken();
24-
const headers = new Headers(init.headers);
24+
const headers = cloneHeaders(init.headers);
2525
addAuthorizationHeader(headers, apiToken);
2626

2727
const queryString = queryParams ? `?${queryParams.toString()}` : "";
@@ -40,6 +40,10 @@ export async function fetchInternal<ResponseType>(
4040
}
4141
}
4242

43+
function cloneHeaders(headers: HeadersInit): HeadersInit {
44+
return { ...headers };
45+
}
46+
4347
async function requireLoggedIn(): Promise<void> {
4448
const loggedIn = await loginOrRefreshIfRequired();
4549
if (!loggedIn) {
@@ -55,7 +59,7 @@ function requireApiToken(): string {
5559
return apiToken;
5660
}
5761

58-
function addAuthorizationHeader(headers: Headers, apiToken: string): void {
62+
function addAuthorizationHeader(headers: HeadersInit, apiToken: string): void {
5963
if (headers["Authorization"]) {
6064
throw new Error(
6165
"The request already specifies an authorisation header - cannot add a new one."

packages/wrangler/src/dev.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ function useEsbuild(props: {
484484
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
485485
const metafile = result.metafile!;
486486
const outputEntry = Object.entries(metafile.outputs).find(
487-
([_path, { entryPoint }]) =>
488-
entryPoint === Object.keys(metafile.inputs)[0]
487+
([_path, { entryPoint }]) => entryPoint === entry
489488
); // assumedly only one entry point
489+
490490
if (outputEntry === undefined) {
491491
throw new Error(
492492
`Cannot find entry-point "${entry}" in generated bundle.`
@@ -508,7 +508,9 @@ function useEsbuild(props: {
508508
// so this is a no-op error handler
509509
});
510510
return () => {
511-
result.stop?.();
511+
if (result && result.stop) {
512+
result.stop();
513+
}
512514
};
513515
}, [entry, destination, staticRoot, jsxFactory, jsxFragment]);
514516
return bundle;
@@ -776,7 +778,7 @@ function ErrorFallback(props: { error: Error }) {
776778
return (
777779
<>
778780
<Text>Something went wrong:</Text>
779-
<Text>{props.error.message}</Text>
781+
<Text>{props.error.stack}</Text>
780782
</>
781783
);
782784
}

packages/wrangler/src/publish.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default async function publish(props: Props): Promise<void> {
5454
props.env && config.env ? config.env[props.env] || {} : config;
5555

5656
assert(
57-
envRootObj.compatibility_date || props["compatibility-date"],
57+
envRootObj.compatibility_date || props.compatibilityDate,
5858
"A compatibility_date is required when publishing. Add one to your wrangler.toml file, or pass it in your terminal as --compatibility_date. See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
5959
);
6060

@@ -139,7 +139,7 @@ export default async function publish(props: Props): Promise<void> {
139139
const metafile = result.metafile!;
140140
const expectedEntryPoint = props.public
141141
? path.join(path.dirname(file), "static-asset-facade.js")
142-
: Object.keys(metafile.inputs)[0];
142+
: file;
143143
const outputEntry = Object.entries(metafile.outputs).find(
144144
([, { entryPoint }]) => entryPoint === expectedEntryPoint
145145
);

0 commit comments

Comments
 (0)