66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { dirname , join , normalize , strings } from '@angular-devkit/core' ;
9+ import { join , normalize , strings } from '@angular-devkit/core' ;
1010import {
1111 Rule ,
1212 SchematicsException ,
13- Tree ,
1413 apply ,
1514 applyTemplates ,
1615 chain ,
@@ -28,16 +27,9 @@ import { targetBuildNotFoundError } from '@schematics/angular/utility/project-ta
2827import { getMainFilePath } from '@schematics/angular/utility/standalone/util' ;
2928import { getWorkspace } from '@schematics/angular/utility/workspace' ;
3029import { Builders } from '@schematics/angular/utility/workspace-models' ;
31- import * as ts from 'typescript' ;
3230
3331import { latestVersions } from '../utility/latest-versions' ;
34- import {
35- addInitialNavigation ,
36- findImport ,
37- getImportOfIdentifier ,
38- getOutputPath ,
39- getProject ,
40- } from '../utility/utils' ;
32+ import { getOutputPath , getProject } from '../utility/utils' ;
4133
4234import { Schema as AddServerOptions } from './schema' ;
4335
@@ -199,109 +191,6 @@ function updateWebpackBuilderServerTsConfigRule(options: AddServerOptions): Rule
199191 } ;
200192}
201193
202- function routingInitialNavigationRule ( options : ServerOptions ) : Rule {
203- return async ( host ) => {
204- const project = await getProject ( host , options . project ) ;
205- const serverTarget = project . targets . get ( 'server' ) ;
206- if ( ! serverTarget || ! serverTarget . options ) {
207- return ;
208- }
209-
210- const tsConfigPath = serverTarget . options . tsConfig ;
211- if ( ! tsConfigPath || typeof tsConfigPath !== 'string' || ! host . exists ( tsConfigPath ) ) {
212- // No tsconfig path
213- return ;
214- }
215-
216- const parseConfigHost : ts . ParseConfigHost = {
217- useCaseSensitiveFileNames : ts . sys . useCaseSensitiveFileNames ,
218- readDirectory : ts . sys . readDirectory ,
219- fileExists : function ( fileName : string ) : boolean {
220- return host . exists ( fileName ) ;
221- } ,
222- readFile : function ( fileName : string ) : string {
223- return host . readText ( fileName ) ;
224- } ,
225- } ;
226- const { config } = ts . readConfigFile ( tsConfigPath , parseConfigHost . readFile ) ;
227- const parsed = ts . parseJsonConfigFileContent (
228- config ,
229- parseConfigHost ,
230- dirname ( normalize ( tsConfigPath ) ) ,
231- ) ;
232- const tsHost = ts . createCompilerHost ( parsed . options , true ) ;
233- // Strip BOM as otherwise TSC methods (Ex: getWidth) will return an offset,
234- // which breaks the CLI UpdateRecorder.
235- // See: https://github.com/angular/angular/pull/30719
236- tsHost . readFile = function ( fileName : string ) : string {
237- return host . readText ( fileName ) . replace ( / ^ \uFEFF / , '' ) ;
238- } ;
239- tsHost . directoryExists = function ( directoryName : string ) : boolean {
240- // When the path is file getDir will throw.
241- try {
242- const dir = host . getDir ( directoryName ) ;
243-
244- return ! ! ( dir . subdirs . length || dir . subfiles . length ) ;
245- } catch {
246- return false ;
247- }
248- } ;
249- tsHost . fileExists = function ( fileName : string ) : boolean {
250- return host . exists ( fileName ) ;
251- } ;
252- tsHost . realpath = function ( path : string ) : string {
253- return path ;
254- } ;
255- tsHost . getCurrentDirectory = function ( ) {
256- return host . root . path ;
257- } ;
258-
259- const program = ts . createProgram ( parsed . fileNames , parsed . options , tsHost ) ;
260- const typeChecker = program . getTypeChecker ( ) ;
261- const sourceFiles = program
262- . getSourceFiles ( )
263- . filter ( ( f ) => ! f . isDeclarationFile && ! program . isSourceFileFromExternalLibrary ( f ) ) ;
264- const printer = ts . createPrinter ( ) ;
265- const routerModule = 'RouterModule' ;
266- const routerSource = '@angular/router' ;
267-
268- sourceFiles . forEach ( ( sourceFile ) => {
269- const routerImport = findImport ( sourceFile , routerSource , routerModule ) ;
270- if ( ! routerImport ) {
271- return ;
272- }
273-
274- ts . forEachChild ( sourceFile , function visitNode ( node : ts . Node ) {
275- if (
276- ts . isCallExpression ( node ) &&
277- ts . isPropertyAccessExpression ( node . expression ) &&
278- ts . isIdentifier ( node . expression . expression ) &&
279- node . expression . name . text === 'forRoot'
280- ) {
281- const imp = getImportOfIdentifier ( typeChecker , node . expression . expression ) ;
282-
283- if ( imp && imp . name === routerModule && imp . importModule === routerSource ) {
284- const print = printer . printNode (
285- ts . EmitHint . Unspecified ,
286- addInitialNavigation ( node ) ,
287- sourceFile ,
288- ) ;
289-
290- const recorder = host . beginUpdate ( sourceFile . fileName ) ;
291- recorder . remove ( node . getStart ( ) , node . getWidth ( ) ) ;
292- recorder . insertRight ( node . getStart ( ) , print ) ;
293- host . commitUpdate ( recorder ) ;
294-
295- return ;
296- }
297- }
298-
299- ts . forEachChild ( node , visitNode ) ;
300- } ) ;
301- } ) ;
302- } ;
303- }
304-
305194function addDependencies ( ) : Rule {
306195 return chain ( [
307196 addDependency ( 'express' , latestVersions [ 'express' ] , {
@@ -369,7 +258,6 @@ export default function (options: AddServerOptions): Rule {
369258 updateWebpackBuilderServerTsConfigRule ( options ) ,
370259 updateWebpackBuilderWorkspaceConfigRule ( options ) ,
371260 ] ) ,
372- isStandalone ? noop ( ) : routingInitialNavigationRule ( options ) ,
373261 addServerFile ( options , isStandalone ) ,
374262 addDependencies ( ) ,
375263 ] ) ;
0 commit comments