Skip to content

Commit

Permalink
Config controller (#6001)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Rahul Sethi <5822355+RamIdeas@users.noreply.github.com>
  • Loading branch information
penalosa and RamIdeas committed Jun 20, 2024
1 parent d4e1e9f commit d39d595
Show file tree
Hide file tree
Showing 17 changed files with 744 additions and 356 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-terms-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: changes to how `wrangler dev` launches your worker, behind the experimental `--x-dev-env` flag
9 changes: 5 additions & 4 deletions fixtures/dev-env/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ async function fakeStartUserWorker(options: {
script: string;
name?: string;
mfOpts?: Partial<MiniflareOptions>;
config?: OptionalKeys<StartDevWorkerOptions, "name" | "script">;
config?: OptionalKeys<StartDevWorkerOptions, "name" | "entrypoint">;
}) {
const config: StartDevWorkerOptions = {
...options.config,
name: options.name ?? "test-worker",
script: { contents: options.script },
// @ts-expect-error Fix when support script.contents
entrypoint: { contents: options.script },
};
const mfOpts: MiniflareOptions = Object.assign(
{
Expand Down Expand Up @@ -109,8 +110,8 @@ async function fakeUserWorkerChanges({

config = {
...config,
script: {
...config.script,
entrypoint: {
...config.entrypoint,
...(script ? { contents: script } : undefined),
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"@sentry/utils": "^7.86.0",
"@types/body-parser": "^1.19.2",
"@types/command-exists": "^1.2.0",
"@types/deepmerge": "^2.2.0",
"@types/express": "^4.17.13",
"@types/glob-to-regexp": "^0.4.1",
"@types/is-ci": "^3.0.0",
Expand All @@ -124,6 +125,7 @@
"clipboardy": "^3.0.0",
"cmd-shim": "^4.1.0",
"command-exists": "^1.2.9",
"deepmerge": "^4.3.1",
"concurrently": "^8.2.2",
"devtools-protocol": "^0.0.1182435",
"dotenv": "^16.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import dedent from "ts-dedent";
import { test as base, describe } from "vitest";
import { BundlerController } from "../../../api/startDevWorker/BundlerController";
import { runInTempDir } from "../../helpers/run-in-tmp";
import { unusable } from "../../helpers/unusable";
import type {
BundleCompleteEvent,
BundleStartEvent,
Expand Down Expand Up @@ -68,20 +67,15 @@ describe("happy path bundle + watch", () => {
});
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("src/index.ts"),
directory: path.resolve("src"),
format: "modules",
moduleRoot: path.resolve("src"),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("src/index.ts") },
directory: path.resolve("src"),
build: {
bundle: true,
moduleRules: [],
custom: {},
define: {},
format: "modules",
moduleRoot: path.resolve("src"),
},
};

Expand Down Expand Up @@ -140,20 +134,15 @@ describe("happy path bundle + watch", () => {
});
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("src/index.ts"),
directory: path.resolve("src"),
format: "modules",
moduleRoot: path.resolve("src"),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("src/index.ts") },
directory: path.resolve("src"),
build: {
bundle: true,
moduleRules: [],
custom: {},
define: {},
format: "modules",
moduleRoot: path.resolve("src"),
},
};

Expand Down Expand Up @@ -206,15 +195,8 @@ describe("happy path bundle + watch", () => {
});
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("out.ts"),
directory: path.resolve("."),
format: "modules",
moduleRoot: path.resolve("."),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("out.ts") },
directory: path.resolve("."),
build: {
bundle: true,
moduleRules: [],
Expand All @@ -223,6 +205,8 @@ describe("happy path bundle + watch", () => {
watch: "random_dir",
},
define: {},
format: "modules",
moduleRoot: path.resolve("."),
},
};

Expand Down Expand Up @@ -281,20 +265,16 @@ describe("switching", () => {
});
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("src/index.ts"),
directory: path.resolve("src"),
format: "modules",
moduleRoot: path.resolve("src"),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("src/index.ts") },
directory: path.resolve("src"),

build: {
bundle: true,
moduleRules: [],
custom: {},
define: {},
format: "modules",
moduleRoot: path.resolve("src"),
},
};

Expand Down Expand Up @@ -327,15 +307,8 @@ describe("switching", () => {
});
const configCustom = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("out.ts"),
directory: process.cwd(),
format: "modules",
moduleRoot: process.cwd(),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("out.ts") },
directory: process.cwd(),
build: {
bundle: true,
moduleRules: [],
Expand All @@ -344,6 +317,8 @@ describe("switching", () => {
watch: "random_dir",
},
define: {},
format: "modules",
moduleRoot: process.cwd(),
},
} satisfies StartDevWorkerOptions;

Expand Down Expand Up @@ -400,15 +375,9 @@ describe("switching", () => {
});
const configCustom = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("out.ts"),
directory: process.cwd(),
format: "modules",
moduleRoot: process.cwd(),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("out.ts") },
directory: process.cwd(),

build: {
bundle: true,
moduleRules: [],
Expand All @@ -417,6 +386,8 @@ describe("switching", () => {
watch: "random_dir",
},
define: {},
format: "modules",
moduleRoot: process.cwd(),
},
} satisfies StartDevWorkerOptions;

Expand Down Expand Up @@ -448,20 +419,16 @@ describe("switching", () => {
});
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
_entry: {
file: path.resolve("src/index.ts"),
directory: path.resolve("src"),
format: "modules",
moduleRoot: path.resolve("src"),
name: "worker-name",
},
_additionalModules: [],
entrypoint: { path: path.resolve("src/index.ts") },
directory: path.resolve("src"),

build: {
bundle: true,
moduleRules: [],
custom: {},
define: {},
format: "modules",
moduleRoot: path.resolve("src"),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
compatibilityFlags: ["nodejs_compat"],
compatibilityDate: "2023-10-01",
};
Expand Down Expand Up @@ -270,7 +270,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
};
const bundle: Bundle = {
type: "commonjs",
Expand Down Expand Up @@ -357,7 +357,7 @@ describe("Core", () => {
function update(version: number) {
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: {
VERSION: { type: "json", value: version },
},
Expand Down Expand Up @@ -405,7 +405,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
compatibilityDate: disabledDate,
};
const bundle = makeEsbuildBundle(dedent/*javascript*/ `
Expand Down Expand Up @@ -443,7 +443,7 @@ describe("Core", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
};
const bundle = makeEsbuildBundle(dedent/*javascript*/ `
export default {
Expand Down Expand Up @@ -502,7 +502,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: {
TEXT: { type: "plain_text", value: "text" },
OBJECT: { type: "json", value: { a: { b: 1 } } },
Expand Down Expand Up @@ -542,7 +542,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: {
// `wasm-module` bindings aren't allowed in modules workers
WASM: { type: "wasm_module", source: { contents: WASM_ADD_MODULE } },
Expand Down Expand Up @@ -572,7 +572,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
dev: { persist: { path: persist } },
};
const bundle = makeEsbuildBundle(`export default {
Expand Down Expand Up @@ -620,7 +620,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: { NAMESPACE: { type: "kv_namespace", id: "ns" } },
dev: { persist: { path: persist } },
};
Expand Down Expand Up @@ -664,10 +664,10 @@ describe("Bindings", () => {
fs.writeFileSync(path.join(tmp, "charts.xlsx"), "📊");
fs.writeFileSync(path.join(tmp, "secrets.txt"), "🔐");

const config: StartDevWorkerOptions = {
let config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
site: { path: tmp, include: ["*.txt"] },
entrypoint: unusable(),
legacy: { site: { bucket: tmp, include: ["*.txt"] } },
};
const bundle = makeEsbuildBundle(`
import manifestJSON from "__STATIC_CONTENT_MANIFEST";
Expand Down Expand Up @@ -695,8 +695,13 @@ describe("Bindings", () => {
expect(res.status).toBe(404);
res = await fetch(new URL("/secrets.txt", url));
expect(res.status).toBe(200);

config.site = { path: tmp, exclude: ["secrets.txt"] };
config = {
...config,
legacy: {
...config.legacy,
site: { bucket: tmp, exclude: ["secrets.txt"] },
},
};
controller.onBundleStart({ type: "bundleStart", config });
controller.onBundleComplete({ type: "bundleComplete", config, bundle });
event = await waitForReloadComplete(controller);
Expand All @@ -716,7 +721,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: { BUCKET: { type: "r2_bucket", bucket_name: "bucket" } },
dev: { persist: { path: persist } },
};
Expand Down Expand Up @@ -760,7 +765,7 @@ describe("Bindings", () => {

const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: {
DB: { type: "d1", database_name: "db-name", database_id: "db" },
},
Expand Down Expand Up @@ -810,7 +815,7 @@ describe("Bindings", () => {
const reportPromise = new DeferredPromise<unknown>();
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: {
QUEUE: { type: "queue", queue_name: "queue" },
BATCH_REPORT: {
Expand Down Expand Up @@ -866,7 +871,7 @@ describe("Bindings", () => {
const localConnectionString = `postgres://username:password@127.0.0.1:${port}/db`;
const config: StartDevWorkerOptions = {
name: "worker",
script: unusable(),
entrypoint: unusable(),
bindings: { DB: { type: "hyperdrive", id: "db", localConnectionString } },
};
const bundle = makeEsbuildBundle(`export default {
Expand Down
Loading

0 comments on commit d39d595

Please sign in to comment.