11/* eslint-disable no-param-reassign */
22import { OutputHashing } from '@angular-devkit/build-angular' ;
33import { VERSION } from '@angular/cli' ;
4- import {
5- ApplicationOptions ,
6- dedupPaths ,
7- getLoggerApi ,
8- normalizePath
9- } from '@bitdev/angular.dev-services.common' ;
10- import {
11- type ApplicationBuilderOptions ,
12- buildApplicationInternal
13- } from '@bitdev/angular.dev-services.ng-compat' ;
4+ import { dedupPaths , getLoggerApi , normalizePath } from '@bitdev/angular.dev-services.common' ;
5+ import { type ApplicationBuilderOptions , buildApplicationInternal } from '@bitdev/angular.dev-services.ng-compat' ;
146import { Logger } from '@teambit/logger' ;
157import assert from 'assert' ;
168import fs from 'fs-extra' ;
@@ -22,7 +14,7 @@ import definePlugin from './plugins/define.plugin.js';
2214import { getIndexInputFile } from './utils.js' ;
2315
2416export type BuildApplicationOptions = {
25- angularOptions : Partial < ApplicationOptions > ;
17+ angularOptions : Partial < ApplicationBuilderOptions > ;
2618 outputPath : string ;
2719 sourceRoot : string ;
2820 workspaceRoot : string ;
@@ -36,10 +28,10 @@ export type BuildApplicationOptions = {
3628const BUILDER_NAME = '@angular-devkit/build-angular:application' ;
3729
3830export async function buildApplication ( options : BuildApplicationOptions ) : Promise < void > {
39- const { angularOptions : { tsConfig, ssr , define } , envVars } = options ;
31+ const { angularOptions : { tsConfig, server , define } , envVars } = options ;
4032 assert ( tsConfig , 'tsConfig option is required' ) ;
41- const isSsr = ! ! ssr && Number ( VERSION . major ) >= 17 ;
42- if ( isSsr ) {
33+ const isSsr = ! ! server && Number ( VERSION . major ) >= 17 ;
34+ if ( isSsr && Number ( VERSION . major ) < 19 ) {
4335 addEntryServer ( options ) ;
4436 }
4537 const appOptions = getAppOptions ( options , isSsr ) ;
@@ -58,7 +50,8 @@ export async function buildApplication(options: BuildApplicationOptions): Promis
5850 }
5951 }
6052
61- if ( isSsr ) {
53+ // Versions of Angular <19 require a nitro middleware to support SSR API endpoints
54+ if ( isSsr && Number ( VERSION . major ) < 19 ) {
6255 await buildNitro ( options ) ;
6356 }
6457}
@@ -68,7 +61,7 @@ function addEntryServer(options: BuildApplicationOptions): void {
6861 if ( ssr && entryServer ) {
6962 const fileContent = `import type { ApplicationRef } from '@angular/core';
7063import { renderApplication, renderModule } from '@angular/platform-server';
71- import bootstrap from '${ server ?. replace ( extname ( server ) , '' ) } ';
64+ import bootstrap from '${ server ?. replace ( extname ( server ) , '' ) } ';
7265
7366function isBootstrapFn(value: unknown): value is () => Promise<ApplicationRef> {
7467 // We can differentiate between a module and a bootstrap function by reading compiler-generated "ɵmod" static property:
@@ -95,19 +88,20 @@ export default async function render(url: string, document: string) {
9588 }
9689}
9790
98- function getAppOptions ( options : BuildApplicationOptions , isSsr : boolean ) : any {
91+ function getAppOptions ( options : BuildApplicationOptions , isSsr : boolean ) : ApplicationBuilderOptions {
9992 const { entryServer, angularOptions, outputPath, sourceRoot, workspaceRoot } = options ;
10093
10194 // declare constants for all reusable values here
102- const normalizedIndex = `./${ join ( sourceRoot , 'index.html' ) } ` ;
103- const normalizedBrowser = `./${ join ( sourceRoot , 'main.ts' ) } ` ;
95+ const normalizedIndex = `./${ join ( sourceRoot , 'index.html' ) } ` ;
96+ const normalizedBrowser = `./${ join ( sourceRoot , 'main.ts' ) } ` ;
97+ const serverPath = `./${ join ( sourceRoot , 'main.server.ts' ) } ` ;
10498
10599 const dedupedAssets = dedupPaths ( [ posix . join ( sourceRoot , `assets/**/*` ) , ...( angularOptions . assets ?? [ ] ) ] ) ;
106- const dedupedStyles = dedupPaths ( [ posix . join ( sourceRoot , `styles.${ angularOptions . inlineStyleLanguage } ` ) , ...( angularOptions . styles ?? [ ] ) ] ) ;
100+ const dedupedStyles = dedupPaths ( [ posix . join ( sourceRoot , `styles.${ angularOptions . inlineStyleLanguage } ` ) , ...( angularOptions . styles ?? [ ] ) ] ) ;
107101
108102 return {
109103 ...angularOptions ,
110- baseHref : angularOptions . baseHref ?? '. /' ,
104+ baseHref : angularOptions . baseHref ?? '/' ,
111105 preserveSymlinks : false ,
112106 outputPath,
113107 index : angularOptions . index ?? normalizedIndex ,
@@ -117,13 +111,14 @@ function getAppOptions(options: BuildApplicationOptions, isSsr: boolean): any {
117111 styles : dedupedStyles ,
118112 scripts : angularOptions . scripts ,
119113 namedChunks : angularOptions . namedChunks ?? true ,
120- optimization : angularOptions . optimization ?? true ,
114+ optimization : false , // angularOptions.optimization ?? true,
121115 aot : true ,
122116 deleteOutputPath : true ,
123117 sourceMap : angularOptions . sourceMap ?? true ,
124- outputHashing : angularOptions . outputHashing ?? OutputHashing . All ,
118+ outputHashing : OutputHashing . None , // angularOptions.outputHashing ?? OutputHashing.All,
125119 watch : false ,
126- server : isSsr ? angularOptions . server : undefined ,
120+ outputMode : angularOptions . outputMode ?? ( isSsr ? 'server' : 'static' ) ,
121+ server : isSsr ? angularOptions . server ?? serverPath : undefined ,
127122 prerender : isSsr ? ( angularOptions . prerender ?? ! ! angularOptions . server ) : undefined ,
128123 ssr : isSsr ? {
129124 entry : entryServer
@@ -149,14 +144,15 @@ function getBuilderContext(options: BuildApplicationOptions, appOptions: Applica
149144 project : 'bit-ng-app-builder' ,
150145 target : 'build'
151146 } ,
152- getProjectMetadata : function ( projectName : string ) : Promise < any > {
147+ getProjectMetadata : function ( ) : Promise < any > {
153148 return Promise . resolve ( {
154149 root : '' ,
155150 sourceRoot,
156151 cli : { cache : { enabled : true , path : resolve ( tempFolder , 'angular/cache' ) } }
157152 } ) ;
158153 } ,
159- addTeardown : ( ) => { } ,
154+ addTeardown : ( ) => {
155+ } ,
160156 getBuilderNameForTarget : ( ) => Promise . resolve ( BUILDER_NAME ) ,
161157 getTargetOptions : ( ) => Promise . resolve ( appOptions as any ) ,
162158 validateOptions : ( ) => Promise . resolve ( appOptions as any )
@@ -177,18 +173,18 @@ async function getNitroConfig(options: BuildApplicationOptions): Promise<NitroCo
177173 const nitroDir = normalizePath ( resolve ( outputDir , 'ssr' ) ) ;
178174 const indexPath = getIndexInputFile ( index ! ) ;
179175
180- const prerenderedRoutes = prerender ? ( await import ( `${ outputDir } /prerendered-routes.json` , { assert : { type : 'json' } } ) ) . default : undefined ;
176+ const prerenderedRoutes = prerender ? ( await import ( `${ outputDir } /prerendered-routes.json` , { assert : { type : 'json' } } ) ) . default : undefined ;
181177
182178 return {
183179 rootDir : workspaceRoot ,
184180 logLevel : 1 , // TODO reset this to 3 or 2 https://github.com/unjs/consola/#log-level
185- srcDir : normalizePath ( `${ workspaceRoot } /src/server` ) ,
186- scanDirs : [ normalizePath ( `${ workspaceRoot } /src/server` ) ] ,
181+ srcDir : normalizePath ( `${ workspaceRoot } /src/server` ) ,
182+ scanDirs : [ normalizePath ( `${ workspaceRoot } /src/server` ) ] ,
187183 buildDir : resolve ( tempFolder , 'nitro' ) ,
188184
189185 alias : ssr ? {
190186 '#alias/entry.server' : normalizePath ( join ( serverDir , 'server.mjs' ) ) ,
191- '#alias/index' : normalizePath ( join ( serverDir , `${ basename ( indexPath , extname ( indexPath ) ) } .server.html` ) )
187+ '#alias/index' : normalizePath ( join ( serverDir , `${ basename ( indexPath , extname ( indexPath ) ) } .server.html` ) )
192188 } : { } ,
193189 serverAssets : ssr ? [ {
194190 baseName : 'public' ,
0 commit comments