Skip to content

Commit f356713

Browse files
In autoconfig consider vite projects using the vite plugin as already configured
1 parent d9f6a6d commit f356713

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

.changeset/old-boxes-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
In autoconfig consider vite projects using the vite plugin as already configured

packages/wrangler/src/__tests__/autoconfig/details/confirm-auto-config-details.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("autoconfig details - confirmAutoConfigDetails()", () => {
102102
workerName: "my-astro-site",
103103
buildCommand: "astro build",
104104
framework: {
105-
configured: false,
105+
isConfigured: () => false,
106106
configure: () => ({ wranglerConfig: {} }),
107107
name: "astro",
108108
},

packages/wrangler/src/__tests__/autoconfig/details/display-auto-config-details.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("autoconfig details - displayAutoConfigDetails()", () => {
3737
workerName: "my-astro-app",
3838
framework: {
3939
name: "Astro",
40-
configured: false,
40+
isConfigured: () => false,
4141
configure: () => ({ wranglerConfig: {} }),
4242
},
4343
buildCommand: "astro build",

packages/wrangler/src/autoconfig/details.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function getDetailsForAutoConfig({
153153

154154
return {
155155
projectPath: projectPath,
156-
configured: framework?.configured ?? false,
156+
configured: (await framework?.isConfigured(projectPath)) ?? false,
157157
framework,
158158
packageJson,
159159
buildCommand: detectedFramework?.buildCommand ?? packageJsonBuild,

packages/wrangler/src/autoconfig/frameworks/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export abstract class Framework {
1919
deploy?: string; // default is `npm run build && wrangler deploy`
2020
typegen?: string; // default is `wrangler types`
2121

22-
/** Some frameworks (i.e. Nuxt) don't need additional configuration */
23-
get configured() {
22+
isConfigured(_projectPath: string): boolean | Promise<boolean> {
2423
return false;
2524
}
2625

packages/wrangler/src/autoconfig/frameworks/utils/vite-config.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,62 @@ import type { types } from "recast";
88
const b = recast.types.builders;
99
const t = recast.types.namedTypes;
1010

11-
export function transformViteConfig(
12-
projectPath: string,
13-
options: { viteEnvironmentName?: string } = {}
14-
) {
11+
export function checkIfViteConfigUsesCloudflarePlugin(
12+
projectPath: string
13+
): boolean {
14+
const filePath = getViteConfigPath(projectPath);
15+
16+
let importsCloudflarePlugin = false;
17+
let usesCloudflarePlugin = false;
18+
19+
transformFile(filePath, {
20+
visitProgram(n) {
21+
// Only import if not already imported
22+
if (
23+
n.node.body.some(
24+
(s) =>
25+
s.type === "ImportDeclaration" &&
26+
s.source.value === "@cloudflare/vite-plugin"
27+
)
28+
) {
29+
importsCloudflarePlugin = true;
30+
return this.traverse(n);
31+
}
32+
33+
this.traverse(n);
34+
},
35+
visitCallExpression: function (n) {
36+
const callee = n.node.callee as types.namedTypes.Identifier;
37+
if (callee.name !== "defineConfig") {
38+
return this.traverse(n);
39+
}
40+
41+
const config = n.node.arguments[0];
42+
assert(t.ObjectExpression.check(config));
43+
const pluginsProp = config.properties.find((prop) => isPluginsProp(prop));
44+
assert(pluginsProp && t.ArrayExpression.check(pluginsProp.value));
45+
46+
// Only add the Cloudflare plugin if it's not already present
47+
if (
48+
pluginsProp.value.elements.some(
49+
(el) =>
50+
el?.type === "CallExpression" &&
51+
el.callee.type === "Identifier" &&
52+
el.callee.name === "cloudflare"
53+
)
54+
) {
55+
usesCloudflarePlugin = true;
56+
return this.traverse(n);
57+
}
58+
59+
this.traverse(n);
60+
},
61+
});
62+
63+
return importsCloudflarePlugin && usesCloudflarePlugin;
64+
}
65+
66+
function getViteConfigPath(projectPath: string): string {
1567
const filePathTS = path.join(projectPath, `vite.config.ts`);
1668
const filePathJS = path.join(projectPath, `vite.config.ts`);
1769

@@ -25,6 +77,15 @@ export function transformViteConfig(
2577
throw new Error("Could not find Vite config file to modify");
2678
}
2779

80+
return filePath;
81+
}
82+
83+
export function transformViteConfig(
84+
projectPath: string,
85+
options: { viteEnvironmentName?: string } = {}
86+
) {
87+
const filePath = getViteConfigPath(projectPath);
88+
2889
transformFile(filePath, {
2990
visitProgram(n) {
3091
// Add an import of the @cloudflare/vite-plugin

packages/wrangler/src/autoconfig/frameworks/vite.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { brandColor, dim } from "@cloudflare/cli/colors";
22
import { installPackages } from "../c3-vendor/packages";
3-
import { transformViteConfig } from "./utils";
3+
import {
4+
checkIfViteConfigUsesCloudflarePlugin,
5+
transformViteConfig,
6+
} from "./utils";
47
import { Framework } from ".";
58
import type { ConfigurationOptions, ConfigurationResults } from ".";
69

710
export class Vite extends Framework {
11+
isConfigured(projectPath: string): boolean {
12+
return checkIfViteConfigUsesCloudflarePlugin(projectPath);
13+
}
14+
815
async configure({
916
dryRun,
1017
outputDir,

0 commit comments

Comments
 (0)