Skip to content

Commit d672e2e

Browse files
Fix SolidStart autoconfig for projects using version 2.0.0-alpha or later (#12733)
1 parent cf96bfa commit d672e2e

File tree

3 files changed

+126
-40
lines changed

3 files changed

+126
-40
lines changed

.changeset/dry-shoes-cheat.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix SolidStart autoconfig for projects using version 2.0.0-alpha or later
6+
7+
SolidStart v2.0.0-alpha introduced a breaking change where configuration moved from `app.config.(js|ts)` to `vite.config.(js|ts)`. Wrangler's autoconfig now detects the installed SolidStart version and based on it updates the appropriate configuration file

packages/create-cloudflare/e2e/tests/frameworks/test-config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,6 @@ function getExperimentalFrameworkTestConfig(
813813
},
814814
{
815815
name: "solid",
816-
// quarantined: SolidStart moved from app.config to vite.config with Nitro plugin
817-
quarantine: true,
818816
promptHandlers: [
819817
{
820818
matcher: /Which template would you like to use/,
Lines changed: 119 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import assert from "node:assert";
12
import { updateStatus } from "@cloudflare/cli";
23
import { blue } from "@cloudflare/cli/colors";
34
import { getLocalWorkerdCompatibilityDate } from "@cloudflare/workers-utils";
45
import * as recast from "recast";
6+
import semiver from "semiver";
57
import { mergeObjectProperties, transformFile } from "../c3-vendor/codemod";
68
import { usesTypescript } from "../uses-typescript";
9+
import { getInstalledPackageVersion } from "./utils/packages";
710
import { Framework } from ".";
811
import type { ConfigurationOptions, ConfigurationResults } from ".";
912

@@ -13,45 +16,13 @@ export class SolidStart extends Framework {
1316
dryRun,
1417
}: ConfigurationOptions): Promise<ConfigurationResults> {
1518
if (!dryRun) {
16-
const filePath = `app.config.${usesTypescript(projectPath) ? "ts" : "js"}`;
19+
const solidStartVersion = getSolidStartVersion(projectPath);
1720

18-
const { date: compatDate } = getLocalWorkerdCompatibilityDate({
19-
projectPath,
20-
});
21-
22-
updateStatus(`Updating configuration in ${blue(filePath)}`);
23-
24-
transformFile(filePath, {
25-
visitCallExpression: function (n) {
26-
const callee = n.node.callee as recast.types.namedTypes.Identifier;
27-
if (callee.name !== "defineConfig") {
28-
return this.traverse(n);
29-
}
30-
31-
const b = recast.types.builders;
32-
mergeObjectProperties(
33-
n.node.arguments[0] as recast.types.namedTypes.ObjectExpression,
34-
[
35-
b.objectProperty(
36-
b.identifier("server"),
37-
b.objectExpression([
38-
// preset: "cloudflare_module"
39-
b.objectProperty(
40-
b.identifier("preset"),
41-
b.stringLiteral("cloudflare_module")
42-
),
43-
b.objectProperty(
44-
b.identifier("compatibilityDate"),
45-
b.stringLiteral(compatDate)
46-
),
47-
])
48-
),
49-
]
50-
);
51-
52-
return false;
53-
},
54-
});
21+
if (semiver(solidStartVersion, "2.0.0-alpha") < 0) {
22+
updateAppConfigFile(projectPath);
23+
} else {
24+
updateViteConfigFile(projectPath);
25+
}
5526
}
5627

5728
return {
@@ -65,3 +36,113 @@ export class SolidStart extends Framework {
6536
};
6637
}
6738
}
39+
40+
/**
41+
* This functions updates the `vite.config.(js|ts)` files used by SolidStart applications
42+
* to use the `cloudflare-module` preset to target Cloudflare Workers.
43+
*
44+
* Note: SolidStart projects prior to version `2.0.0-alpha` used to have an `app.config.(js|ts)` file instead
45+
*
46+
* @param projectPath The path of the project
47+
*/
48+
function updateViteConfigFile(projectPath: string): void {
49+
const filePath = `vite.config.${usesTypescript(projectPath) ? "ts" : "js"}`;
50+
51+
transformFile(filePath, {
52+
visitCallExpression: function (n) {
53+
const callee = n.node.callee as recast.types.namedTypes.Identifier;
54+
if (callee.name !== "nitro") {
55+
return this.traverse(n);
56+
}
57+
58+
const b = recast.types.builders;
59+
const presetProp = b.objectProperty(
60+
b.identifier("preset"),
61+
b.stringLiteral("cloudflare-module")
62+
);
63+
64+
if (n.node.arguments.length === 0) {
65+
n.node.arguments.push(b.objectExpression([presetProp]));
66+
} else {
67+
mergeObjectProperties(
68+
n.node.arguments[0] as recast.types.namedTypes.ObjectExpression,
69+
[presetProp]
70+
);
71+
}
72+
73+
return false;
74+
},
75+
});
76+
}
77+
78+
/**
79+
* SolidStart apps used to have an `app.config.(js|ts)` before version `2.0.0-alpha`
80+
* (afterwards this has been replaced by `vite.config.(js|ts)`).
81+
* Reference: https://github.com/solidjs/templates/commit/c4cd73e08bdc
82+
*
83+
* This functions updates the `app.config.(js|ts)` to use the `cloudflare_module` preset
84+
* to target Cloudflare Workers.
85+
*
86+
* @param projectPath The path of the project
87+
*/
88+
function updateAppConfigFile(projectPath: string): void {
89+
const filePath = `app.config.${usesTypescript(projectPath) ? "ts" : "js"}`;
90+
91+
const { date: compatDate } = getLocalWorkerdCompatibilityDate({
92+
projectPath,
93+
});
94+
95+
updateStatus(`Updating configuration in ${blue(filePath)}`);
96+
97+
transformFile(filePath, {
98+
visitCallExpression: function (n) {
99+
const callee = n.node.callee as recast.types.namedTypes.Identifier;
100+
if (callee.name !== "defineConfig") {
101+
return this.traverse(n);
102+
}
103+
104+
const b = recast.types.builders;
105+
mergeObjectProperties(
106+
n.node.arguments[0] as recast.types.namedTypes.ObjectExpression,
107+
[
108+
b.objectProperty(
109+
b.identifier("server"),
110+
b.objectExpression([
111+
// preset: "cloudflare_module"
112+
b.objectProperty(
113+
b.identifier("preset"),
114+
b.stringLiteral("cloudflare_module")
115+
),
116+
b.objectProperty(
117+
b.identifier("compatibilityDate"),
118+
b.stringLiteral(compatDate)
119+
),
120+
])
121+
),
122+
]
123+
);
124+
125+
return false;
126+
},
127+
});
128+
}
129+
130+
/**
131+
* Gets the installed version of the "@solidjs/start" package
132+
*
133+
* @param projectPath The path of the project
134+
*/
135+
function getSolidStartVersion(projectPath: string): string {
136+
const packageName = "@solidjs/start";
137+
const solidStartVersion = getInstalledPackageVersion(
138+
packageName,
139+
projectPath
140+
);
141+
142+
assert(
143+
solidStartVersion,
144+
`Unable to discern the version of the \`${packageName}\` package`
145+
);
146+
147+
return solidStartVersion;
148+
}

0 commit comments

Comments
 (0)