Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Wrangler 2 routes #307

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/content/core/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ filename: api/wrangler.toml
route = "https://example.com/api/*"
routes = ["example.com/v1/*", "example.com/v2/*"]

# Miniflare supports Wrangler2 routes. Zones are ignored
route = {pattern = "https://example.com/api/*", zone_name="<ignored>"}
routes = [{pattern = "example.com/v1/*", zone_name="<ignored>"}, {pattern = "example.com/v2/*", zone_id = "<ignored>"}]

# Only loaded if the wrangler.toml environment is set to "dev"
[env.dev]
route = "miniflare.test/api/*"
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/plugins/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
Plugin,
PluginContext,
ProcessedModuleRule,
RouteType,
STRING_SCRIPT_PATH,
SetupResult,
globsToMatcher,
Expand Down Expand Up @@ -328,12 +329,14 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
type: OptionType.ARRAY,
description: "Route to respond with this worker on",
fromWrangler: ({ route, routes, miniflare }) => {
const result: string[] = [];
const result: RouteType[] = [];
const toPattern = (route: RouteType): string =>
typeof route === "string" ? route : route.pattern;
if (route) result.push(route);
if (routes) result.push(...routes);
if (miniflare?.route) result.push(miniflare.route);
if (miniflare?.routes) result.push(...miniflare.routes);
return result.length ? result : undefined;
return result.length ? result.map(toPattern) : undefined;
},
})
routes?: string[];
Expand Down
16 changes: 14 additions & 2 deletions packages/core/test/plugins/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,23 @@ test("CorePlugin: parses options from wrangler config", async (t) => {
},
},
route: "miniflare.dev/*",
routes: ["dev.miniflare.dev/*"],
routes: [
"dev.miniflare.dev/*",
{ pattern: "dev_with_zone_id.miniflare.dev/*", zone_id: "" },
{ pattern: "dev_with_zone_name.miniflare.dev/*", zone_name: "" },
],
usage_model: "unbound",
miniflare: {
upstream: "https://miniflare.dev",
watch: true,
update_check: false,
mounts: { api: "./api", site: "./site@dev" },
route: "http://localhost:8787/*",
routes: ["miniflare.mf:8787/*"],
routes: [
"miniflare.mf:8787/*",
{ pattern: "dev_with_zone_id.miniflare.mf:8787/*", zone_id: "" },
{ pattern: "dev_with_zone_name.miniflare.mf:8787/*", zone_name: "" },
],
global_async_io: true,
global_timers: true,
global_random: true,
Expand Down Expand Up @@ -220,8 +228,12 @@ test("CorePlugin: parses options from wrangler config", async (t) => {
routes: [
"miniflare.dev/*",
"dev.miniflare.dev/*",
"dev_with_zone_id.miniflare.dev/*",
"dev_with_zone_name.miniflare.dev/*",
"http://localhost:8787/*",
"miniflare.mf:8787/*",
"dev_with_zone_id.miniflare.mf:8787/*",
"dev_with_zone_name.miniflare.mf:8787/*",
],
logUnhandledRejections: undefined,
globalAsyncIO: true,
Expand Down
12 changes: 8 additions & 4 deletions packages/shared/src/wrangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ export interface WranglerServiceConfig {
environment: string;
}

export type RouteType =
| string
| { pattern: string; zone_id?: string; zone_name?: string };
mrbbot marked this conversation as resolved.
Show resolved Hide resolved

export interface WranglerEnvironmentConfig {
name?: string; // inherited
zone_id?: string; // inherited
account_id?: string; // inherited
workers_dev?: boolean; // inherited
route?: string; // NOT inherited
routes?: string[]; // NOT inherited
route?: RouteType; // NOT inherited
routes?: RouteType[]; // NOT inherited
webpack_config?: string; // inherited
vars?: Record<string, any>; // NOT inherited
kv_namespaces?: {
Expand Down Expand Up @@ -84,8 +88,8 @@ export interface WranglerEnvironmentConfig {
live_reload?: boolean;
update_check?: boolean;
mounts?: Record<string, string>;
route?: string;
routes?: string[];
route?: RouteType;
routes?: RouteType[];
global_async_io?: boolean;
global_timers?: boolean;
global_random?: boolean;
Expand Down