1+ import assert from "node:assert" ;
12import { updateStatus } from "@cloudflare/cli" ;
23import { blue } from "@cloudflare/cli/colors" ;
34import { getLocalWorkerdCompatibilityDate } from "@cloudflare/workers-utils" ;
45import * as recast from "recast" ;
6+ import semiver from "semiver" ;
57import { mergeObjectProperties , transformFile } from "../c3-vendor/codemod" ;
68import { usesTypescript } from "../uses-typescript" ;
9+ import { getInstalledPackageVersion } from "./utils/packages" ;
710import { Framework } from "." ;
811import 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