Skip to content

Commit

Permalink
reimplement handling enablePagesAssetsServiceBinding as a fetcher typ…
Browse files Browse the repository at this point in the history
…e binding
  • Loading branch information
RamIdeas committed Jul 22, 2024
1 parent 46fcd79 commit 7c244bb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
17 changes: 0 additions & 17 deletions packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,6 @@ async function convertToConfigBundle(
event.bundle = { ...event.bundle, modules: [] };
}

if (event.config.legacy?.enablePagesAssetsServiceBinding !== undefined) {
// `../miniflare-cli/assets` dynamically imports`@cloudflare/pages-shared/environment-polyfills`.
// `@cloudflare/pages-shared/environment-polyfills/types.ts` defines `global`
// augmentations that pollute the `import`-site's typing environment.
//
// We `require` instead of `import`ing here to avoid polluting the main
// `wrangler` TypeScript project with the `global` augmentations. This
// relies on the fact that `require` is untyped.
//
// eslint-disable-next-line @typescript-eslint/no-var-requires
const generateASSETSBinding = require("../../miniflare-cli/assets").default;
fetchers.ASSETS = await generateASSETSBinding({
log: logger,
...event.config.legacy?.enablePagesAssetsServiceBinding,
});
}

return {
name: event.config.name,
bundle: event.bundle,
Expand Down
2 changes: 0 additions & 2 deletions packages/wrangler/src/api/startDevWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
import type { WorkerRegistry } from "../../dev-registry";
import type { CfAccount } from "../../dev/create-worker-preview";
import type { EsbuildBundle } from "../../dev/use-esbuild";
import type { EnablePagesAssetsServiceBindingOptions } from "../../miniflare-cli/types";
import type { ConfigController } from "./ConfigController";
import type {
DispatchFetch,
Expand Down Expand Up @@ -161,7 +160,6 @@ export interface StartDevWorkerInput {
site?: Hook<Config["site"], [Config]>;
assets?: Hook<Config["assets"], [Config]>;
enableServiceEnvironments?: boolean;
enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
};
unsafe?: Omit<CfUnsafe, "bindings">;
}
Expand Down
29 changes: 29 additions & 0 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,32 @@ async function updateDevEnvRegistry(
});
}

async function getPagesAssetsFetcher(
options: EnablePagesAssetsServiceBindingOptions | undefined
): Promise<StartDevWorkerInput["bindings"] | undefined> {
if (options !== undefined) {
// `../miniflare-cli/assets` dynamically imports`@cloudflare/pages-shared/environment-polyfills`.
// `@cloudflare/pages-shared/environment-polyfills/types.ts` defines `global`
// augmentations that pollute the `import`-site's typing environment.
//
// We `require` instead of `import`ing here to avoid polluting the main
// `wrangler` TypeScript project with the `global` augmentations. This
// relies on the fact that `require` is untyped.
//
// eslint-disable-next-line @typescript-eslint/no-var-requires
const generateASSETSBinding = require("../miniflare-cli/assets").default;
return {
ASSETS: {
type: "fetcher",
fetcher: await generateASSETSBinding({
log: logger,
...options,
}),
},
};
}
}

export async function startDev(args: StartDevOptions) {
let watcher: ReturnType<typeof watch> | undefined;
let rerender: (node: React.ReactNode) => void | undefined;
Expand Down Expand Up @@ -625,6 +651,9 @@ export async function startDev(args: StartDevOptions) {
}),
},
bindings: {
...(await getPagesAssetsFetcher(
args.enablePagesAssetsServiceBinding
)),
...collectPlainTextVars(args.var),
...convertCfWorkerInitBindingstoBindings({
kv_namespaces: args.kv,
Expand Down
2 changes: 0 additions & 2 deletions packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ function DevSession(props: DevSessionProps) {
: undefined,
assets: props.assetsConfig,
enableServiceEnvironments: !props.legacyEnv,
enablePagesAssetsServiceBinding: props.enablePagesAssetsServiceBinding,
},
unsafe: {
capnp: props.bindings.unsafe?.capnp,
Expand Down Expand Up @@ -519,7 +518,6 @@ function DevSession(props: DevSessionProps) {
props.localUpstream,
props.liveReload,
props.testScheduled,
props.enablePagesAssetsServiceBinding,
accountIdDeferred,
]);

Expand Down
3 changes: 0 additions & 3 deletions packages/wrangler/src/dev/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ export async function startDevServer(
moduleRoot: props.entry.moduleRoot,
nodejsCompatMode: null,
},
legacy: {
enablePagesAssetsServiceBinding: props.enablePagesAssetsServiceBinding,
},
};

// temp: fake these events by calling the handler directly
Expand Down

0 comments on commit 7c244bb

Please sign in to comment.