1- import { cp , rm } from "node:fs/promises" ;
1+ import { cp } from "node:fs/promises" ;
22import { resolve } from "node:path" ;
33import { logRaw } from "@cloudflare/cli" ;
44import { brandColor , dim } from "@cloudflare/cli/colors" ;
55import { spinner } from "@cloudflare/cli/interactive" ;
6- import {
7- installPackages ,
8- runCommand ,
9- runFrameworkGenerator ,
10- } from "helpers/command" ;
6+ import { installPackages , runFrameworkGenerator } from "helpers/command" ;
117import { compatDateFlag , readFile , readJSON , writeFile } from "helpers/files" ;
128import { detectPackageManager } from "helpers/packages" ;
139import { getFrameworkCli } from "../index" ;
1410import type { FrameworkConfig , PagesGeneratorContext } from "types" ;
1511
16- const { dlx, npx , npm } = detectPackageManager ( ) ;
12+ const { dlx, npm } = detectPackageManager ( ) ;
1713
1814const generate = async ( ctx : PagesGeneratorContext ) => {
1915 const cli = getFrameworkCli ( ctx ) ;
2016
21- await runFrameworkGenerator (
22- ctx ,
23- `${ dlx } ${ cli } new ${ ctx . project . name } --standalone`
24- ) ;
17+ await runFrameworkGenerator ( ctx , `${ dlx } ${ cli } ${ ctx . project . name } --ssr` ) ;
2518
2619 logRaw ( "" ) ;
2720} ;
2821
2922const configure = async ( ctx : PagesGeneratorContext ) => {
3023 process . chdir ( ctx . project . path ) ;
31- await runCommand ( `${ npx } ng analytics disable` , {
32- silent : true ,
33- } ) ;
34- await addSSRAdapter ( ) ;
35- await installCFWorker ( ctx ) ;
36- await updateAppCode ( ) ;
3724 updateAngularJson ( ctx ) ;
25+ await updateAppCode ( ) ;
26+ await installCFWorker ( ctx ) ;
3827} ;
3928
4029const config : FrameworkConfig = {
4130 generate,
4231 configure,
4332 displayName : "Angular" ,
4433 getPackageScripts : async ( ) => ( {
45- process :
46- "node ./tools/copy-worker-files.mjs && node ./tools/copy-client-files.mjs && node ./tools/bundle.mjs" ,
47- "pages:build" : `${ npm } run build:ssr && ${ npm } run process` ,
4834 start : `${ npm } run pages:build && wrangler pages dev dist/cloudflare ${ await compatDateFlag ( ) } --experimental-local` ,
35+ process : "node ./tools/copy-files.mjs && node ./tools/alter-polyfills.mjs" ,
36+ "pages:build" : `ng build && ${ npm } run process` ,
4937 deploy : `${ npm } run pages:build && wrangler pages deploy dist/cloudflare` ,
5038 } ) ,
5139 deployCommand : "deploy" ,
5240 devCommand : "start" ,
53- testFlags : [ "--routing " , "--style" , "sass" ] ,
41+ testFlags : [ "--ssr " , "--style" , "sass" ] ,
5442} ;
5543export default config ;
5644
@@ -66,67 +54,51 @@ async function installCFWorker(ctx: PagesGeneratorContext) {
6654 s . stop ( `${ brandColor ( "copied" ) } ${ dim ( "adapter code" ) } ` ) ;
6755
6856 await installPackages (
69- [
70- "@cloudflare/workers-types" ,
71- "@esbuild-plugins/node-globals-polyfill" ,
72- "@esbuild-plugins/node-modules-polyfill" ,
73- "@miniflare/tre@next" ,
74- "esbuild" ,
75- "fast-glob" ,
76- "wrangler@beta" ,
77- ] ,
57+ [ "@cloudflare/workers-types" , "@miniflare/tre@next" , "wrangler@beta" ] ,
7858 {
7959 dev : true ,
8060 startText : "Installing adapter dependencies" ,
8161 doneText : `${ brandColor ( "installed" ) } ${ dim ( `via \`${ npm } install\`` ) } ` ,
8262 }
8363 ) ;
8464}
85-
86- async function addSSRAdapter ( ) {
87- const cmd = `${ npx } ng add @nguniversal/express-engine` ;
88-
89- await runCommand ( `${ cmd } --skip-confirmation` , {
90- silent : true ,
91- startText : "Installing Angular SSR" ,
92- doneText : `${ brandColor ( "installed" ) } ${ dim ( `via \`${ cmd } \`` ) } ` ,
93- } ) ;
94- }
95-
9665async function updateAppCode ( ) {
9766 const s = spinner ( ) ;
9867 s . start ( `Updating application code` ) ;
9968
10069 // Update an app config file to:
101- // - add the `provideClientHydration()` provider to enable hydration
10270 // - add the `provideHttpClient(withFetch())` call to enable `fetch` usage in `HttpClient`
10371 const appConfigPath = "src/app/app.config.ts" ;
10472 const appConfig = readFile ( resolve ( appConfigPath ) ) ;
10573 const newAppConfig =
106- "import { provideClientHydration } from '@angular/platform-browser';\n" +
10774 "import { provideHttpClient, withFetch } from '@angular/common/http';\n" +
10875 appConfig . replace (
10976 "providers: [" ,
110- "providers: [provideHttpClient(withFetch()), provideClientHydration(), "
77+ "providers: [provideHttpClient(withFetch()), "
11178 ) ;
11279 writeFile ( resolve ( appConfigPath ) , newAppConfig ) ;
80+ s . stop ( `${ brandColor ( `updated` ) } ${ dim ( appConfigPath ) } ` ) ;
11381
114- // Remove the unwanted node.js server entry-point
115- await rm ( resolve ( "server.ts" ) ) ;
82+ // Remove unwanted dependencies
83+ s . start ( `Updating package.json` ) ;
84+ const packageJsonPath = resolve ( "package.json" ) ;
85+ const packageManifest = readJSON ( packageJsonPath ) ;
11686
117- s . stop ( `${ brandColor ( `updated` ) } ${ dim ( appConfigPath ) } ` ) ;
87+ delete packageManifest [ "dependencies" ] [ "@angular/ssr" ] ;
88+ delete packageManifest [ "dependencies" ] [ "express" ] ;
89+
90+ writeFile ( packageJsonPath , JSON . stringify ( packageManifest , null , 2 ) ) ;
91+ s . stop ( `${ brandColor ( `updated` ) } ${ dim ( `\`package.json\`` ) } ` ) ;
11892}
11993
12094function updateAngularJson ( ctx : PagesGeneratorContext ) {
12195 const s = spinner ( ) ;
12296 s . start ( `Updating angular.json config` ) ;
12397 const angularJson = readJSON ( resolve ( "angular.json" ) ) ;
98+ // Update builder
12499 const architectSection = angularJson . projects [ ctx . project . name ] . architect ;
125- architectSection . build . options . outputPath = "dist/browser " ;
100+ architectSection . build . options . outputPath = "dist" ;
126101 architectSection . build . options . assets . push ( "src/_routes.json" ) ;
127- architectSection . server . options . outputPath = "dist/server" ;
128- architectSection . server . options . main = "src/main.server.ts" ;
129- delete architectSection [ "serve-ssr" ] ;
130102
131103 writeFile ( resolve ( "angular.json" ) , JSON . stringify ( angularJson , null , 2 ) ) ;
132104 s . stop ( `${ brandColor ( `updated` ) } ${ dim ( `\`angular.json\`` ) } ` ) ;
0 commit comments