Skip to content

Commit ea2de1f

Browse files
Support Angular in autoconfig
1 parent 2ca70b1 commit ea2de1f

File tree

8 files changed

+138
-2
lines changed

8 files changed

+138
-2
lines changed

.changeset/huge-onions-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
Support Angular in `--experimental` mode

.changeset/short-donuts-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Support Angular projects in autoconfig

packages/create-cloudflare/e2e/tests/cli/cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ describe("Create Cloudflare CLI", () => {
555555
npm create cloudflare -- --framework next -- --ts
556556
pnpm create cloudflare --framework next -- --ts
557557
Allowed Values:
558-
gatsby, svelte, docusaurus, astro
558+
gatsby, svelte, docusaurus, astro, angular
559559
--platform=<value>
560560
Whether the application should be deployed to Pages or Workers. This is only applicable for Frameworks templates that support both Pages and Workers.
561561
Allowed Values:

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,26 @@ function getExperimentalFrameworkTestConfig(
727727
],
728728
verifyTypes: false,
729729
},
730+
{
731+
name: "angular:workers",
732+
argv: ["--platform", "workers"],
733+
testCommitMessage: true,
734+
timeout: LONG_TIMEOUT,
735+
unsupportedOSs: ["win32"],
736+
unsupportedPms: ["bun"],
737+
verifyDeploy: {
738+
route: "/",
739+
expectedText: "Congratulations! Your app is running.",
740+
},
741+
verifyPreview: {
742+
previewArgs: ["--inspector-port=0"],
743+
route: "/",
744+
expectedText: "Congratulations! Your app is running.",
745+
},
746+
nodeCompat: false,
747+
flags: ["--style", "sass"],
748+
verifyTypes: false,
749+
},
730750
];
731751
}
732752

packages/create-cloudflare/src/templates.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export function getFrameworkMap({ experimental = false }): TemplateMap {
240240
svelte: svelteTemplate,
241241
docusaurus: docusaurusTemplate,
242242
astro: astroTemplate,
243+
angular: angularTemplate,
243244
};
244245
} else {
245246
return {

packages/create-cloudflare/templates/angular/workers/c3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const config: TemplateConfig = {
9494
path: "templates/angular/workers",
9595
devScript: "start",
9696
deployScript: "deploy",
97-
previewScript: "start",
97+
previewScript: "preview",
9898
generate,
9999
configure,
100100
transformPackageJson: async () => ({
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { readFile, writeFile } from "node:fs/promises";
2+
import { resolve } from "node:path";
3+
import { brandColor, dim } from "@cloudflare/cli/colors";
4+
import { spinner } from "@cloudflare/cli/interactive";
5+
import { dedent } from "../../utils/dedent";
6+
import { installPackages } from "../c3-vendor/packages";
7+
import { Framework } from ".";
8+
import type { ConfigurationOptions } from ".";
9+
import type { RawConfig } from "@cloudflare/workers-utils";
10+
11+
export class Angular extends Framework {
12+
name = "angular";
13+
14+
async configure({
15+
workerName,
16+
outputDir,
17+
dryRun,
18+
}: ConfigurationOptions): Promise<RawConfig> {
19+
if (!dryRun) {
20+
await updateAngularJson(workerName);
21+
await overrideServerFile();
22+
await installAdditionalDependencies();
23+
}
24+
return {
25+
main: "./dist/server/server.mjs",
26+
assets: {
27+
binding: "ASSETS",
28+
directory: `${outputDir}browser`,
29+
},
30+
};
31+
}
32+
}
33+
34+
async function updateAngularJson(projectName: string) {
35+
const s = spinner();
36+
s.start(`Updating angular.json config`);
37+
const angularJson = JSON.parse(
38+
await readFile(resolve("angular.json"), "utf8")
39+
) as AngularJson;
40+
41+
// Update builder
42+
const architectSection = angularJson.projects[projectName].architect;
43+
architectSection.build.options.outputPath = "dist";
44+
architectSection.build.options.outputMode = "server";
45+
architectSection.build.options.ssr.experimentalPlatform = "neutral";
46+
47+
await writeFile(
48+
resolve("angular.json"),
49+
JSON.stringify(angularJson, null, 2)
50+
);
51+
52+
s.stop(`${brandColor(`updated`)} ${dim(`\`angular.json\``)}`);
53+
}
54+
55+
async function overrideServerFile() {
56+
await writeFile(
57+
resolve("src/server.ts"),
58+
dedent`
59+
import { AngularAppEngine, createRequestHandler } from '@angular/ssr';
60+
61+
const angularApp = new AngularAppEngine();
62+
63+
/**
64+
* This is a request handler used by the Angular CLI (dev-server and during build).
65+
*/
66+
export const reqHandler = createRequestHandler(async (req) => {
67+
const res = await angularApp.handle(req);
68+
69+
return res ?? new Response('Page not found.', { status: 404 });
70+
});
71+
72+
export default { fetch: reqHandler };
73+
`
74+
);
75+
}
76+
77+
async function installAdditionalDependencies() {
78+
await installPackages(["xhr2"], {
79+
dev: true,
80+
startText: "Installing additional dependencies",
81+
doneText: `${brandColor("installed")}`,
82+
});
83+
}
84+
85+
type AngularJson = {
86+
projects: Record<
87+
string,
88+
{
89+
architect: {
90+
build: {
91+
options: {
92+
outputPath: string;
93+
outputMode: string;
94+
ssr: Record<string, unknown>;
95+
assets: string[];
96+
};
97+
};
98+
};
99+
}
100+
>;
101+
};

packages/wrangler/src/autoconfig/frameworks/get-framework.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Angular } from "./angular";
12
import { Astro } from "./astro";
23
import { Static } from "./static";
34
import { SvelteKit } from "./sveltekit";
@@ -9,6 +10,9 @@ export function getFramework(id: string) {
910
if (id === "svelte-kit") {
1011
return new SvelteKit();
1112
}
13+
if (id === "angular") {
14+
return new Angular();
15+
}
1216

1317
return new Static(id);
1418
}

0 commit comments

Comments
 (0)