88
99import { DirEntry , Rule , chain } from '@angular-devkit/schematics' ;
1010import { addDependency } from '../../utility' ;
11- import { removePackageJsonDependency } from '../../utility/dependencies' ;
11+ import { getPackageJsonDependency , removePackageJsonDependency } from '../../utility/dependencies' ;
1212import { latestVersions } from '../../utility/latest-versions' ;
1313import { allTargetOptions , getWorkspace } from '../../utility/workspace' ;
1414import { Builders , ProjectType } from '../../utility/workspace-models' ;
@@ -36,6 +36,8 @@ function* visit(directory: DirEntry): IterableIterator<[fileName: string, conten
3636 }
3737}
3838
39+ const UNIVERSAL_PACKAGES = [ '@nguniversal/common' , '@nguniversal/express-engine' ] ;
40+
3941/**
4042 * Regexp to match Universal packages.
4143 * @nguniversal /common/engine
@@ -45,53 +47,61 @@ function* visit(directory: DirEntry): IterableIterator<[fileName: string, conten
4547const 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;
4648
4749export 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+ }
5655
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 ) {
6062 continue ;
6163 }
6264
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 ;
6472
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+ }
7281 }
7382 }
74- }
7583
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+ }
8089 }
81- }
8290
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+ }
8896
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+ } ;
95105}
96106
97107function getServerFileContents ( outputPath : string ) : string {
0 commit comments