Skip to content

Commit

Permalink
[wrangler] Make wrangler dev --experimental-local the default 🎉 (cl…
Browse files Browse the repository at this point in the history
…oudflare#3224)

* Use Miniflare 3 for `--local` and deprecate `--experimental-local`

...and make `--local` the default and add `--remote` flag

* Revert "fix: c3 arguments"

This reverts commit 13abe44.
  • Loading branch information
mrbbot authored and petebacondarwin committed May 17, 2023
1 parent 72a359b commit bac9b5d
Show file tree
Hide file tree
Showing 66 changed files with 1,253 additions and 2,076 deletions.
9 changes: 9 additions & 0 deletions .changeset/khaki-oranges-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": major
---

feature: enable local development with Miniflare 3 and `workerd` by default

`wrangler dev` now runs fully-locally by default, using the open-source Cloudflare Workers runtime [`workerd`](https://github.com/cloudflare/workerd).
To restore the previous behaviour of running on a remote machine with access to production data, use the new `--remote` flag.
The `--local` and `--experimental-local` flags have been deprecated, as this behaviour is now the default, and will be removed in the next major version.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,5 @@ packages/quick-edit/web
# VSCode Theme
*.vsix
vendor/vscode

.wrangler/
1 change: 0 additions & 1 deletion fixtures/d1-worker-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"author": "",
"main": "src/index.js",
"scripts": {
"check:type": "tsc",
"db:query": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --command='SELECT * FROM Customers'",
"db:query-json": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --command='SELECT * FROM Customers' --json",
"db:reset": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --file=./schema.sql",
Expand Down
12 changes: 0 additions & 12 deletions fixtures/d1-worker-app/tsconfig.json

This file was deleted.

3 changes: 2 additions & 1 deletion fixtures/external-durable-objects-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
"types": ["node"],
"esModuleInterop": true
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
13 changes: 2 additions & 11 deletions fixtures/no-bundle-import/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import path from "path";
import { describe, expect, test, beforeAll, afterAll } from "vitest";
import { unstable_dev } from "../../../packages/wrangler/wrangler-dist/cli.js";
import type { UnstableDevWorker } from "../../../packages/wrangler/wrangler-dist/cli.js";
import { unstable_dev } from "wrangler";
import type { UnstableDevWorker } from "wrangler";

describe("Worker", () => {
let worker: UnstableDevWorker;

// TODO: Remove this when `workerd` has Windows support
if (process.env.RUNNER_OS === "Windows") {
test("dummy windows test", () => {
expect(process.env.RUNNER_OS).toStrictEqual("Windows");
});
return;
}

beforeAll(async () => {
worker = await unstable_dev(path.resolve(__dirname, "index.js"), {
bundle: false,
experimental: { experimentalLocal: true },
});
}, 30_000);

Expand Down
1 change: 0 additions & 1 deletion fixtures/node-app-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
"@types/node": "^17.0.33",
"undici": "^5.9.1"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion fixtures/pages-functions-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
"types": ["node"],
"esModuleInterop": true
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
3 changes: 2 additions & 1 deletion fixtures/pages-functions-with-routes-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
"types": ["node"],
"esModuleInterop": true
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
"types": ["node"],
"esModuleInterop": true
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
3 changes: 2 additions & 1 deletion fixtures/pages-simple-assets/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
"types": ["node"],
"esModuleInterop": true
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
1 change: 1 addition & 0 deletions fixtures/pages-ws-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test:ci": "npx vitest"
},
"devDependencies": {
"miniflare": "^3.0.0",
"ws": "^8.8.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions fixtures/pages-ws-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fork, spawnSync } from "child_process";
import * as path from "path";
import { upgradingFetch } from "@miniflare/web-sockets";
import { fetch } from "miniflare";
import { describe, expect, it, beforeAll, afterAll } from "vitest";
import type { ChildProcess } from "child_process";

Expand Down Expand Up @@ -50,15 +50,15 @@ describe.concurrent.skip("Pages Functions", () => {

it("understands normal fetches", async () => {
await readyPromise;
const response = await upgradingFetch(`http://${ip}:${port}/`);
const response = await fetch(`http://${ip}:${port}/`);
expect(response.headers.get("x-proxied")).toBe("true");
const text = await response.text();
expect(text).toContain("Hello, world!");
});

it("understands websocket fetches", async () => {
await readyPromise;
const response = await upgradingFetch(`http://${ip}:${port}/ws`, {
const response = await fetch(`http://${ip}:${port}/ws`, {
headers: { Upgrade: "websocket" },
});
expect(response.status).toBe(101);
Expand Down
4 changes: 2 additions & 2 deletions fixtures/worker-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default {
request.cf
);

await fetch(new URL("https://example.com"));
await fetch(new URL("http://example.com"));
await fetch(
new Request("https://example.com", { method: "POST", body: "foo" })
new Request("http://example.com", { method: "POST", body: "foo" })
);

return new Response(`${request.url} ${now()}`);
Expand Down
22 changes: 1 addition & 21 deletions node-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
// https://github.com/cloudflare/workers-sdk/pull/2496#discussion_r1062516883

import {
Event as WorkerEvent,
WebAssembly as WorkerWebAssembly,
} from "@cloudflare/workers-types";
import { WebAssembly as WorkerWebAssembly } from "@cloudflare/workers-types";
import type {
EventListenerOrEventListenerObject,
EventTargetAddEventListenerOptions,
EventTargetEventListenerOptions,
} from "@cloudflare/workers-types";

declare global {
// `Event` and `EventTarget` have been global since Node 15, but aren't
// included in `@types/node`.
class Event extends WorkerEvent {}
type EventListenerOptions = EventTargetEventListenerOptions;
type AddEventListenerOptions = EventTargetAddEventListenerOptions;
// (can't use EventTarget from "@cloudflare/workers-types" as it's event map
// type parameters are incompatible with `tinybench`, a `vitest` dependency)
class EventTarget {
addEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: EventTargetAddEventListenerOptions | boolean
): void;
dispatchEvent(event: Event): boolean;
removeEventListener(
type: string,
callback: EventListenerOrEventListenerObject | null,
options?: EventTargetEventListenerOptions | boolean
): void;
}

// `WebAssembly` has been global since Node 8, but isn't included in
// `@types/node`.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.22.0",
"@types/jest": "^28.1.6",
"@types/node": "^16.11.11",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"cross-env": "^7.0.3",
Expand All @@ -45,7 +45,7 @@
"rimraf": "^3.0.2",
"typescript": "^4.8.4",
"vite": "^4.0.4",
"vitest": "^0.26.3"
"vitest": "^0.31.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/edge-preview-authenticated-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@cloudflare/workers-types": "^4.20230321.0",
"cookie": "^0.5.0",
"toucan-js": "^3.1.0",
"vitest": "^0.29.8",
"vitest": "^0.31.0",
"wrangler": "*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe("Preview Worker", () => {
}
);
expect(Object.fromEntries([...resp.headers.entries()])).toMatchObject({
"content-length": "0",
location: "/hello?world",
"set-cookie":
"token=%7B%22token%22%3A%22TEST_TOKEN%22%2C%22remote%22%3A%22http%3A%2F%2F127.0.0.1%3A6756%22%7D; Domain=preview.devprod.cloudflare.dev; HttpOnly; Secure; SameSite=None",
Expand All @@ -119,7 +120,7 @@ describe("Preview Worker", () => {
}
);

const json = await resp.json();
const json = (await resp.json()) as { headers: string[][]; url: string };
expect(Object.fromEntries([...json.headers])).toMatchObject({
"cf-workers-preview-token": "TEST_TOKEN",
});
Expand Down
8 changes: 4 additions & 4 deletions packages/pages-shared/asset-server/responses.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type HeadersInit = ConstructorParameters<typeof Headers>[0];

function mergeHeaders(base: HeadersInit, extra: HeadersInit) {
base = new Headers(base ?? {});
extra = new Headers(extra ?? {});
const baseHeaders = new Headers(base ?? {});
const extraHeaders = new Headers(extra ?? {});

return new Headers({
...Object.fromEntries(base.entries()),
...Object.fromEntries(extra.entries()),
...Object.fromEntries(baseHeaders.entries()),
...Object.fromEntries(extraHeaders.entries()),
});
}

Expand Down
12 changes: 0 additions & 12 deletions packages/pages-shared/environment-polyfills/miniflare-tre.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/pages-shared/environment-polyfills/miniflare.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { polyfill } from ".";

export default async () => {
const mf = await import("@miniflare/core");

const mf = await import("miniflare");
polyfill({
fetch: mf.fetch,
Headers: mf.Headers,
Expand Down
46 changes: 18 additions & 28 deletions packages/pages-shared/environment-polyfills/types.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import {
Headers as MiniflareHeaders,
Request as MiniflareRequest,
Response as MiniflareResponse,
} from "@miniflare/core";
import { HTMLRewriter as MiniflareHTMLRewriter } from "@miniflare/html-rewriter";
import type { CacheInterface as MiniflareCacheInterface } from "@miniflare/cache";
import type { fetch as miniflareFetch } from "@miniflare/core";
import type { ReadableStream as SimilarReadableStream } from "stream/web";
Headers as WorkerHeaders,
Request as WorkerRequest,
Response as WorkerResponse,
HTMLRewriter as WorkerHTMLRewriter,
} from "@cloudflare/workers-types/experimental";
import type {
fetch as workerFetch,
ReadableStream as WorkerReadableStream,
CacheStorage as WorkerCacheStorage,
} from "@cloudflare/workers-types/experimental";

declare global {
const fetch: typeof miniflareFetch;
class Headers extends MiniflareHeaders {}
class Request extends MiniflareRequest {}
class Response extends MiniflareResponse {}
const fetch: typeof workerFetch;
class Headers extends WorkerHeaders {}
class Request extends WorkerRequest {}
class Response extends WorkerResponse {}

type CacheInterface = Omit<MiniflareCacheInterface, "match"> & {
match(
...args: Parameters<MiniflareCacheInterface["match"]>
): Promise<Response | undefined>;
};

class CacheStorage {
get default(): CacheInterface;
open(cacheName: string): Promise<CacheInterface>;
}

class HTMLRewriter extends MiniflareHTMLRewriter {
transform(response: Response): Response;
}

type ReadableStream = SimilarReadableStream;
// Not polyfilled
type ReadableStream = WorkerReadableStream;
type CacheStorage = WorkerCacheStorage;
class HTMLRewriter extends WorkerHTMLRewriter {}
}

export type PolyfilledRuntimeEnvironment = {
Expand Down
5 changes: 2 additions & 3 deletions packages/pages-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@
]
},
"dependencies": {
"@miniflare/core": "2.14.0"
"miniflare": "^3.0.0"
},
"devDependencies": {
"@miniflare/cache": "2.14.0",
"@miniflare/html-rewriter": "2.14.0",
"@cloudflare/workers-types": "^4.20230511.0",
"@types/service-worker-mock": "^2.0.1",
"concurrently": "^7.3.0",
"glob": "^8.0.3",
Expand Down
10 changes: 3 additions & 7 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,10 @@
"@cloudflare/kv-asset-handler": "^0.2.0",
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
"@miniflare/core": "2.14.0",
"@miniflare/d1": "2.14.0",
"@miniflare/durable-objects": "2.14.0",
"blake3-wasm": "^2.1.5",
"chokidar": "^3.5.3",
"esbuild": "0.16.3",
"miniflare": "2.14.0",
"miniflare": "^3.0.0",
"nanoid": "^3.3.3",
"path-to-regexp": "^6.2.0",
"selfsigned": "^2.0.1",
Expand All @@ -113,10 +110,9 @@
},
"devDependencies": {
"@cloudflare/types": "^6.18.4",
"@cloudflare/workers-types": "^4.20221111.1",
"@cloudflare/workers-types": "^4.20230511.0",
"@iarna/toml": "^3.0.0",
"@microsoft/api-extractor": "^7.28.3",
"@miniflare/tre": "3.0.0-next.13",
"@types/better-sqlite3": "^7.6.0",
"@types/busboy": "^1.5.0",
"@types/command-exists": "^1.2.0",
Expand Down Expand Up @@ -181,7 +177,7 @@
"ts-dedent": "^2.2.0",
"undici": "5.20.0",
"update-check": "^1.5.4",
"vitest": "^0.29.2",
"vitest": "^0.31.0",
"ws": "^8.5.0",
"xdg-app-paths": "^7.3.0",
"yargs": "^17.4.1",
Expand Down
Loading

0 comments on commit bac9b5d

Please sign in to comment.