Skip to content

Commit ab8ab30

Browse files
alan-agius4clydin
authored andcommitted
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
1 parent ecdbe72 commit ab8ab30

File tree

5 files changed

+19
-37
lines changed

5 files changed

+19
-37
lines changed

Diff for: packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
},
99
"files": [
1010
"main.server.ts"
11-
],
12-
"angularCompilerOptions": {
13-
"entryModule": "app/app.server.module#AppServerModule"
14-
}
11+
]
1512
}

Diff for: packages/schematics/angular/universal/files/root/tsconfig.server.json.template

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,5 @@
1010
},
1111
"files": [
1212
"src/<%= stripTsExtension(main) %>.ts"
13-
],
14-
"angularCompilerOptions": {
15-
"entryModule": "./<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
16-
}
13+
]
1714
}

Diff for: packages/schematics/angular/universal/index.ts

+15-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { JsonValue, Path, basename, join, normalize } from '@angular-devkit/core';
9+
import { JsonValue, Path, basename, dirname, join, normalize } from '@angular-devkit/core';
1010
import {
1111
Rule,
1212
SchematicContext,
@@ -77,18 +77,15 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
7777
}
7878

7979
const mainPath = options.main as string;
80+
const sourceRoot = clientProject.sourceRoot ?? join(normalize(clientProject.root), 'src');
8081
const serverTsConfig = join(tsConfigDirectory, 'tsconfig.server.json');
8182
clientProject.targets.add({
8283
name: 'server',
8384
builder: Builders.Server,
8485
defaultConfiguration: 'production',
8586
options: {
8687
outputPath: `dist/${options.project}/server`,
87-
main: join(
88-
normalize(clientProject.root),
89-
'src',
90-
mainPath.endsWith('.ts') ? mainPath : mainPath + '.ts',
91-
),
88+
main: join(normalize(sourceRoot), mainPath.endsWith('.ts') ? mainPath : mainPath + '.ts'),
9289
tsConfig: serverTsConfig,
9390
...(buildTarget?.options ? getServerOptions(buildTarget?.options) : {}),
9491
},
@@ -147,12 +144,12 @@ function wrapBootstrapCall(mainFile: string): Rule {
147144
`\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}};\n` +
148145
`
149146
150-
if (document.readyState === 'complete') {
151-
bootstrap();
152-
} else {
153-
document.addEventListener('DOMContentLoaded', bootstrap);
154-
}
155-
`;
147+
if (document.readyState === 'complete') {
148+
bootstrap();
149+
} else {
150+
document.addEventListener('DOMContentLoaded', bootstrap);
151+
}
152+
`;
156153

157154
// in some cases we need to cater for a trailing semicolon such as;
158155
// bootstrap().catch(err => console.log(err));
@@ -252,35 +249,31 @@ export default function (options: UniversalOptions): Rule {
252249
const clientBuildOptions = (clientBuildTarget.options ||
253250
{}) as unknown as BrowserBuilderOptions;
254251

255-
const clientTsConfig = normalize(clientBuildOptions.tsConfig);
256-
const tsConfigExtends = basename(clientTsConfig);
257-
// this is needed because prior to version 8, tsconfig might have been in 'src'
258-
// and we don't want to break the 'ng add @nguniversal/express-engine schematics'
259-
const rootInSrc = clientProject.root === '' && clientTsConfig.includes('src/');
260-
const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : '');
261-
262252
if (!options.skipInstall) {
263253
context.addTask(new NodePackageInstallTask());
264254
}
265255

266256
const templateSource = apply(url('./files/src'), [
267257
applyTemplates({
268258
...strings,
269-
...(options as object),
259+
...options,
270260
stripTsExtension: (s: string) => s.replace(/\.ts$/, ''),
271261
hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'),
272262
}),
273263
move(join(normalize(clientProject.root), 'src')),
274264
]);
275265

266+
const clientTsConfig = normalize(clientBuildOptions.tsConfig);
267+
const tsConfigExtends = basename(clientTsConfig);
268+
const tsConfigDirectory = dirname(clientTsConfig);
269+
276270
const rootSource = apply(url('./files/root'), [
277271
applyTemplates({
278272
...strings,
279-
...(options as object),
273+
...options,
280274
stripTsExtension: (s: string) => s.replace(/\.ts$/, ''),
281275
tsConfigExtends,
282276
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(tsConfigDirectory),
283-
rootInSrc,
284277
}),
285278
move(tsConfigDirectory),
286279
]);

Diff for: packages/schematics/angular/universal/index_spec.ts

-6
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ describe('Universal Schematic', () => {
9797
types: ['node'],
9898
},
9999
files: ['src/main.server.ts'],
100-
angularCompilerOptions: {
101-
entryModule: './src/app/app.server.module#AppServerModule',
102-
},
103100
});
104101
const angularConfig = JSON.parse(tree.readContent('angular.json'));
105102
expect(angularConfig.projects.workspace.architect.server.options.tsConfig).toEqual(
@@ -122,9 +119,6 @@ describe('Universal Schematic', () => {
122119
types: ['node'],
123120
},
124121
files: ['src/main.server.ts'],
125-
angularCompilerOptions: {
126-
entryModule: './src/app/app.server.module#AppServerModule',
127-
},
128122
});
129123
const angularConfig = JSON.parse(tree.readContent('angular.json'));
130124
expect(angularConfig.projects.bar.architect.server.options.tsConfig).toEqual(

Diff for: packages/schematics/angular/universal/schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"type": "string",
3030
"format": "path",
3131
"description": "The name of the application folder.",
32-
"default": "app"
32+
"default": "app",
33+
"x-deprecated": "This option has no effect."
3334
},
3435
"rootModuleFileName": {
3536
"type": "string",

0 commit comments

Comments
 (0)