|
| 1 | +import path from "node:path"; |
| 2 | +import { brandColor, dim } from "@cloudflare/cli/colors"; |
| 3 | +import * as recast from "recast"; |
| 4 | +import { mergeObjectProperties, transformFile } from "../c3-vendor/codemod"; |
| 5 | +import { installPackages } from "../c3-vendor/packages"; |
| 6 | +import { Framework } from "."; |
| 7 | +import type { ConfigurationOptions, ConfigurationResults } from "."; |
| 8 | + |
| 9 | +const updateNuxtConfig = (projectPath: string) => { |
| 10 | + const configFile = path.join(projectPath, "nuxt.config.ts"); |
| 11 | + |
| 12 | + const b = recast.types.builders; |
| 13 | + |
| 14 | + const presetDef = b.objectProperty( |
| 15 | + b.identifier("nitro"), |
| 16 | + b.objectExpression([ |
| 17 | + b.objectProperty( |
| 18 | + b.identifier("preset"), |
| 19 | + b.stringLiteral("cloudflare_module") |
| 20 | + ), |
| 21 | + b.objectProperty( |
| 22 | + b.identifier("cloudflare"), |
| 23 | + b.objectExpression([ |
| 24 | + b.objectProperty( |
| 25 | + b.identifier("deployConfig"), |
| 26 | + b.booleanLiteral(true) |
| 27 | + ), |
| 28 | + b.objectProperty(b.identifier("nodeCompat"), b.booleanLiteral(true)), |
| 29 | + ]) |
| 30 | + ), |
| 31 | + ]) |
| 32 | + ); |
| 33 | + |
| 34 | + const moduleDef = b.objectProperty( |
| 35 | + b.identifier("modules"), |
| 36 | + b.arrayExpression([b.stringLiteral("nitro-cloudflare-dev")]) |
| 37 | + ); |
| 38 | + |
| 39 | + transformFile(configFile, { |
| 40 | + visitCallExpression: function (n) { |
| 41 | + const callee = n.node.callee as recast.types.namedTypes.Identifier; |
| 42 | + if (callee.name === "defineNuxtConfig") { |
| 43 | + mergeObjectProperties( |
| 44 | + n.node.arguments[0] as recast.types.namedTypes.ObjectExpression, |
| 45 | + [presetDef, moduleDef] |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return this.traverse(n); |
| 50 | + }, |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +export class Nuxt extends Framework { |
| 55 | + async configure({ |
| 56 | + dryRun, |
| 57 | + projectPath, |
| 58 | + }: ConfigurationOptions): Promise<ConfigurationResults> { |
| 59 | + if (!dryRun) { |
| 60 | + await installPackages(["nitro-cloudflare-dev"], { |
| 61 | + dev: true, |
| 62 | + startText: "Installing the Cloudflare dev module", |
| 63 | + doneText: `${brandColor(`installed`)} ${dim("nitro-cloudflare-dev")}`, |
| 64 | + }); |
| 65 | + updateNuxtConfig(projectPath); |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + wranglerConfig: { |
| 70 | + main: "./.output/server/index.mjs", |
| 71 | + assets: { |
| 72 | + binding: "ASSETS", |
| 73 | + directory: "./.output/public/", |
| 74 | + }, |
| 75 | + observability: { |
| 76 | + enabled: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + configurationDescription = "Configuring project for Nuxt"; |
| 83 | +} |
0 commit comments