From 7cb61fb3f02b410d0b80d4ba8558cebc24e95215 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 13 Jan 2021 10:35:45 +0100 Subject: [PATCH] fix(@schematics/angular): use `sourceRoot` instead of `src` in universal schematic Closes #12104 --- .../files/root/tsconfig.server.json.template | 2 +- .../schematics/angular/universal/index.ts | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) 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 4c93c28ff79f..8ce36eee156b 100644 --- a/packages/schematics/angular/universal/files/root/tsconfig.server.json.template +++ b/packages/schematics/angular/universal/files/root/tsconfig.server.json.template @@ -12,6 +12,6 @@ "src/<%= stripTsExtension(main) %>.ts" ], "angularCompilerOptions": { - "entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>" + "entryModule": "./<%= sourceRoot ? sourceRoot + '/' : ''%><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>" } } diff --git a/packages/schematics/angular/universal/index.ts b/packages/schematics/angular/universal/index.ts index 67d6be6f7be8..fc65d8a7bedd 100644 --- a/packages/schematics/angular/universal/index.ts +++ b/packages/schematics/angular/universal/index.ts @@ -8,6 +8,7 @@ import { Path, basename, + dirname, join, normalize, strings, @@ -68,13 +69,14 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R const mainPath = options.main as string; const serverTsConfig = join(tsConfigDirectory, 'tsconfig.server.json'); + const sourceRoot = clientProject.sourceRoot ?? join(normalize(clientProject.root), 'src'); clientProject.targets.add({ name: 'server', builder: Builders.Server, options: { outputPath: `dist/${options.clientProject}/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, }, configurations: { @@ -194,7 +196,7 @@ function addServerTransition( const bootstrapModuleRelativePath = findBootstrapModulePath(host, mainPath); const bootstrapModulePath = normalize( - `/${clientProjectRoot}/src/${bootstrapModuleRelativePath}.ts`); + `/${clientProjectRoot}/${bootstrapModuleRelativePath}.ts`); const browserModuleImport = findBrowserModuleImport(host, bootstrapModulePath); const appId = options.appId; @@ -234,6 +236,10 @@ export default function (options: UniversalOptions): Rule { throw new SchematicsException(`Universal requires a project type of "application".`); } + if (typeof clientProject.sourceRoot !== 'string') { + throw new SchematicsException(`"sourceRoot" option is not defined in "${options.clientProject}" project.`); + } + const clientBuildTarget = clientProject.targets.get('build'); if (!clientBuildTarget) { throw targetBuildNotFoundError(); @@ -244,10 +250,7 @@ export default function (options: UniversalOptions): Rule { 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' : ''); + const tsConfigDirectory = dirname(clientTsConfig); if (!options.skipInstall) { context.addTask(new NodePackageInstallTask()); @@ -260,7 +263,7 @@ export default function (options: UniversalOptions): Rule { stripTsExtension: (s: string) => s.replace(/\.ts$/, ''), hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'), }), - move(join(normalize(clientProject.root), 'src')), + move(clientProject.sourceRoot), ]); const rootSource = apply(url('./files/root'), [ @@ -270,7 +273,7 @@ export default function (options: UniversalOptions): Rule { stripTsExtension: (s: string) => s.replace(/\.ts$/, ''), tsConfigExtends, relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(tsConfigDirectory), - rootInSrc, + sourceRoot: clientProject.sourceRoot ?? '', }), move(tsConfigDirectory), ]); @@ -279,9 +282,9 @@ export default function (options: UniversalOptions): Rule { mergeWith(templateSource), mergeWith(rootSource), addDependencies(), - updateConfigFile(options, tsConfigDirectory), + updateConfigFile(options, normalize(tsConfigDirectory)), wrapBootstrapCall(clientBuildOptions.main), - addServerTransition(options, clientBuildOptions.main, clientProject.root), + addServerTransition(options, clientBuildOptions.main, clientProject.sourceRoot), ]); }; }