Skip to content

Commit 5a873bb

Browse files
Support Angular in autoconfig (#11330)
* Support Angular in autoconfig * remove no-longer necessary name * add missing preview script * use parseJSONC to parse angular.json * adhere to new configure signature * remove preview script * remove `start` script from Angular c3`s template in favour of `preview`
1 parent e6aa865 commit 5a873bb

File tree

9 files changed

+143
-3
lines changed

9 files changed

+143
-3
lines changed

.changeset/few-owls-mate.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+
Update the Angular starter to set a `preview` script instead of a `start` one

.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, tanstack-start
558+
gatsby, svelte, docusaurus, astro, tanstack-start, 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
@@ -745,6 +745,26 @@ function getExperimentalFrameworkTestConfig(
745745
nodeCompat: true,
746746
verifyTypes: false,
747747
},
748+
{
749+
name: "angular:workers",
750+
argv: ["--platform", "workers"],
751+
testCommitMessage: true,
752+
timeout: LONG_TIMEOUT,
753+
unsupportedOSs: ["win32"],
754+
unsupportedPms: ["bun"],
755+
verifyDeploy: {
756+
route: "/",
757+
expectedText: "Congratulations! Your app is running.",
758+
},
759+
verifyPreview: {
760+
previewArgs: ["--inspector-port=0"],
761+
route: "/",
762+
expectedText: "Congratulations! Your app is running.",
763+
},
764+
nodeCompat: false,
765+
flags: ["--style", "sass"],
766+
verifyTypes: false,
767+
},
748768
];
749769
}
750770

packages/create-cloudflare/src/templates.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export function getFrameworkMap({ experimental = false }): TemplateMap {
241241
docusaurus: docusaurusTemplate,
242242
astro: astroTemplate,
243243
"tanstack-start": tanStackStartTemplate,
244+
angular: angularTemplate,
244245
};
245246
} else {
246247
return {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ 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 () => ({
101101
scripts: {
102-
start: `${npm} run build && wrangler dev`,
102+
preview: `${npm} run build && wrangler dev`,
103103
build: `ng build`,
104104
deploy: `${npm} run build && wrangler deploy`,
105105
"cf-typegen": `wrangler types`,
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 { parseJSONC } from "@cloudflare/workers-utils";
6+
import { dedent } from "../../utils/dedent";
7+
import { installPackages } from "../c3-vendor/packages";
8+
import { Framework } from ".";
9+
import type { ConfigurationOptions, ConfigurationResults } from ".";
10+
11+
export class Angular extends Framework {
12+
async configure({
13+
workerName,
14+
outputDir,
15+
dryRun,
16+
}: ConfigurationOptions): Promise<ConfigurationResults> {
17+
if (!dryRun) {
18+
await updateAngularJson(workerName);
19+
await overrideServerFile();
20+
await installAdditionalDependencies();
21+
}
22+
return {
23+
wranglerConfig: {
24+
main: "./dist/server/server.mjs",
25+
assets: {
26+
binding: "ASSETS",
27+
directory: `${outputDir}browser`,
28+
},
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 = parseJSONC(
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: 3 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";
@@ -15,6 +16,8 @@ export function getFramework(detectedFramework?: {
1516
return new SvelteKit(detectedFramework.name);
1617
case "tanstack-start":
1718
return new TanstackStart(detectedFramework.name);
19+
case "angular":
20+
return new Angular(detectedFramework.name);
1821
default:
1922
return new Static(detectedFramework?.name);
2023
}

0 commit comments

Comments
 (0)