8
8
9
9
import { DirEntry , Rule , chain } from '@angular-devkit/schematics' ;
10
10
import { addDependency } from '../../utility' ;
11
- import { removePackageJsonDependency } from '../../utility/dependencies' ;
11
+ import { getPackageJsonDependency , removePackageJsonDependency } from '../../utility/dependencies' ;
12
12
import { latestVersions } from '../../utility/latest-versions' ;
13
13
import { allTargetOptions , getWorkspace } from '../../utility/workspace' ;
14
14
import { Builders , ProjectType } from '../../utility/workspace-models' ;
@@ -36,6 +36,8 @@ function* visit(directory: DirEntry): IterableIterator<[fileName: string, conten
36
36
}
37
37
}
38
38
39
+ const UNIVERSAL_PACKAGES = [ '@nguniversal/common' , '@nguniversal/express-engine' ] ;
40
+
39
41
/**
40
42
* Regexp to match Universal packages.
41
43
* @nguniversal /common/engine
@@ -45,53 +47,61 @@ function* visit(directory: DirEntry): IterableIterator<[fileName: string, conten
45
47
const NGUNIVERSAL_PACKAGE_REGEXP = / @ n g u n i v e r s a l \/ ( c o m m o n ( \/ e n g i n e ) ? | e x p r e s s - e n g i n e ) / g;
46
48
47
49
export default function ( ) : Rule {
48
- return chain ( [
49
- async ( tree ) => {
50
- // Replace server file.
51
- const workspace = await getWorkspace ( tree ) ;
52
- for ( const [ , project ] of workspace . projects ) {
53
- if ( project . extensions . projectType !== ProjectType . Application ) {
54
- continue ;
55
- }
50
+ return async ( tree ) => {
51
+ const hasUniversalDeps = UNIVERSAL_PACKAGES . some ( ( d ) => getPackageJsonDependency ( tree , d ) ) ;
52
+ if ( ! hasUniversalDeps ) {
53
+ return ;
54
+ }
56
55
57
- const serverMainFiles = new Map < string /** Main Path */ , string /** Output Path */ > ( ) ;
58
- for ( const [ , target ] of project . targets ) {
59
- if ( target . builder !== Builders . Server ) {
56
+ return chain ( [
57
+ async ( tree ) => {
58
+ // Replace server file.
59
+ const workspace = await getWorkspace ( tree ) ;
60
+ for ( const [ , project ] of workspace . projects ) {
61
+ if ( project . extensions . projectType !== ProjectType . Application ) {
60
62
continue ;
61
63
}
62
64
63
- const outputPath = project . targets . get ( 'build' ) ?. options ?. outputPath ;
65
+ const serverMainFiles = new Map < string /** Main Path */ , string /** Output Path */ > ( ) ;
66
+ for ( const [ , target ] of project . targets ) {
67
+ if ( target . builder !== Builders . Server ) {
68
+ continue ;
69
+ }
70
+
71
+ const outputPath = project . targets . get ( 'build' ) ?. options ?. outputPath ;
64
72
65
- for ( const [ , { main } ] of allTargetOptions ( target , false ) ) {
66
- if (
67
- typeof main === 'string' &&
68
- typeof outputPath === 'string' &&
69
- tree . readText ( main ) . includes ( 'ngExpressEngine' )
70
- ) {
71
- serverMainFiles . set ( main , outputPath ) ;
73
+ for ( const [ , { main } ] of allTargetOptions ( target , false ) ) {
74
+ if (
75
+ typeof main === 'string' &&
76
+ typeof outputPath === 'string' &&
77
+ tree . readText ( main ) . includes ( 'ngExpressEngine' )
78
+ ) {
79
+ serverMainFiles . set ( main , outputPath ) ;
80
+ }
72
81
}
73
82
}
74
- }
75
83
76
- // Replace server file
77
- for ( const [ path , outputPath ] of serverMainFiles . entries ( ) ) {
78
- tree . rename ( path , path + '.bak' ) ;
79
- tree . create ( path , getServerFileContents ( outputPath ) ) ;
84
+ // Replace server file
85
+ for ( const [ path , outputPath ] of serverMainFiles . entries ( ) ) {
86
+ tree . rename ( path , path + '.bak' ) ;
87
+ tree . create ( path , getServerFileContents ( outputPath ) ) ;
88
+ }
80
89
}
81
- }
82
90
83
- // Replace all import specifiers in all files.
84
- for ( const file of visit ( tree . root ) ) {
85
- const [ path , content ] = file ;
86
- tree . overwrite ( path , content . replaceAll ( NGUNIVERSAL_PACKAGE_REGEXP , '@angular/ssr' ) ) ;
87
- }
91
+ // Replace all import specifiers in all files.
92
+ for ( const file of visit ( tree . root ) ) {
93
+ const [ path , content ] = file ;
94
+ tree . overwrite ( path , content . replaceAll ( NGUNIVERSAL_PACKAGE_REGEXP , '@angular/ssr' ) ) ;
95
+ }
88
96
89
- // Remove universal packages from deps.
90
- removePackageJsonDependency ( tree , '@nguniversal/express-engine' ) ;
91
- removePackageJsonDependency ( tree , '@nguniversal/common' ) ;
92
- } ,
93
- addDependency ( '@angular/ssr' , latestVersions . AngularSSR ) ,
94
- ] ) ;
97
+ // Remove universal packages from deps.
98
+ for ( const name of UNIVERSAL_PACKAGES ) {
99
+ removePackageJsonDependency ( tree , name ) ;
100
+ }
101
+ } ,
102
+ addDependency ( '@angular/ssr' , latestVersions . AngularSSR ) ,
103
+ ] ) ;
104
+ } ;
95
105
}
96
106
97
107
function getServerFileContents ( outputPath : string ) : string {
0 commit comments