From c58c66c0d5c76630453151b65b1a1c3707c82e9f Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 29 Jun 2022 11:23:41 +0000 Subject: [PATCH] fix(@schematics/angular): use `sourceRoot` instead of `src` in universal schematic With this change we remove the usage of hard coded `src` directory and instead infer this from the `sourceRoot` project option. We also remove the `angularCompilerOptions.entryModule` property in the server tsconfig as this is no longer needed with Ivy. Closes #12104 (cherry picked from commit ab8ab30c879f04777b9a444a7f3072682ea161b5) --- .../hello-world-app/src/tsconfig.server.json | 5 +-- .../files/root/tsconfig.server.json.template | 5 +-- .../schematics/angular/universal/index.ts | 37 ++++++++----------- .../angular/universal/index_spec.ts | 6 --- .../schematics/angular/universal/schema.json | 3 +- 5 files changed, 19 insertions(+), 37 deletions(-) diff --git a/packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json b/packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json index 62dc009f4ba5..0b0bc22e90b6 100644 --- a/packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json +++ b/packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json @@ -8,8 +8,5 @@ }, "files": [ "main.server.ts" - ], - "angularCompilerOptions": { - "entryModule": "app/app.server.module#AppServerModule" - } + ] } diff --git a/packages/schematics/angular/universal/files/root/tsconfig.server.json.template b/packages/schematics/angular/universal/files/root/tsconfig.server.json.template index d6fc57740587..f43b41cae4ad 100644 --- a/packages/schematics/angular/universal/files/root/tsconfig.server.json.template +++ b/packages/schematics/angular/universal/files/root/tsconfig.server.json.template @@ -10,8 +10,5 @@ }, "files": [ "src/<%= stripTsExtension(main) %>.ts" - ], - "angularCompilerOptions": { - "entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>" - } + ] } diff --git a/packages/schematics/angular/universal/index.ts b/packages/schematics/angular/universal/index.ts index c660995305bc..2eae32dcd947 100644 --- a/packages/schematics/angular/universal/index.ts +++ b/packages/schematics/angular/universal/index.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import { JsonValue, Path, basename, join, normalize } from '@angular-devkit/core'; +import { JsonValue, Path, basename, dirname, join, normalize } from '@angular-devkit/core'; import { Rule, SchematicContext, @@ -77,6 +77,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R } const mainPath = options.main as string; + const sourceRoot = clientProject.sourceRoot ?? join(normalize(clientProject.root), 'src'); const serverTsConfig = join(tsConfigDirectory, 'tsconfig.server.json'); clientProject.targets.add({ name: 'server', @@ -84,11 +85,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R defaultConfiguration: 'production', options: { outputPath: `dist/${options.project}/server`, - main: join( - normalize(clientProject.root), - 'src', - mainPath.endsWith('.ts') ? mainPath : mainPath + '.ts', - ), + main: join(normalize(sourceRoot), mainPath.endsWith('.ts') ? mainPath : mainPath + '.ts'), tsConfig: serverTsConfig, ...(buildTarget?.options ? getServerOptions(buildTarget?.options) : {}), }, @@ -147,12 +144,12 @@ function wrapBootstrapCall(mainFile: string): Rule { `\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}};\n` + ` -if (document.readyState === 'complete') { - bootstrap(); -} else { - document.addEventListener('DOMContentLoaded', bootstrap); -} -`; + if (document.readyState === 'complete') { + bootstrap(); + } else { + document.addEventListener('DOMContentLoaded', bootstrap); + } + `; // in some cases we need to cater for a trailing semicolon such as; // bootstrap().catch(err => console.log(err)); @@ -252,13 +249,6 @@ export default function (options: UniversalOptions): Rule { const clientBuildOptions = (clientBuildTarget.options || {}) as unknown as BrowserBuilderOptions; - const clientTsConfig = normalize(clientBuildOptions.tsConfig); - const tsConfigExtends = basename(clientTsConfig); - // this is needed because prior to version 8, tsconfig might have been in 'src' - // and we don't want to break the 'ng add @nguniversal/express-engine schematics' - const rootInSrc = clientProject.root === '' && clientTsConfig.includes('src/'); - const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : ''); - if (!options.skipInstall) { context.addTask(new NodePackageInstallTask()); } @@ -266,21 +256,24 @@ export default function (options: UniversalOptions): Rule { const templateSource = apply(url('./files/src'), [ applyTemplates({ ...strings, - ...(options as object), + ...options, stripTsExtension: (s: string) => s.replace(/\.ts$/, ''), hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'), }), move(join(normalize(clientProject.root), 'src')), ]); + const clientTsConfig = normalize(clientBuildOptions.tsConfig); + const tsConfigExtends = basename(clientTsConfig); + const tsConfigDirectory = dirname(clientTsConfig); + const rootSource = apply(url('./files/root'), [ applyTemplates({ ...strings, - ...(options as object), + ...options, stripTsExtension: (s: string) => s.replace(/\.ts$/, ''), tsConfigExtends, relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(tsConfigDirectory), - rootInSrc, }), move(tsConfigDirectory), ]); diff --git a/packages/schematics/angular/universal/index_spec.ts b/packages/schematics/angular/universal/index_spec.ts index 04eb9c08d089..a9e64dc40be3 100644 --- a/packages/schematics/angular/universal/index_spec.ts +++ b/packages/schematics/angular/universal/index_spec.ts @@ -97,9 +97,6 @@ describe('Universal Schematic', () => { types: ['node'], }, files: ['src/main.server.ts'], - angularCompilerOptions: { - entryModule: './src/app/app.server.module#AppServerModule', - }, }); const angularConfig = JSON.parse(tree.readContent('angular.json')); expect(angularConfig.projects.workspace.architect.server.options.tsConfig).toEqual( @@ -122,9 +119,6 @@ describe('Universal Schematic', () => { types: ['node'], }, files: ['src/main.server.ts'], - angularCompilerOptions: { - entryModule: './src/app/app.server.module#AppServerModule', - }, }); const angularConfig = JSON.parse(tree.readContent('angular.json')); expect(angularConfig.projects.bar.architect.server.options.tsConfig).toEqual( diff --git a/packages/schematics/angular/universal/schema.json b/packages/schematics/angular/universal/schema.json index 16ba8cde6a30..4027e90cc296 100644 --- a/packages/schematics/angular/universal/schema.json +++ b/packages/schematics/angular/universal/schema.json @@ -29,7 +29,8 @@ "type": "string", "format": "path", "description": "The name of the application folder.", - "default": "app" + "default": "app", + "x-deprecated": "This option has no effect." }, "rootModuleFileName": { "type": "string",