Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit c77c7b0

Browse files
FrozenPandazhansl
authored andcommitted
fix(@schematics/angular): generate server tsconfig to correct location for universal schematic
1 parent 8a72879 commit c77c7b0

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

packages/schematics/angular/universal/files/src/__tsconfigFileName__.json renamed to packages/schematics/angular/universal/files/root/__tsconfigFileName__.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"module": "commonjs"
77
},
88
"angularCompilerOptions": {
9-
"entryModule": "<%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
9+
"entryModule": "<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
1010
}
1111
}

packages/schematics/angular/universal/index.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
*/
88
import {
99
JsonObject,
10+
Path,
1011
basename,
1112
experimental,
13+
join,
1214
normalize,
1315
parseJson,
1416
strings,
@@ -67,7 +69,7 @@ function getClientArchitect(
6769
return clientArchitect;
6870
}
6971

70-
function updateConfigFile(options: UniversalOptions): Rule {
72+
function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): Rule {
7173
return (host: Tree) => {
7274
const workspace = getWorkspace(host);
7375
if (!workspace.projects[options.clientProject]) {
@@ -82,7 +84,7 @@ function updateConfigFile(options: UniversalOptions): Rule {
8284
const builderOptions: JsonObject = {
8385
outputPath: `dist/${options.clientProject}-server`,
8486
main: `${clientProject.root}src/main.server.ts`,
85-
tsConfig: `${clientProject.root}src/tsconfig.server.json`,
87+
tsConfig: join(tsConfigDirectory, `${options.tsconfigFileName}.json`),
8688
};
8789
const serverTarget: JsonObject = {
8890
builder: '@angular-devkit/build-angular:server',
@@ -214,26 +216,39 @@ export default function (options: UniversalOptions): Rule {
214216
const clientArchitect = getClientArchitect(host, options);
215217
const outDir = getTsConfigOutDir(host, clientArchitect);
216218
const tsConfigExtends = basename(clientArchitect.build.options.tsConfig);
219+
const rootInSrc = clientProject.root === '';
220+
const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : '');
217221

218222
if (!options.skipInstall) {
219223
context.addTask(new NodePackageInstallTask());
220224
}
221225

222-
const templateSource = apply(url('./files'), [
226+
const templateSource = apply(url('./files/src'), [
227+
template({
228+
...strings,
229+
...options as object,
230+
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); },
231+
}),
232+
move(join(normalize(clientProject.root), 'src')),
233+
]);
234+
235+
const rootSource = apply(url('./files/root'), [
223236
template({
224237
...strings,
225238
...options as object,
226239
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); },
227240
outDir,
228241
tsConfigExtends,
242+
rootInSrc,
229243
}),
230-
move(clientProject.root),
244+
move(tsConfigDirectory),
231245
]);
232246

233247
return chain([
234248
mergeWith(templateSource),
249+
mergeWith(rootSource),
235250
addDependencies(),
236-
updateConfigFile(options),
251+
updateConfigFile(options, tsConfigDirectory),
237252
wrapBootstrapCall(options),
238253
addServerTransition(options),
239254
])(host, context);

packages/schematics/angular/universal/index_spec.ts

+43-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe('Universal Schematic', () => {
1919
const defaultOptions: UniversalOptions = {
2020
clientProject: 'bar',
2121
};
22+
const workspaceUniversalOptions: UniversalOptions = {
23+
clientProject: 'workspace',
24+
};
2225

2326
const workspaceOptions: WorkspaceOptions = {
2427
name: 'workspace',
@@ -36,10 +39,22 @@ describe('Universal Schematic', () => {
3639
skipPackageJson: false,
3740
};
3841

42+
const initialWorkspaceAppOptions: ApplicationOptions = {
43+
name: 'workspace',
44+
projectRoot: '',
45+
inlineStyle: false,
46+
inlineTemplate: false,
47+
routing: false,
48+
style: 'css',
49+
skipTests: false,
50+
skipPackageJson: false,
51+
};
52+
3953
let appTree: UnitTestTree;
4054

4155
beforeEach(() => {
4256
appTree = schematicRunner.runSchematic('workspace', workspaceOptions);
57+
appTree = schematicRunner.runSchematic('application', initialWorkspaceAppOptions, appTree);
4358
appTree = schematicRunner.runSchematic('application', appOptions, appTree);
4459
});
4560

@@ -57,9 +72,30 @@ describe('Universal Schematic', () => {
5772
expect(contents).toMatch(/export { AppServerModule } from '\.\/app\/app\.server\.module'/);
5873
});
5974

60-
it('should create a tsconfig file', () => {
75+
it('should create a tsconfig file for the workspace project', () => {
76+
const tree = schematicRunner.runSchematic('universal', workspaceUniversalOptions, appTree);
77+
const filePath = '/src/tsconfig.server.json';
78+
expect(tree.exists(filePath)).toEqual(true);
79+
const contents = tree.readContent(filePath);
80+
expect(JSON.parse(contents)).toEqual({
81+
extends: './tsconfig.app.json',
82+
compilerOptions: {
83+
outDir: '../out-tsc/app-server',
84+
baseUrl: '.',
85+
module: 'commonjs',
86+
},
87+
angularCompilerOptions: {
88+
entryModule: 'app/app.server.module#AppServerModule',
89+
},
90+
});
91+
const angularConfig = JSON.parse(tree.readContent('angular.json'));
92+
expect(angularConfig.projects.workspace.architect.server.options.tsConfig)
93+
.toEqual('src/tsconfig.server.json');
94+
});
95+
96+
it('should create a tsconfig file for a generated application', () => {
6197
const tree = schematicRunner.runSchematic('universal', defaultOptions, appTree);
62-
const filePath = '/projects/bar/src/tsconfig.server.json';
98+
const filePath = '/projects/bar/tsconfig.server.json';
6399
expect(tree.exists(filePath)).toEqual(true);
64100
const contents = tree.readContent(filePath);
65101
expect(JSON.parse(contents)).toEqual({
@@ -70,9 +106,12 @@ describe('Universal Schematic', () => {
70106
module: 'commonjs',
71107
},
72108
angularCompilerOptions: {
73-
entryModule: 'app/app.server.module#AppServerModule',
109+
entryModule: 'src/app/app.server.module#AppServerModule',
74110
},
75111
});
112+
const angularConfig = JSON.parse(tree.readContent('angular.json'));
113+
expect(angularConfig.projects.bar.architect.server.options.tsConfig)
114+
.toEqual('projects/bar/tsconfig.server.json');
76115
});
77116

78117
it('should add dependency: @angular/platform-server', () => {
@@ -93,7 +132,7 @@ describe('Universal Schematic', () => {
93132
const opts = arch.server.options;
94133
expect(opts.outputPath).toEqual('dist/bar-server');
95134
expect(opts.main).toEqual('projects/bar/src/main.server.ts');
96-
expect(opts.tsConfig).toEqual('projects/bar/src/tsconfig.server.json');
135+
expect(opts.tsConfig).toEqual('projects/bar/tsconfig.server.json');
97136
});
98137

99138
it('should add a server transition to BrowerModule import', () => {

0 commit comments

Comments
 (0)