Skip to content

fix(@schematics/angular): use sourceRoot instead of src in universal schematic #19732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"src/<%= stripTsExtension(main) %>.ts"
],
"angularCompilerOptions": {
"entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
"entryModule": "./<%= sourceRoot ? sourceRoot + '/' : ''%><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
}
}
23 changes: 13 additions & 10 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {
Path,
basename,
dirname,
join,
normalize,
strings,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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'), [
Expand All @@ -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),
]);
Expand All @@ -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),
]);
};
}