11import { VERSION } from '@angular/cli' ;
22import { getWorkspace , NG_APP_NAME , normalizePath } from '@bitdev/angular.dev-services.common' ;
33import { ApplicationBuilderOptions , SsrClass } from '@bitdev/angular.dev-services.ng-compat' ;
4- import { AngularPreview } from '@bitdev/angular.dev-services.preview.preview' ;
4+ import { AngularVitePreview } from '@bitdev/angular.dev-services.preview.vite-preview' ;
5+ import { buildApplication , generateAppTsConfig , getEnvFile , serveApplication } from '@bitdev/angular.dev-services.vite' ;
56import {
67 AppBuildContext ,
78 AppBuildResult ,
@@ -11,25 +12,17 @@ import {
1112 DeployFn
1213} from '@teambit/application' ;
1314import { Bundler , BundlerContext , DevServerContext } from '@teambit/bundler' ;
14- import { Component } from '@teambit/component' ;
1515import { DependencyResolverAspect , DependencyResolverMain } from '@teambit/dependency-resolver' ;
1616import { EnvContext , EnvHandler } from '@teambit/envs' ;
17- import { CACHE_ROOT } from '@teambit/legacy/dist/constants.js ' ;
17+ import { CACHE_ROOT } from '@teambit/legacy.constants ' ;
1818import { Preview } from '@teambit/preview' ;
1919import { Port } from '@teambit/toolbox.network.get-port' ;
2020import { Workspace } from '@teambit/workspace' ;
2121import assert from 'assert' ;
2222import fs from 'fs-extra' ;
2323import { cloneDeep } from 'lodash-es' ;
24- import objectHash from 'object-hash' ;
2524import { join } from 'path' ;
26- import ts from 'typescript' ;
2725import { AngularAppOptions } from './angular-app-options.js' ;
28- import { buildApplication } from './application.bundler.js' ;
29- import { serveApplication } from './application.dev-server.js' ;
30- import { expandIncludeExclude , JsonObject } from './utils.js' ;
31-
32- const writeHash = new Map < string , string > ( ) ;
3326
3427export class AngularApp implements Application {
3528 readonly name : string ;
@@ -63,10 +56,9 @@ export class AngularApp implements Application {
6356 }
6457
6558 private getDevServerContext ( context : AppContext ) : DevServerContext {
66- // const ngEnvOptions = this.angularEnv.getNgEnvOptions();
6759 return Object . assign ( cloneDeep ( context ) , {
6860 entry : [ ] ,
69- rootPath : /*ngEnvOptions.devServer === 'vite' ? _appRootPath : */ '' ,
61+ rootPath : '' ,
7062 publicPath : `${ this . publicDir } /${ this . options . name } ` ,
7163 title : this . options . name
7264 } ) ;
@@ -93,48 +85,11 @@ export class AngularApp implements Application {
9385 const angularServeOptions : any = Object . assign ( cloneDeep ( this . options . angularServeOptions ) , { tsConfig : tsconfigPath } ) ;
9486 const angularBuildOptions : any = Object . assign ( cloneDeep ( this . options . angularBuildOptions ) , { tsConfig : tsconfigPath } ) ;
9587
96- return AngularPreview . from ( {
97- webpackServeTransformers : this . options . webpackServeTransformers ,
98- webpackBuildTransformers : this . options . webpackBuildTransformers ,
88+ return AngularVitePreview . from ( {
9989 angularServeOptions,
10090 angularBuildOptions,
101- ngEnvOptions : this . options . ngEnvOptions ,
10291 sourceRoot : this . options . sourceRoot
10392 } ) ;
104-
105- }
106-
107- private generateTsConfig ( bitCmps : Component [ ] , appRootPath : string , appTsconfigPath : string , tsconfigPath : string , depsResolver : DependencyResolverMain , workspace ?: Workspace , serverEntry ?: string ) : void {
108- const tsconfigJSON : JsonObject = ts . readConfigFile ( appTsconfigPath , ts . sys . readFile ) . config ;
109-
110- // Add the paths to tsconfig to remap bit components to local folders
111- tsconfigJSON . compilerOptions . paths = tsconfigJSON . compilerOptions . paths || { } ;
112- bitCmps . forEach ( ( dep : Component ) => {
113- let componentDir = workspace ?. componentDir ( dep . id , {
114- ignoreVersion : true
115- } ) ;
116- if ( componentDir ) {
117- componentDir = normalizePath ( componentDir ) ;
118- const pkgName = depsResolver . getPackageName ( dep ) ;
119- // TODO we should find a way to use the real entry file based on the component config because people can change it
120- if ( fs . existsSync ( join ( componentDir , 'public-api.ts' ) ) ) {
121- tsconfigJSON . compilerOptions . paths [ pkgName ] = [ `${ componentDir } /public-api.ts` , `${ componentDir } ` ] ;
122- }
123- tsconfigJSON . compilerOptions . paths [ `${ pkgName } /*` ] = [ `${ componentDir } /*` ] ;
124- }
125- } ) ;
126-
127- if ( serverEntry ) {
128- tsconfigJSON . files . push ( serverEntry ) ;
129- }
130-
131- const tsconfigContent = expandIncludeExclude ( tsconfigJSON , tsconfigPath , [ appRootPath ] ) ;
132- const hash = objectHash ( tsconfigContent ) ;
133- // write only if link has changed (prevents triggering fs watches)
134- if ( writeHash . get ( tsconfigPath ) !== hash ) {
135- fs . outputJsonSync ( tsconfigPath , tsconfigContent , { spaces : 2 } ) ;
136- writeHash . set ( tsconfigPath , hash ) ;
137- }
13893 }
13994
14095 /**
@@ -145,16 +100,6 @@ export class AngularApp implements Application {
145100 return context as any as EnvContext ;
146101 }
147102
148- private async getEnvFile ( mode : string , rootDir : string , overrides ?: Record < string , string > ) {
149- // TODO: enable this one we have ESM envs, otherwise we get a warning message about loading the deprecated CJS build of Vite
150- // const vite = await import('vite');
151- // const dotenv = vite.loadEnv(mode, rootDir);
152- return {
153- ...overrides
154- // ...dotenv
155- } ;
156- }
157-
158103 // TODO: fix return type once bit has a new stable version
159104 async run ( context : AppContext ) : Promise < ApplicationInstance > {
160105 const depsResolver = context . getAspect < DependencyResolverMain > ( DependencyResolverAspect . id ) ;
@@ -171,32 +116,23 @@ export class AngularApp implements Application {
171116 const bitCmps = await workspace . getMany ( workspaceCmpsIDs ) ;
172117 const tempFolder = this . getTempFolder ( workspace ) ;
173118 const tsconfigPath = this . getTsconfigPath ( tempFolder ) ;
174- this . generateTsConfig ( bitCmps , appRootPath , appTsconfigPath , tsconfigPath , depsResolver , workspace ) ;
175-
176- if ( Number ( VERSION . major ) >= 16 ) {
177- const envVars = await this . getEnvFile ( 'development' , appRootPath , context . envVariables as any ) ;
178- await serveApplication ( {
179- angularOptions : {
180- ...this . options . angularBuildOptions as ApplicationBuilderOptions ,
181- tsConfig : tsconfigPath
182- } ,
183- sourceRoot : this . options . sourceRoot || 'src' ,
184- workspaceRoot : appRootPath ,
185- port,
186- logger : logger ,
187- tempFolder : tempFolder ,
188- envVars : {
189- process : { env : envVars }
190- }
191- } ) ;
192- } else {
193- const devServerContext = this . getDevServerContext ( context ) ;
194- const envContext = this . getEnvContext ( context ) ;
195- const preview = this . getPreview ( tsconfigPath ) ( envContext ) ;
196-
197- const devServer = await preview . getDevServer ( devServerContext ) ( envContext ) ;
198- await devServer . listen ( port ) ;
199- }
119+ generateAppTsConfig ( bitCmps , appRootPath , appTsconfigPath , tsconfigPath , depsResolver , workspace ) ;
120+
121+ const envVars = await getEnvFile ( 'development' , appRootPath , context . envVariables as any ) ;
122+ await serveApplication ( {
123+ angularOptions : {
124+ ...this . options . angularBuildOptions as ApplicationBuilderOptions ,
125+ tsConfig : tsconfigPath
126+ } ,
127+ sourceRoot : this . options . sourceRoot || 'src' ,
128+ workspaceRoot : appRootPath ,
129+ port,
130+ logger : logger ,
131+ tempFolder : tempFolder ,
132+ envVars : {
133+ process : { env : envVars }
134+ }
135+ } ) ;
200136
201137 return {
202138 appName : this . name ,
@@ -221,11 +157,12 @@ export class AngularApp implements Application {
221157 }
222158 const tempFolder = this . getTempFolder ( ) ;
223159 const tsconfigPath = this . getTsconfigPath ( tempFolder ) ;
224- this . generateTsConfig ( [ capsule . component ] , appRootPath , appTsconfigPath , tsconfigPath , depsResolver , undefined , entryServer ) ;
160+ generateAppTsConfig ( [ capsule . component ] , appRootPath , appTsconfigPath , tsconfigPath , depsResolver , undefined , entryServer ? [ entryServer ] : undefined ) ;
225161
226- if ( ! this . options . bundler && Number ( VERSION . major ) >= 16 ) {
227- const envVars = await this . getEnvFile ( 'production' , appRootPath , context . envVariables as any ) ;
228- await buildApplication ( {
162+ const errors : Error [ ] = [ ] ;
163+ if ( ! this . options . bundler ) {
164+ const envVars = await getEnvFile ( 'production' , appRootPath , context . envVariables ) ;
165+ const results = await buildApplication ( {
229166 angularOptions : {
230167 ...appOptions ,
231168 tsConfig : tsconfigPath
@@ -240,7 +177,11 @@ export class AngularApp implements Application {
240177 'process.env' : envVars
241178 }
242179 } ) ;
243- console . log ( 'build done' ) ;
180+ for ( const result of results ) {
181+ if ( result . error ) {
182+ errors . push ( new Error ( result . error ) ) ;
183+ }
184+ }
244185 } else {
245186 let bundler : Bundler ;
246187 if ( this . options . bundler ) {
@@ -252,9 +193,15 @@ export class AngularApp implements Application {
252193
253194 bundler = await preview . getBundler ( bundlerContext ) ( envContext ) ;
254195 }
255- await bundler . run ( ) ;
196+ const results = await bundler . run ( ) ;
197+ for ( const result of results ) {
198+ if ( result . errors ) {
199+ errors . push ( ...result . errors ) ;
200+ }
201+ }
256202 }
257203 return {
204+ errors,
258205 artifacts : [ {
259206 name : this . name ,
260207 globPatterns : [ outputPath ] ,
0 commit comments