Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
fix(@schematics/angular): generate server tsconfig to correct locatio…
Browse files Browse the repository at this point in the history
…n for universal schematic
  • Loading branch information
FrozenPandaz authored and Brocco committed May 16, 2018
1 parent c2a1bce commit eeb615f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"module": "commonjs"
},
"angularCompilerOptions": {
"entryModule": "<%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
"entryModule": "<%= rootInSrc ? '' : 'src/' %><%= appDir %>/<%= stripTsExtension(rootModuleFileName) %>#<%= rootModuleClassName %>"
}
}
25 changes: 20 additions & 5 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
*/
import {
JsonObject,
Path,
basename,
experimental,
join,
normalize,
parseJson,
strings,
Expand Down Expand Up @@ -67,7 +69,7 @@ function getClientArchitect(
return clientArchitect;
}

function updateConfigFile(options: UniversalOptions): Rule {
function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): Rule {
return (host: Tree) => {
const workspace = getWorkspace(host);
if (!workspace.projects[options.clientProject]) {
Expand All @@ -82,7 +84,7 @@ function updateConfigFile(options: UniversalOptions): Rule {
const builderOptions: JsonObject = {
outputPath: `dist/${options.clientProject}-server`,
main: `${clientProject.root}src/main.server.ts`,
tsConfig: `${clientProject.root}src/tsconfig.server.json`,
tsConfig: join(tsConfigDirectory, `${options.tsconfigFileName}.json`),
};
const serverTarget: JsonObject = {
builder: '@angular-devkit/build-angular:server',
Expand Down Expand Up @@ -214,26 +216,39 @@ export default function (options: UniversalOptions): Rule {
const clientArchitect = getClientArchitect(host, options);
const outDir = getTsConfigOutDir(host, clientArchitect);
const tsConfigExtends = basename(clientArchitect.build.options.tsConfig);
const rootInSrc = clientProject.root === '';
const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : '');

if (!options.skipInstall) {
context.addTask(new NodePackageInstallTask());
}

const templateSource = apply(url('./files'), [
const templateSource = apply(url('./files/src'), [
template({
...strings,
...options as object,
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); },
}),
move(join(normalize(clientProject.root), 'src')),
]);

const rootSource = apply(url('./files/root'), [
template({
...strings,
...options as object,
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); },
outDir,
tsConfigExtends,
rootInSrc,
}),
move(clientProject.root),
move(tsConfigDirectory),
]);

return chain([
mergeWith(templateSource),
mergeWith(rootSource),
addDependencies(),
updateConfigFile(options),
updateConfigFile(options, tsConfigDirectory),
wrapBootstrapCall(options),
addServerTransition(options),
])(host, context);
Expand Down
47 changes: 43 additions & 4 deletions packages/schematics/angular/universal/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('Universal Schematic', () => {
const defaultOptions: UniversalOptions = {
clientProject: 'bar',
};
const workspaceUniversalOptions: UniversalOptions = {
clientProject: 'workspace',
};

const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
Expand All @@ -36,10 +39,22 @@ describe('Universal Schematic', () => {
skipPackageJson: false,
};

const initialWorkspaceAppOptions: ApplicationOptions = {
name: 'workspace',
projectRoot: '',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: 'css',
skipTests: false,
skipPackageJson: false,
};

let appTree: UnitTestTree;

beforeEach(() => {
appTree = schematicRunner.runSchematic('workspace', workspaceOptions);
appTree = schematicRunner.runSchematic('application', initialWorkspaceAppOptions, appTree);
appTree = schematicRunner.runSchematic('application', appOptions, appTree);
});

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

it('should create a tsconfig file', () => {
it('should create a tsconfig file for the workspace project', () => {
const tree = schematicRunner.runSchematic('universal', workspaceUniversalOptions, appTree);
const filePath = '/src/tsconfig.server.json';
expect(tree.exists(filePath)).toEqual(true);
const contents = tree.readContent(filePath);
expect(JSON.parse(contents)).toEqual({
extends: './tsconfig.app.json',
compilerOptions: {
outDir: '../out-tsc/app-server',
baseUrl: '.',
module: 'commonjs',
},
angularCompilerOptions: {
entryModule: 'app/app.server.module#AppServerModule',
},
});
const angularConfig = JSON.parse(tree.readContent('angular.json'));
expect(angularConfig.projects.workspace.architect.server.options.tsConfig)
.toEqual('src/tsconfig.server.json');
});

it('should create a tsconfig file for a generated application', () => {
const tree = schematicRunner.runSchematic('universal', defaultOptions, appTree);
const filePath = '/projects/bar/src/tsconfig.server.json';
const filePath = '/projects/bar/tsconfig.server.json';
expect(tree.exists(filePath)).toEqual(true);
const contents = tree.readContent(filePath);
expect(JSON.parse(contents)).toEqual({
Expand All @@ -70,9 +106,12 @@ describe('Universal Schematic', () => {
module: 'commonjs',
},
angularCompilerOptions: {
entryModule: 'app/app.server.module#AppServerModule',
entryModule: 'src/app/app.server.module#AppServerModule',
},
});
const angularConfig = JSON.parse(tree.readContent('angular.json'));
expect(angularConfig.projects.bar.architect.server.options.tsConfig)
.toEqual('projects/bar/tsconfig.server.json');
});

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

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

0 comments on commit eeb615f

Please sign in to comment.