Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/wet-flowers-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be considered a minor... please let me know

---

Update the structure of the `configure` method of autoconfig frameworks

Update the signature of the `configure` function of autoconfig frameworks (`AutoconfigDetails#Framework`), before they would return a `RawConfig` object to use to update the project's wrangler config file, now they return an object that includes the `RawConfig` and that can potentially also hold additional data relevant to the configuration.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("autoconfig details - confirmAutoConfigDetails()", () => {
buildCommand: "astro build",
framework: {
configured: false,
configure: () => ({}),
configure: () => ({ wranglerConfig: {} }),
name: "astro",
},
outputDir: "<OUTPUT_DIR>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ describe("autoconfig details - displayAutoConfigDetails()", () => {
configured: false,
projectPath: process.cwd(),
workerName: "my-astro-app",
framework: { name: "Astro", configured: false, configure: () => ({}) },
framework: {
name: "Astro",
configured: false,
configure: () => ({ wranglerConfig: {} }),
},
buildCommand: "astro build",
outputDir: "dist",
});
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/__tests__/autoconfig/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ describe("autoconfig (deploy)", () => {
});
await writeFile(".gitignore", "");
const configureSpy = vi.fn(async ({ outputDir }) => ({
assets: { directory: outputDir },
wranglerConfig: {
assets: { directory: outputDir },
},
}));
await run.runAutoConfig({
projectPath: process.cwd(),
Expand Down
17 changes: 9 additions & 8 deletions packages/wrangler/src/autoconfig/frameworks/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { brandColor, dim } from "@cloudflare/cli/colors";
import { getPackageManager } from "../../package-manager";
import { runCommand } from "../c3-vendor/command";
import { Framework } from ".";
import type { ConfigurationOptions } from ".";
import type { RawConfig } from "@cloudflare/workers-utils";
import type { ConfigurationOptions, ConfigurationResults } from ".";

export class Astro extends Framework {
async configure({
outputDir,
dryRun,
}: ConfigurationOptions): Promise<RawConfig> {
}: ConfigurationOptions): Promise<ConfigurationResults> {
const { npx } = await getPackageManager();
if (!dryRun) {
await runCommand([npx, "astro", "add", "cloudflare", "-y"], {
Expand All @@ -23,11 +22,13 @@ export class Astro extends Framework {
writeFileSync("public/.assetsignore", "_worker.js\n_routes.json");
}
return {
main: `${outputDir}/_worker.js/index.js`,
compatibility_flags: ["nodejs_compat", "global_fetch_strictly_public"],
assets: {
binding: "ASSETS",
directory: outputDir,
wranglerConfig: {
main: `${outputDir}/_worker.js/index.js`,
compatibility_flags: ["nodejs_compat", "global_fetch_strictly_public"],
assets: {
binding: "ASSETS",
directory: outputDir,
},
},
};
}
Expand Down
7 changes: 6 additions & 1 deletion packages/wrangler/src/autoconfig/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export type ConfigurationOptions = {
workerName: string;
dryRun: boolean;
};

export type ConfigurationResults = {
wranglerConfig: RawConfig;
};

export abstract class Framework {
constructor(public name: string = "Static") {}

Expand All @@ -21,7 +26,7 @@ export abstract class Framework {

abstract configure(
options: ConfigurationOptions
): Promise<RawConfig> | RawConfig;
): Promise<ConfigurationResults> | ConfigurationResults;

configurationDescription?: string;
}
13 changes: 6 additions & 7 deletions packages/wrangler/src/autoconfig/frameworks/static.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Framework } from ".";
import type { ConfigurationOptions } from ".";
import type { RawConfig } from "@cloudflare/workers-utils";
import type { ConfigurationOptions, ConfigurationResults } from ".";

export class Static extends Framework {
configure({
outputDir,
}: ConfigurationOptions): Promise<RawConfig> | RawConfig {
configure({ outputDir }: ConfigurationOptions): ConfigurationResults {
return {
assets: {
directory: outputDir,
wranglerConfig: {
assets: {
directory: outputDir,
},
},
};
}
Expand Down
19 changes: 11 additions & 8 deletions packages/wrangler/src/autoconfig/frameworks/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { getPackageManager } from "../../package-manager";
import { runCommand } from "../c3-vendor/command";
import { installPackages } from "../c3-vendor/packages";
import { Framework } from ".";
import type { ConfigurationOptions } from ".";
import type { RawConfig } from "@cloudflare/workers-utils";
import type { ConfigurationOptions, ConfigurationResults } from ".";

export class SvelteKit extends Framework {
async configure({ dryRun }: ConfigurationOptions): Promise<RawConfig> {
async configure({
dryRun,
}: ConfigurationOptions): Promise<ConfigurationResults> {
const { dlx } = await getPackageManager();
if (!dryRun) {
await runCommand(
Expand Down Expand Up @@ -36,11 +37,13 @@ export class SvelteKit extends Framework {
});
}
return {
main: ".svelte-kit/cloudflare/_worker.js",
compatibility_flags: ["nodejs_als"],
assets: {
binding: "ASSETS",
directory: ".svelte-kit/cloudflare",
wranglerConfig: {
main: ".svelte-kit/cloudflare/_worker.js",
compatibility_flags: ["nodejs_als"],
assets: {
binding: "ASSETS",
directory: ".svelte-kit/cloudflare",
},
},
};
}
Expand Down
11 changes: 6 additions & 5 deletions packages/wrangler/src/autoconfig/frameworks/tanstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import * as recast from "recast";
import { transformFile } from "../c3-vendor/codemod";
import { installPackages } from "../c3-vendor/packages";
import { Framework } from ".";
import type { ConfigurationOptions } from ".";
import type { RawConfig } from "@cloudflare/workers-utils";
import type { ConfigurationOptions, ConfigurationResults } from ".";
import type { types } from "recast";

const b = recast.types.builders;
Expand Down Expand Up @@ -128,7 +127,7 @@ export class TanstackStart extends Framework {
async configure({
dryRun,
projectPath,
}: ConfigurationOptions): Promise<RawConfig> {
}: ConfigurationOptions): Promise<ConfigurationResults> {
if (!dryRun) {
await installPackages(["@cloudflare/vite-plugin"], {
dev: true,
Expand All @@ -140,8 +139,10 @@ export class TanstackStart extends Framework {
}

return {
compatibility_flags: ["nodejs_compat"],
main: "@tanstack/react-start/server-entry",
wranglerConfig: {
compatibility_flags: ["nodejs_compat"],
main: "@tanstack/react-start/server-entry",
},
};
}

Expand Down
38 changes: 22 additions & 16 deletions packages/wrangler/src/autoconfig/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ export async function runAutoConfig(
},
} satisfies RawConfig;

const modifications = await buildOperationsSummary(autoConfigDetails, {
...wranglerConfig,
...(await autoConfigDetails.framework?.configure({
const dryRunConfigurationResults =
await autoConfigDetails.framework?.configure({
outputDir: autoConfigDetails.outputDir,
projectPath: autoConfigDetails.projectPath,
workerName: autoConfigDetails.workerName,
dryRun: true,
})),
});

const modifications = await buildOperationsSummary(autoConfigDetails, {
...wranglerConfig,
...dryRunConfigurationResults?.wranglerConfig,
});

if (!(skipConfirmations || (await confirm("Proceed with setup?")))) {
Expand All @@ -99,10 +102,6 @@ export async function runAutoConfig(
`Running autoconfig with:\n${JSON.stringify(autoConfigDetails, null, 2)}...`
);

if (modifications.wranglerInstall) {
await installWrangler();
}

if (autoConfigDetails.packageJson) {
await writeFile(
resolve(autoConfigDetails.projectPath, "package.json"),
Expand All @@ -119,17 +118,24 @@ export async function runAutoConfig(
)
);
}
const additionalConfigDetails =
(await autoConfigDetails.framework?.configure({
outputDir: autoConfigDetails.outputDir,
projectPath: autoConfigDetails.projectPath,
workerName: autoConfigDetails.workerName,
dryRun: false,
})) ?? {};

if (modifications.wranglerInstall) {
await installWrangler();
}
const configurationResults = await autoConfigDetails.framework?.configure({
outputDir: autoConfigDetails.outputDir,
projectPath: autoConfigDetails.projectPath,
workerName: autoConfigDetails.workerName,
dryRun: false,
});

await writeFile(
resolve(autoConfigDetails.projectPath, "wrangler.jsonc"),
JSON.stringify({ ...wranglerConfig, ...additionalConfigDetails }, null, 2)
JSON.stringify(
{ ...wranglerConfig, ...configurationResults?.wranglerConfig },
null,
2
)
);

addWranglerToGitIgnore(autoConfigDetails.projectPath);
Expand Down
Loading