1
1
import { VERSION } from '@angular/cli' ;
2
2
import { getWorkspace , NG_APP_NAME , normalizePath } from '@bitdev/angular.dev-services.common' ;
3
3
import { 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' ;
5
6
import {
6
7
AppBuildContext ,
7
8
AppBuildResult ,
@@ -11,25 +12,17 @@ import {
11
12
DeployFn
12
13
} from '@teambit/application' ;
13
14
import { Bundler , BundlerContext , DevServerContext } from '@teambit/bundler' ;
14
- import { Component } from '@teambit/component' ;
15
15
import { DependencyResolverAspect , DependencyResolverMain } from '@teambit/dependency-resolver' ;
16
16
import { EnvContext , EnvHandler } from '@teambit/envs' ;
17
- import { CACHE_ROOT } from '@teambit/legacy/dist/constants.js ' ;
17
+ import { CACHE_ROOT } from '@teambit/legacy.constants ' ;
18
18
import { Preview } from '@teambit/preview' ;
19
19
import { Port } from '@teambit/toolbox.network.get-port' ;
20
20
import { Workspace } from '@teambit/workspace' ;
21
21
import assert from 'assert' ;
22
22
import fs from 'fs-extra' ;
23
23
import { cloneDeep } from 'lodash-es' ;
24
- import objectHash from 'object-hash' ;
25
24
import { join } from 'path' ;
26
- import ts from 'typescript' ;
27
25
import { 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 > ( ) ;
33
26
34
27
export class AngularApp implements Application {
35
28
readonly name : string ;
@@ -63,10 +56,9 @@ export class AngularApp implements Application {
63
56
}
64
57
65
58
private getDevServerContext ( context : AppContext ) : DevServerContext {
66
- // const ngEnvOptions = this.angularEnv.getNgEnvOptions();
67
59
return Object . assign ( cloneDeep ( context ) , {
68
60
entry : [ ] ,
69
- rootPath : /*ngEnvOptions.devServer === 'vite' ? _appRootPath : */ '' ,
61
+ rootPath : '' ,
70
62
publicPath : `${ this . publicDir } /${ this . options . name } ` ,
71
63
title : this . options . name
72
64
} ) ;
@@ -93,48 +85,11 @@ export class AngularApp implements Application {
93
85
const angularServeOptions : any = Object . assign ( cloneDeep ( this . options . angularServeOptions ) , { tsConfig : tsconfigPath } ) ;
94
86
const angularBuildOptions : any = Object . assign ( cloneDeep ( this . options . angularBuildOptions ) , { tsConfig : tsconfigPath } ) ;
95
87
96
- return AngularPreview . from ( {
97
- webpackServeTransformers : this . options . webpackServeTransformers ,
98
- webpackBuildTransformers : this . options . webpackBuildTransformers ,
88
+ return AngularVitePreview . from ( {
99
89
angularServeOptions,
100
90
angularBuildOptions,
101
- ngEnvOptions : this . options . ngEnvOptions ,
102
91
sourceRoot : this . options . sourceRoot
103
92
} ) ;
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
- }
138
93
}
139
94
140
95
/**
@@ -145,16 +100,6 @@ export class AngularApp implements Application {
145
100
return context as any as EnvContext ;
146
101
}
147
102
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
-
158
103
// TODO: fix return type once bit has a new stable version
159
104
async run ( context : AppContext ) : Promise < ApplicationInstance > {
160
105
const depsResolver = context . getAspect < DependencyResolverMain > ( DependencyResolverAspect . id ) ;
@@ -171,32 +116,23 @@ export class AngularApp implements Application {
171
116
const bitCmps = await workspace . getMany ( workspaceCmpsIDs ) ;
172
117
const tempFolder = this . getTempFolder ( workspace ) ;
173
118
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
+ } ) ;
200
136
201
137
return {
202
138
appName : this . name ,
@@ -221,11 +157,12 @@ export class AngularApp implements Application {
221
157
}
222
158
const tempFolder = this . getTempFolder ( ) ;
223
159
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 ) ;
225
161
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 ( {
229
166
angularOptions : {
230
167
...appOptions ,
231
168
tsConfig : tsconfigPath
@@ -240,7 +177,11 @@ export class AngularApp implements Application {
240
177
'process.env' : envVars
241
178
}
242
179
} ) ;
243
- console . log ( 'build done' ) ;
180
+ for ( const result of results ) {
181
+ if ( result . error ) {
182
+ errors . push ( new Error ( result . error ) ) ;
183
+ }
184
+ }
244
185
} else {
245
186
let bundler : Bundler ;
246
187
if ( this . options . bundler ) {
@@ -252,9 +193,15 @@ export class AngularApp implements Application {
252
193
253
194
bundler = await preview . getBundler ( bundlerContext ) ( envContext ) ;
254
195
}
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
+ }
256
202
}
257
203
return {
204
+ errors,
258
205
artifacts : [ {
259
206
name : this . name ,
260
207
globPatterns : [ outputPath ] ,
0 commit comments