Skip to content

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

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

Merged
merged 1 commit into from
Jun 29, 2022
Merged
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 @@ -8,8 +8,5 @@
},
"files": [
"main.server.ts"
],
"angularCompilerOptions": {
"entryModule": "app/app.server.module#AppServerModule"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
},
"files": [
"src/<%= stripTsExtension(main) %>.ts"
],
"angularCompilerOptions": {
"entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
}
]
}
37 changes: 15 additions & 22 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -77,18 +77,15 @@ 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',
builder: Builders.Server,
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) : {}),
},
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -252,35 +249,31 @@ 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());
}

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),
]);
Expand Down
6 changes: 0 additions & 6 deletions packages/schematics/angular/universal/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/angular/universal/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down