|
| 1 | +import assert from "node:assert"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { updateStatus } from "@cloudflare/cli"; |
| 5 | +import { blue } from "@cloudflare/cli/colors"; |
| 6 | +import * as recast from "recast"; |
| 7 | +import semiver from "semiver"; |
| 8 | +import { getDevCompatibilityDate } from "../../utils/compatibility-date"; |
| 9 | +import { mergeObjectProperties, transformFile } from "../c3-vendor/codemod"; |
| 10 | +import { Framework, getInstalledPackageVersion } from "."; |
| 11 | +import type { ConfigurationOptions, ConfigurationResults } from "."; |
| 12 | + |
| 13 | +export class Analog extends Framework { |
| 14 | + async configure({ |
| 15 | + dryRun, |
| 16 | + projectPath, |
| 17 | + }: ConfigurationOptions): Promise<ConfigurationResults> { |
| 18 | + checkMinimumAnalogVersion(projectPath); |
| 19 | + |
| 20 | + if (!dryRun) { |
| 21 | + await updateViteConfig(projectPath); |
| 22 | + } |
| 23 | + return { |
| 24 | + wranglerConfig: { |
| 25 | + main: "./dist/analog/server/index.mjs", |
| 26 | + assets: { |
| 27 | + binding: "ASSETS", |
| 28 | + directory: "./dist/analog/public", |
| 29 | + }, |
| 30 | + }, |
| 31 | + }; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function updateViteConfig(projectPath: string) { |
| 36 | + const viteConfigTsPAth = join(projectPath, "vite.config.ts"); |
| 37 | + const viteConfigJsPath = join(projectPath, "vite.config.js"); |
| 38 | + |
| 39 | + let viteConfigPath: string; |
| 40 | + |
| 41 | + if (existsSync(viteConfigTsPAth)) { |
| 42 | + viteConfigPath = viteConfigTsPAth; |
| 43 | + } else if (existsSync(viteConfigJsPath)) { |
| 44 | + viteConfigPath = viteConfigJsPath; |
| 45 | + } else { |
| 46 | + throw new Error("Could not find Vite config file to modify"); |
| 47 | + } |
| 48 | + |
| 49 | + const compatDate = getDevCompatibilityDate(undefined); |
| 50 | + |
| 51 | + updateStatus(`Updating configuration in ${blue(viteConfigPath)}`); |
| 52 | + |
| 53 | + transformFile(viteConfigPath, { |
| 54 | + visitCallExpression: function (n) { |
| 55 | + const callee = n.node.callee as recast.types.namedTypes.Identifier; |
| 56 | + if (callee.name !== "analog") { |
| 57 | + return this.traverse(n); |
| 58 | + } |
| 59 | + |
| 60 | + const b = recast.types.builders; |
| 61 | + const presetDef = [ |
| 62 | + b.objectProperty( |
| 63 | + b.identifier("nitro"), |
| 64 | + b.objectExpression([ |
| 65 | + // preset: "cloudflare_module" |
| 66 | + b.objectProperty( |
| 67 | + b.identifier("preset"), |
| 68 | + b.stringLiteral("cloudflare_module") |
| 69 | + ), |
| 70 | + b.objectProperty( |
| 71 | + b.identifier("compatibilityDate"), |
| 72 | + b.stringLiteral(compatDate) |
| 73 | + ), |
| 74 | + ]) |
| 75 | + ), |
| 76 | + ]; |
| 77 | + |
| 78 | + if (n.node.arguments.length === 0) { |
| 79 | + n.node.arguments.push(b.objectExpression(presetDef)); |
| 80 | + } else { |
| 81 | + mergeObjectProperties( |
| 82 | + n.node.arguments[0] as recast.types.namedTypes.ObjectExpression, |
| 83 | + presetDef |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + }, |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Checks that the project's analog version to ensure that it is greater than 2.0.0, an error is thrown if it isn't. |
| 94 | + * |
| 95 | + * We preform this check because, prior to v2 Analog had a different implementation, so the autoconfig configuration steps |
| 96 | + * would be significantly different for such versions, also some of those versions had some incompatibility with what was |
| 97 | + * at the time our integration solution with Analog (and we didn't get to the bottom of those issues), for these two reasons |
| 98 | + * we just say what analog pre-v2 is not supported (of course we can always revisit this in the future is needed). |
| 99 | + * |
| 100 | + * @param projectPath The path of the project |
| 101 | + */ |
| 102 | +function checkMinimumAnalogVersion(projectPath: string): void { |
| 103 | + const analogJsVersion = getInstalledPackageVersion( |
| 104 | + "@analogjs/platform", |
| 105 | + projectPath |
| 106 | + ); |
| 107 | + |
| 108 | + assert( |
| 109 | + analogJsVersion, |
| 110 | + "Unable to discern the version of the `@analogjs/platform` package" |
| 111 | + ); |
| 112 | + |
| 113 | + if (semiver(analogJsVersion, "2.0.0") < 0) { |
| 114 | + // Note: analog, prior to v2 had a different implementation so the configuration steps here would be significantly different, |
| 115 | + // also some of those analog versions had some incompatibility with what was at the time our integration solution with analog, |
| 116 | + // for these two reasons we just say what analog pre-v2 is not supported |
| 117 | + throw new Error("Analog versions earlier than 2.0.0 are not supported"); |
| 118 | + } |
| 119 | +} |
0 commit comments