Skip to content

Commit

Permalink
fix(@schematics/angular): use sourceRoot instead of src in univer…
Browse files Browse the repository at this point in the history
…sal 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 ab8ab30)
  • Loading branch information
alan-agius4 authored and clydin committed Jun 29, 2022
1 parent 88acec1 commit c58c66c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
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

0 comments on commit c58c66c

Please sign in to comment.