|
| 1 | +import { ng, npm } from '../../utils/process'; |
| 2 | +import { expectFileToMatch, writeFile } from '../../utils/fs'; |
| 3 | +import { getGlobalVariable } from '../../utils/env'; |
| 4 | +import { expectToFail } from '../../utils/utils'; |
| 5 | +import { updateJsonFile } from '../../utils/project'; |
| 6 | +import { readNgVersion } from '../../utils/version'; |
| 7 | +import { stripIndent } from 'common-tags'; |
| 8 | + |
| 9 | + |
| 10 | +export default function () { |
| 11 | + // Skip this in ejected tests. |
| 12 | + if (getGlobalVariable('argv').eject) { |
| 13 | + return Promise.resolve(); |
| 14 | + } |
| 15 | + |
| 16 | + let platformServerVersion = readNgVersion(); |
| 17 | + |
| 18 | + if (getGlobalVariable('argv').nightly) { |
| 19 | + platformServerVersion = 'github:angular/platform-server-builds'; |
| 20 | + } |
| 21 | + |
| 22 | + return Promise.resolve() |
| 23 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 24 | + const app = configJson['apps'][0]; |
| 25 | + app['appShell'] = { |
| 26 | + app: '1', |
| 27 | + route: 'shell' |
| 28 | + }; |
| 29 | + configJson['apps'].push({ |
| 30 | + platform: 'server', |
| 31 | + root: 'src', |
| 32 | + outDir: 'dist-server', |
| 33 | + assets: [ |
| 34 | + 'assets', |
| 35 | + 'favicon.ico' |
| 36 | + ], |
| 37 | + index: 'index.html', |
| 38 | + main: 'main.server.ts', |
| 39 | + test: 'test.ts', |
| 40 | + tsconfig: 'tsconfig.server.json', |
| 41 | + testTsconfig: 'tsconfig.spec.json', |
| 42 | + prefix: 'app', |
| 43 | + styles: [ |
| 44 | + 'styles.css' |
| 45 | + ], |
| 46 | + scripts: [], |
| 47 | + environmentSource: 'environments/environment.ts', |
| 48 | + environments: { |
| 49 | + dev: 'environments/environment.ts', |
| 50 | + prod: 'environments/environment.prod.ts' |
| 51 | + } |
| 52 | + }); |
| 53 | + })) |
| 54 | + .then(() => writeFile('src/app/app.module.ts', stripIndent` |
| 55 | + import { BrowserModule } from '@angular/platform-browser'; |
| 56 | + import { NgModule } from '@angular/core'; |
| 57 | + import { RouterModule } from '@angular/router'; |
| 58 | +
|
| 59 | + import { AppComponent } from './app.component'; |
| 60 | +
|
| 61 | + @NgModule({ |
| 62 | + imports: [ |
| 63 | + BrowserModule.withServerTransition({ appId: 'appshell-play' }), |
| 64 | + RouterModule |
| 65 | + ], |
| 66 | + declarations: [AppComponent], |
| 67 | + bootstrap: [AppComponent] |
| 68 | + }) |
| 69 | + export class AppModule { } |
| 70 | + `)) |
| 71 | + .then(() => writeFile('src/app/app.component.html', stripIndent` |
| 72 | + Hello World |
| 73 | + <router-outlet></router-outlet> |
| 74 | + `)) |
| 75 | + .then(() => writeFile('src/tsconfig.server.json', stripIndent` |
| 76 | + { |
| 77 | + "extends": "../tsconfig.json", |
| 78 | + "compilerOptions": { |
| 79 | + "outDir": "../out-tsc/app", |
| 80 | + "baseUrl": "./", |
| 81 | + "module": "commonjs", |
| 82 | + "types": [] |
| 83 | + }, |
| 84 | + "exclude": [ |
| 85 | + "test.ts", |
| 86 | + "**/*.spec.ts" |
| 87 | + ], |
| 88 | + "angularCompilerOptions": { |
| 89 | + "entryModule": "app/app.server.module#AppServerModule" |
| 90 | + } |
| 91 | + } |
| 92 | + `)) |
| 93 | + .then(() => writeFile('src/main.server.ts', stripIndent` |
| 94 | + export {AppServerModule} from './app/app.server.module'; |
| 95 | + `)) |
| 96 | + .then(() => writeFile('src/app/app.server.module.ts', stripIndent` |
| 97 | + import {NgModule} from '@angular/core'; |
| 98 | + import {ServerModule} from '@angular/platform-server'; |
| 99 | + import { Routes, RouterModule } from '@angular/router'; |
| 100 | +
|
| 101 | + import { AppModule } from './app.module'; |
| 102 | + import { AppComponent } from './app.component'; |
| 103 | + import { ShellComponent } from './shell.component'; |
| 104 | +
|
| 105 | + const routes: Routes = [ |
| 106 | + { path: 'shell', component: ShellComponent } |
| 107 | + ]; |
| 108 | +
|
| 109 | + @NgModule({ |
| 110 | + imports: [ |
| 111 | + // The AppServerModule should import your AppModule followed |
| 112 | + // by the ServerModule from @angular/platform-server. |
| 113 | + AppModule, |
| 114 | + ServerModule, |
| 115 | + RouterModule.forRoot(routes), |
| 116 | + ], |
| 117 | + // Since the bootstrapped component is not inherited from your |
| 118 | + // imported AppModule, it needs to be repeated here. |
| 119 | + bootstrap: [AppComponent], |
| 120 | + declarations: [ShellComponent], |
| 121 | + }) |
| 122 | + export class AppServerModule {} |
| 123 | + `)) |
| 124 | + .then(() => writeFile('src/app/shell.component.ts', stripIndent` |
| 125 | + import { Component } from '@angular/core'; |
| 126 | + @Component({ |
| 127 | + selector: 'app-shell', |
| 128 | + template: '<p>shell Works!</p>', |
| 129 | + styles: [] |
| 130 | + }) |
| 131 | + export class ShellComponent {} |
| 132 | + `)) |
| 133 | + .then(() => updateJsonFile('package.json', packageJson => { |
| 134 | + const dependencies = packageJson['dependencies']; |
| 135 | + dependencies['@angular/platform-server'] = platformServerVersion; |
| 136 | + }) |
| 137 | + .then(() => npm('install'))) |
| 138 | + .then(() => ng('build', '--prod')) |
| 139 | + .then(() => expectFileToMatch('dist/index.html', /shell Works!/)) |
| 140 | + .then(() => ng('build', '--prod', '--skip-app-shell')) |
| 141 | + .then(() => expectToFail(() => expectFileToMatch('dist/index.html', /shell Works!/))); |
| 142 | +} |
0 commit comments