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/c3-frameworks-update-17266.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-cloudflare": patch
---

Scaffold new Analog projects with `create-analog` 3.0.0

C3 configures Analog 3's standalone Nitro plugin to build for Cloudflare Workers, so new projects can use the generated preview and deploy scripts.
9 changes: 9 additions & 0 deletions .changeset/configure-analog-three.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@cloudflare/autoconfig": minor
"create-cloudflare": patch
"wrangler": minor
---

Support Analog 3 in `wrangler setup`

Configure Analog’s standalone Nitro Vite plugin for Cloudflare Workers. Both `wrangler setup` and `create-cloudflare` configure the Minimal starter as a static site. Analog 2 projects continue to use the Nitro options on the Analog Vite plugin. Projects using native Nitro 3 (`nitro/vite`) should use Nitro’s generated Wrangler configuration instead.
2 changes: 1 addition & 1 deletion packages/autoconfig/src/frameworks/all-frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getAllKnownFrameworks(target: AutoConfigTarget) {
// Analog didn't work well before 2.0.0 with Cloudflare
// See: https://github.com/cloudflare/workers-sdk/issues/11470
minimumVersion: "2.0.0",
maximumKnownMajorVersion: "2",
maximumKnownMajorVersion: "3",
Comment thread
petebacondarwin marked this conversation as resolved.
Resolved
},
supportsCf: false,
supported: true,
Expand Down
203 changes: 141 additions & 62 deletions packages/autoconfig/src/frameworks/analog.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,173 @@
import { existsSync } from "node:fs";
import { existsSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { updateStatus } from "@cloudflare/cli-shared-helpers";
import { blue } from "@cloudflare/cli-shared-helpers/colors";
import {
isAnalogStaticSite,
mergeObjectProperties,
transformFile,
parseFile,
} from "@cloudflare/shared-ast-primitives";
import { DEFAULT_COMPAT_DATE } from "@cloudflare/workers-utils";
import * as recast from "recast";
import { major } from "semver";
import { AutoConfigFrameworkConfigurationError } from "../errors";
import { Framework } from "./framework-class";
import type {
ConfigurationOptions,
ConfigurationResults,
} from "./framework-class";

export class Analog extends Framework {
async configure({
configure({
dryRun,
projectPath,
}: ConfigurationOptions): Promise<ConfigurationResults> {
if (!dryRun) {
await updateViteConfig(projectPath);
}
}: ConfigurationOptions): ConfigurationResults {
const staticSite = configureVite(
projectPath,
major(this.frameworkVersion) >= 3,
dryRun
);

return {
buildTool: "wrangler",
workerConfig: {
entrypoint: "./dist/analog/server/index.mjs",
env: {
ASSETS: { type: "assets" },
},
},
workerConfig: staticSite
? { assets: { notFoundHandling: "single-page-application" } }
: {
entrypoint: "./dist/analog/server/index.mjs",
env: { ASSETS: { type: "assets" } },
},
buildConfig: { assetsDirectory: "./dist/analog/public" },
};
}
}

async function updateViteConfig(projectPath: string) {
const viteConfigTsPAth = join(projectPath, "vite.config.ts");
const viteConfigJsPath = join(projectPath, "vite.config.js");

let viteConfigPath: string;

if (existsSync(viteConfigTsPAth)) {
viteConfigPath = viteConfigTsPAth;
} else if (existsSync(viteConfigJsPath)) {
viteConfigPath = viteConfigJsPath;
} else {
function configureVite(
projectPath: string,
standaloneNitro: boolean,
dryRun: boolean
) {
const viteConfigPath = ["vite.config.ts", "vite.config.js"]
.map((file) => join(projectPath, file))
.find(existsSync);
if (!viteConfigPath) {
throw new Error("Could not find Vite config file to modify");
}

const compatDate = DEFAULT_COMPAT_DATE;

updateStatus(`Updating configuration in ${blue(viteConfigPath)}`);

transformFile(viteConfigPath, {
visitCallExpression: function (n) {
const callee = n.node.callee as recast.types.namedTypes.Identifier;
if (callee.name !== "analog") {
return this.traverse(n);
}
const config = parseFile(viteConfigPath);
if (
standaloneNitro &&
config?.body.some(
(statement) =>
statement.type === "ImportDeclaration" &&
statement.source.value === "nitro/vite"
)
) {
throw new AutoConfigFrameworkConfigurationError(
"Wrangler setup cannot configure Analog's native Nitro 3 integration (nitro/vite). Set the cloudflare_module preset in Nitro and deploy using the Wrangler configuration generated by Nitro.",
{ telemetryMessage: "analog native nitro setup unsupported" }
);
}
const nitroImport = config?.body.find(
(statement) =>
statement.type === "ImportDeclaration" &&
statement.source.value === "@analogjs/vite-plugin-nitro"
);
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
const nitroPlugin =
nitroImport?.type === "ImportDeclaration"
? nitroImport.specifiers.find(
(specifier) =>
specifier.type === "ImportDefaultSpecifier" ||
(specifier.type === "ImportSpecifier" &&
specifier.imported.type === "Identifier" &&
specifier.imported.name === "nitro")
)?.local?.name
: undefined;
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
const platformImport = config?.body.find(
(statement) =>
statement.type === "ImportDeclaration" &&
statement.source.value === "@analogjs/platform"
);
const platformPlugin =
(platformImport?.type === "ImportDeclaration"
? platformImport.specifiers.find(
(specifier) => specifier.type === "ImportDefaultSpecifier"
)?.local?.name
: undefined) ?? "analog";
const pluginName = standaloneNitro ? nitroPlugin : platformPlugin;
const pluginDescription = standaloneNitro
? "the @analogjs/vite-plugin-nitro plugin"
: "the Analog Vite plugin";
let configured = false;
let platformOptions: recast.types.namedTypes.ObjectExpression | undefined;
let standaloneNitroOptions:
| recast.types.namedTypes.ObjectExpression
| undefined;
if (config && pluginName) {
recast.visit(config, {
visitCallExpression(call) {
const { callee, arguments: args } = call.node;
const pluginOptions = args[0];
if (callee.type === "Identifier" && callee.name === platformPlugin) {
platformOptions =
pluginOptions?.type === "ObjectExpression"
? pluginOptions
: undefined;
}
if (callee.type !== "Identifier" || callee.name !== pluginName) {
return this.traverse(call);
}

const b = recast.types.builders;
const presetDef = [
b.objectProperty(
b.identifier("nitro"),
b.objectExpression([
// preset: "cloudflare_module"
b.objectProperty(
b.identifier("preset"),
b.stringLiteral("cloudflare_module")
),
b.objectProperty(
b.identifier("compatibilityDate"),
b.stringLiteral(compatDate)
),
])
),
];
const builders = recast.types.builders;
if (standaloneNitro && pluginOptions?.type === "ObjectExpression") {
standaloneNitroOptions = pluginOptions;
}
const nitroOptions = [
builders.objectProperty(
builders.identifier("preset"),
builders.stringLiteral("cloudflare_module")
),
builders.objectProperty(
builders.identifier("compatibilityDate"),
builders.stringLiteral(DEFAULT_COMPAT_DATE)
),
];
const options = standaloneNitro
? nitroOptions
: [
builders.objectProperty(
builders.identifier("nitro"),
builders.objectExpression(nitroOptions)
),
];

if (n.node.arguments.length === 0) {
n.node.arguments.push(b.objectExpression(presetDef));
} else {
mergeObjectProperties(
n.node.arguments[0] as recast.types.namedTypes.ObjectExpression,
presetDef
);
}
if (args.length === 0) {
args.push(builders.objectExpression(options));
} else if (pluginOptions?.type === "ObjectExpression") {
mergeObjectProperties(pluginOptions, options);
} else {
throw new AutoConfigFrameworkConfigurationError(
`Cannot configure Analog for Cloudflare Workers: expected ${pluginDescription} in ${viteConfigPath} to receive an object literal or no arguments.`,
{ telemetryMessage: "unexpected vite config" }
);
}

return false;
},
});
configured = true;
return false;
},
});
}
if (!config || !configured) {
throw new AutoConfigFrameworkConfigurationError(
`Cannot configure Analog for Cloudflare Workers: no call to ${pluginDescription} found in ${viteConfigPath}.`,
{ telemetryMessage: "unexpected vite config" }
);
}
if (!dryRun) {
updateStatus(`Updating configuration in ${blue(viteConfigPath)}`);
writeFileSync(viteConfigPath, recast.print(config).code);
}
return (
standaloneNitro &&
isAnalogStaticSite(platformOptions, standaloneNitroOptions)
);
}
Loading
Loading