├── .angular-cli.json ├── .dockerignore ├── .editorconfig ├── .gitignore ├── Dockerfile ├── README.md ├── apps ├── .gitkeep └── web │ ├── e2e │ ├── app.e2e-spec.ts │ └── app.po.ts │ └── src │ ├── app │ ├── app-routing.module.ts │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ └── app.server.module.ts │ ├── assets │ ├── .gitkeep │ └── nx-logo.png │ ├── environments │ ├── environment.prod.ts │ └── environment.ts │ ├── favicon.ico │ ├── index.html │ ├── main.server.ts │ ├── main.ts │ ├── polyfills.ts │ └── styles.scss ├── karma.conf.js ├── libs ├── .gitkeep ├── pages │ ├── index.ts │ └── src │ │ ├── about │ │ ├── about.component.spec.ts │ │ └── about.component.ts │ │ ├── contact │ │ ├── contact.component.spec.ts │ │ └── contact.component.ts │ │ ├── home │ │ ├── home.component.spec.ts │ │ └── home.component.ts │ │ ├── pages-routing.module.ts │ │ ├── pages.module.spec.ts │ │ └── pages.module.ts └── ui │ ├── index.ts │ └── src │ ├── footer │ ├── footer.component.spec.ts │ └── footer.component.ts │ ├── header │ ├── header.component.spec.ts │ └── header.component.ts │ ├── layout │ ├── layout.component.spec.ts │ └── layout.component.ts │ ├── ui.module.spec.ts │ └── ui.module.ts ├── package.json ├── protractor.conf.js ├── server.ts ├── test.js ├── tsconfig.app.json ├── tsconfig.e2e.json ├── tsconfig.json ├── tsconfig.server.json ├── tsconfig.spec.json ├── tslint.json └── webpack.server.config.js /.angular-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "project": { 4 | "name": "angular-nx-ssr" 5 | }, 6 | "apps": [ 7 | { 8 | "name": "web", 9 | "root": "apps/web/src", 10 | "outDir": "dist/apps/web", 11 | "assets": [ 12 | "assets", 13 | "favicon.ico" 14 | ], 15 | "index": "index.html", 16 | "main": "main.ts", 17 | "polyfills": "polyfills.ts", 18 | "test": "../../../test.js", 19 | "tsconfig": "../../../tsconfig.app.json", 20 | "testTsconfig": "../../../tsconfig.spec.json", 21 | "prefix": "app", 22 | "styles": [ 23 | "styles.scss" 24 | ], 25 | "scripts": [], 26 | "environmentSource": "environments/environment.ts", 27 | "environments": { 28 | "dev": "environments/environment.ts", 29 | "prod": "environments/environment.prod.ts" 30 | } 31 | }, 32 | { 33 | "name": "server", 34 | "root": "apps/web/src", 35 | "outDir": "dist/apps/server", 36 | "platform": "server", 37 | "assets": [ 38 | "assets", 39 | "favicon.ico" 40 | ], 41 | "index": "index.html", 42 | "main": "main.server.ts", 43 | "test": "../../../test.js", 44 | "tsconfig": "../../../tsconfig.server.json", 45 | "testTsconfig": "../../../tsconfig.spec.json", 46 | "prefix": "app", 47 | "styles": [ 48 | "styles.scss" 49 | ], 50 | "scripts": [], 51 | "environmentSource": "environments/environment.ts", 52 | "environments": { 53 | "dev": "environments/environment.ts", 54 | "prod": "environments/environment.prod.ts" 55 | } 56 | }, 57 | 58 | { 59 | "name": "pages", 60 | "root": "libs/pages/src", 61 | "test": "../../../test.js", 62 | "appRoot": "" 63 | }, 64 | { 65 | "name": "ui", 66 | "root": "libs/ui/src", 67 | "test": "../../../test.js", 68 | "appRoot": "" 69 | } 70 | ], 71 | "e2e": { 72 | "protractor": { 73 | "config": "./protractor.conf.js" 74 | } 75 | }, 76 | "lint": [ 77 | { 78 | "project": "./tsconfig.app.json" 79 | }, 80 | { 81 | "project": "./tsconfig.spec.json" 82 | }, 83 | { 84 | "project": "./tsconfig.e2e.json" 85 | } 86 | ], 87 | "test": { 88 | "karma": { 89 | "config": "./karma.conf.js" 90 | } 91 | }, 92 | "defaults": { 93 | "styleExt": "scss", 94 | "component": { 95 | "inlineStyle": true, 96 | "inlineTemplate": true 97 | }, 98 | "schematics": { 99 | "collection": "@nrwl/schematics", 100 | "newProject": [ 101 | "app", 102 | "lib" 103 | ], 104 | "postGenerate": "npm run format" 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | testem.log 34 | /typings 35 | 36 | # e2e 37 | /e2e/*.js 38 | /e2e/*.map 39 | 40 | # System Files 41 | .DS_Store 42 | Thumbs.db 43 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:8 2 | 3 | RUN mkdir /app 4 | 5 | WORKDIR /app 6 | 7 | COPY ./ ./ 8 | 9 | ENV NPM_CONFIG_LOGLEVEL warn 10 | 11 | RUN npm i 12 | 13 | RUN npm run build 14 | 15 | EXPOSE 4000 16 | 17 | CMD ["npm", "run", "serve"] 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular + NX + SSR 2 | 3 | > Example of Angular Universal in a @nrwl nx workspace 4 | 5 | ## Features 6 | 7 | - Angular v5 8 | - Angular Universal 9 | - @nrwl/nx workspace 10 | - Bootswatch theme 11 | - Lazy loading 12 | - Docker support 13 | - now.sh support 14 | 15 | -------------------------------------------------------------------------------- /apps/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /apps/web/e2e/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('web App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.text()).toContain('app works!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /apps/web/e2e/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | text() { 9 | return browser.findElement(by.css('body')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /apps/web/src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | import { LayoutComponent } from '../../../../libs/ui/src/layout/layout.component'; 4 | 5 | const routes: Routes = [ 6 | { 7 | path: '', 8 | component: LayoutComponent, 9 | children: [{ path: '', loadChildren: '../../../../libs/pages/src/pages.module#PagesModule' }] 10 | } 11 | ]; 12 | 13 | @NgModule({ 14 | imports: [RouterModule.forRoot(routes)], 15 | exports: [RouterModule] 16 | }) 17 | export class AppRoutingModule {} 18 | -------------------------------------------------------------------------------- /apps/web/src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | 4 | import { AppComponent } from './app.component'; 5 | 6 | describe('AppComponent', () => { 7 | let component: AppComponent; 8 | let fixture: ComponentFixture; 9 | 10 | beforeEach( 11 | async(() => { 12 | TestBed.configureTestingModule({ 13 | imports: [RouterTestingModule], 14 | declarations: [AppComponent] 15 | }).compileComponents(); 16 | }) 17 | ); 18 | 19 | beforeEach(() => { 20 | fixture = TestBed.createComponent(AppComponent); 21 | component = fixture.componentInstance; 22 | fixture.detectChanges(); 23 | }); 24 | 25 | it('should create the component', () => { 26 | expect(component).toBeTruthy(); 27 | }); 28 | 29 | it( 30 | 'should render the router-outlet', 31 | async(() => { 32 | const fixture = TestBed.createComponent(AppComponent); 33 | fixture.detectChanges(); 34 | const compiled = fixture.debugElement.nativeElement; 35 | expect(compiled.querySelector('router-outlet')).toBeTruthy(); 36 | }) 37 | ); 38 | }); 39 | -------------------------------------------------------------------------------- /apps/web/src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | template: `` 6 | }) 7 | export class AppComponent {} 8 | -------------------------------------------------------------------------------- /apps/web/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | 3 | import { AppRoutingModule } from './app-routing.module'; 4 | import { AppComponent } from './app.component'; 5 | import { BrowserModule } from '@angular/platform-browser'; 6 | import { NxModule } from '@nrwl/nx'; 7 | import { UiModule } from '../../../../libs/ui'; 8 | 9 | @NgModule({ 10 | imports: [ 11 | AppRoutingModule, 12 | BrowserModule.withServerTransition({ appId: 'angular-nx-ssr' }), 13 | NxModule.forRoot(), 14 | UiModule 15 | ], 16 | declarations: [AppComponent], 17 | bootstrap: [AppComponent] 18 | }) 19 | export class AppModule {} 20 | -------------------------------------------------------------------------------- /apps/web/src/app/app.server.module.ts: -------------------------------------------------------------------------------- 1 | import {NgModule} from '@angular/core'; 2 | import {ServerModule} from '@angular/platform-server'; 3 | import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader'; 4 | 5 | import {AppModule} from './app.module'; 6 | import {AppComponent} from './app.component'; 7 | 8 | @NgModule({ 9 | imports: [ 10 | // The AppServerModule should import your AppModule followed 11 | // by the ServerModule from @angular/platform-server. 12 | AppModule, 13 | ServerModule, 14 | ModuleMapLoaderModule // <-- *Important* to have lazy-loaded routes work 15 | ], 16 | // Since the bootstrapped component is not inherited from your 17 | // imported AppModule, it needs to be repeated here. 18 | bootstrap: [AppComponent], 19 | }) 20 | export class AppServerModule {} 21 | -------------------------------------------------------------------------------- /apps/web/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeman/angular-nx-ssr/0f354c1b853025679a8651962f5fe16d7dba0421/apps/web/src/assets/.gitkeep -------------------------------------------------------------------------------- /apps/web/src/assets/nx-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeman/angular-nx-ssr/0f354c1b853025679a8651962f5fe16d7dba0421/apps/web/src/assets/nx-logo.png -------------------------------------------------------------------------------- /apps/web/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /apps/web/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `.angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /apps/web/src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeman/angular-nx-ssr/0f354c1b853025679a8651962f5fe16d7dba0421/apps/web/src/favicon.ico -------------------------------------------------------------------------------- /apps/web/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angular + NX + SSR 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /apps/web/src/main.server.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | enableProdMode() 3 | export { AppServerModule } from './app/app.server.module'; 4 | -------------------------------------------------------------------------------- /apps/web/src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic() 12 | .bootstrapModule(AppModule) 13 | .catch(err => console.log(err)); 14 | -------------------------------------------------------------------------------- /apps/web/src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 38 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 39 | 40 | /** IE10 and IE11 requires the following for the Reflect API. */ 41 | // import 'core-js/es6/reflect'; 42 | 43 | /** Evergreen browsers require these. **/ 44 | // Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove. 45 | import 'core-js/es7/reflect'; 46 | 47 | /** 48 | * Required to support Web Animations `@angular/platform-browser/animations`. 49 | * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation 50 | **/ 51 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 52 | 53 | /*************************************************************************************************** 54 | * Zone JS is required by Angular itself. 55 | */ 56 | import 'zone.js/dist/zone'; // Included with Angular CLI. 57 | 58 | /*************************************************************************************************** 59 | * APPLICATION IMPORTS 60 | */ 61 | 62 | /** 63 | * Date, currency, decimal and percent pipes. 64 | * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 65 | */ 66 | // import 'intl'; // Run `npm install --save intl`. 67 | /** 68 | * Need to import at least one locale-data with intl. 69 | */ 70 | // import 'intl/locale-data/jsonp/en'; 71 | -------------------------------------------------------------------------------- /apps/web/src/styles.scss: -------------------------------------------------------------------------------- 1 | @import "~bootswatch/dist/lux/bootstrap.css"; 2 | @import "~font-awesome/css/font-awesome.css"; 3 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular/cli'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular/cli/plugins/karma') 14 | ], 15 | client:{ 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | reports: [ 'html', 'lcovonly' ], 20 | fixWebpackSourcePaths: true 21 | }, 22 | angularCli: { 23 | environment: 'dev' 24 | }, 25 | reporters: ['progress', 'kjhtml'], 26 | port: 9876, 27 | colors: true, 28 | logLevel: config.LOG_INFO, 29 | autoWatch: true, 30 | browsers: ['Chrome'], 31 | singleRun: false 32 | }); 33 | }; 34 | -------------------------------------------------------------------------------- /libs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beeman/angular-nx-ssr/0f354c1b853025679a8651962f5fe16d7dba0421/libs/.gitkeep -------------------------------------------------------------------------------- /libs/pages/index.ts: -------------------------------------------------------------------------------- 1 | export { PagesModule } from './src/pages.module'; 2 | -------------------------------------------------------------------------------- /libs/pages/src/about/about.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { AboutComponent } from './about.component'; 4 | 5 | describe('AboutComponent', () => { 6 | let component: AboutComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach( 10 | async(() => { 11 | TestBed.configureTestingModule({ 12 | declarations: [AboutComponent] 13 | }).compileComponents(); 14 | }) 15 | ); 16 | 17 | beforeEach(() => { 18 | fixture = TestBed.createComponent(AboutComponent); 19 | component = fixture.componentInstance; 20 | fixture.detectChanges(); 21 | }); 22 | 23 | it('should create', () => { 24 | expect(component).toBeTruthy(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /libs/pages/src/about/about.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, VERSION } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-about', 5 | template: ` 6 |

About

7 |

8 | ` 9 | }) 10 | export class AboutComponent implements OnInit { 11 | constructor() {} 12 | 13 | ngOnInit() {} 14 | 15 | lines = [ 16 | { class: 'text-primary', text: 'This project brings together a lot of good stuff:' }, 17 | { class: 'text-muted', text: 'The repository is structured using @nrwl/nx' }, 18 | { class: 'text-info', text: `This app runs on Angular v${VERSION.full}` }, 19 | { class: 'text-success', text: `Server Side Rendering by Angular Universal` }, 20 | { class: 'text-warning', text: `Support for running in a Docker container` }, 21 | { class: 'text-danger', text: `Support for hosting on now.sh` }, 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /libs/pages/src/contact/contact.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ContactComponent } from './contact.component'; 4 | 5 | describe('ContactComponent', () => { 6 | let component: ContactComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach( 10 | async(() => { 11 | TestBed.configureTestingModule({ 12 | declarations: [ContactComponent] 13 | }).compileComponents(); 14 | }) 15 | ); 16 | 17 | beforeEach(() => { 18 | fixture = TestBed.createComponent(ContactComponent); 19 | component = fixture.componentInstance; 20 | fixture.detectChanges(); 21 | }); 22 | 23 | it('should create', () => { 24 | expect(component).toBeTruthy(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /libs/pages/src/contact/contact.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-contact', 5 | template: ` 6 |

Contact

7 |
8 |
9 |
10 | 11 | 12 |
13 |
14 | 15 | 16 | We'll never share your email with anyone else. 17 |
18 |
19 | 20 | 21 |
22 |
23 | 27 |
28 | 29 |
30 |
31 | ` 32 | }) 33 | export class ContactComponent { 34 | submit($event) { 35 | $event.preventDefault(); 36 | window.alert('Sending the form is not implemented!'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/pages/src/home/home.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HomeComponent } from './home.component'; 4 | 5 | describe('HomeComponent', () => { 6 | let component: HomeComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach( 10 | async(() => { 11 | TestBed.configureTestingModule({ 12 | declarations: [HomeComponent] 13 | }).compileComponents(); 14 | }) 15 | ); 16 | 17 | beforeEach(() => { 18 | fixture = TestBed.createComponent(HomeComponent); 19 | component = fixture.componentInstance; 20 | fixture.detectChanges(); 21 | }); 22 | 23 | it('should create', () => { 24 | expect(component).toBeTruthy(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /libs/pages/src/home/home.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-home', 5 | template: ` 6 |

Home

7 |

Is where the heart is...

8 |
On the bus...
9 |

10 | 11 | 12 | 13 |

14 | `, 15 | }) 16 | export class HomeComponent {} 17 | -------------------------------------------------------------------------------- /libs/pages/src/pages-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | import { HomeComponent } from './home/home.component'; 4 | import { AboutComponent } from './about/about.component'; 5 | import { ContactComponent } from './contact/contact.component'; 6 | 7 | const routes: Routes = [ 8 | { path: '', redirectTo: 'home', pathMatch: 'full' }, 9 | { path: 'about', component: AboutComponent }, 10 | { path: 'contact', component: ContactComponent }, 11 | { path: 'home', component: HomeComponent } 12 | ]; 13 | 14 | @NgModule({ 15 | imports: [RouterModule.forChild(routes)], 16 | exports: [RouterModule] 17 | }) 18 | export class PagesRoutingModule {} 19 | -------------------------------------------------------------------------------- /libs/pages/src/pages.module.spec.ts: -------------------------------------------------------------------------------- 1 | import { PagesModule } from './pages.module'; 2 | 3 | describe('PagesModule', () => { 4 | it('should work', () => { 5 | expect(new PagesModule()).toBeDefined(); 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /libs/pages/src/pages.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | 4 | import { PagesRoutingModule } from './pages-routing.module'; 5 | 6 | import { HomeComponent } from './home/home.component'; 7 | import { AboutComponent } from './about/about.component'; 8 | import { ContactComponent } from './contact/contact.component'; 9 | 10 | @NgModule({ 11 | imports: [CommonModule, PagesRoutingModule], 12 | declarations: [HomeComponent, AboutComponent, ContactComponent] 13 | }) 14 | export class PagesModule {} 15 | -------------------------------------------------------------------------------- /libs/ui/index.ts: -------------------------------------------------------------------------------- 1 | export { UiModule } from './src/ui.module'; 2 | -------------------------------------------------------------------------------- /libs/ui/src/footer/footer.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { FooterComponent } from './footer.component'; 4 | 5 | describe('FooterComponent', () => { 6 | let component: FooterComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach( 10 | async(() => { 11 | TestBed.configureTestingModule({ 12 | declarations: [FooterComponent] 13 | }).compileComponents(); 14 | }) 15 | ); 16 | 17 | beforeEach(() => { 18 | fixture = TestBed.createComponent(FooterComponent); 19 | component = fixture.componentInstance; 20 | fixture.detectChanges(); 21 | }); 22 | 23 | it('should create', () => { 24 | expect(component).toBeTruthy(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /libs/ui/src/footer/footer.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, isDevMode, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-footer', 5 | template: ` 6 | 14 | `, 15 | styles: [ 16 | ` 17 | .fa-heart { 18 | color: red; 19 | } 20 | ` 21 | ] 22 | }) 23 | export class FooterComponent implements OnInit { 24 | public devMode: boolean = false 25 | ngOnInit() { 26 | if (isDevMode()) { 27 | this.devMode = isDevMode() 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libs/ui/src/header/header.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HeaderComponent } from './header.component'; 4 | import { RouterTestingModule } from '@angular/router/testing'; 5 | 6 | describe('HeaderComponent', () => { 7 | let component: HeaderComponent; 8 | let fixture: ComponentFixture; 9 | 10 | beforeEach( 11 | async(() => { 12 | TestBed.configureTestingModule({ 13 | imports: [RouterTestingModule], 14 | declarations: [HeaderComponent] 15 | }).compileComponents(); 16 | }) 17 | ); 18 | 19 | beforeEach(() => { 20 | fixture = TestBed.createComponent(HeaderComponent); 21 | component = fixture.componentInstance; 22 | fixture.detectChanges(); 23 | }); 24 | 25 | it('should create', () => { 26 | expect(component).toBeTruthy(); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /libs/ui/src/header/header.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-header', 5 | template: ` 6 | 18 | ` 19 | }) 20 | export class HeaderComponent { 21 | public title = 'NX Demo'; 22 | public links = [ 23 | { label: 'Home', url: '/home' }, 24 | { label: 'About', url: '/about' }, 25 | { label: 'Contact', url: '/contact' } 26 | ]; 27 | } 28 | -------------------------------------------------------------------------------- /libs/ui/src/layout/layout.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { RouterTestingModule } from '@angular/router/testing'; 4 | 5 | import { LayoutComponent } from './layout.component'; 6 | import { HeaderComponent } from '../header/header.component'; 7 | import { FooterComponent } from '../footer/footer.component'; 8 | 9 | describe('LayoutComponent', () => { 10 | let component: LayoutComponent; 11 | let fixture: ComponentFixture; 12 | 13 | beforeEach( 14 | async(() => { 15 | TestBed.configureTestingModule({ 16 | imports: [RouterTestingModule], 17 | declarations: [LayoutComponent, HeaderComponent, FooterComponent] 18 | }).compileComponents(); 19 | }) 20 | ); 21 | 22 | beforeEach(() => { 23 | fixture = TestBed.createComponent(LayoutComponent); 24 | component = fixture.componentInstance; 25 | fixture.detectChanges(); 26 | }); 27 | 28 | it('should create', () => { 29 | expect(component).toBeTruthy(); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /libs/ui/src/layout/layout.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-layout', 5 | template: ` 6 | 7 |
8 | 9 |
10 | 11 | ` 12 | }) 13 | export class LayoutComponent {} 14 | -------------------------------------------------------------------------------- /libs/ui/src/ui.module.spec.ts: -------------------------------------------------------------------------------- 1 | import { UiModule } from './ui.module'; 2 | 3 | describe('UiModule', () => { 4 | it('should work', () => { 5 | expect(new UiModule()).toBeDefined(); 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /libs/ui/src/ui.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | import { RouterModule } from '@angular/router'; 4 | import { LayoutComponent } from './layout/layout.component'; 5 | import { HeaderComponent } from './header/header.component'; 6 | import { FooterComponent } from './footer/footer.component'; 7 | 8 | @NgModule({ 9 | imports: [CommonModule, RouterModule], 10 | declarations: [LayoutComponent, HeaderComponent, FooterComponent] 11 | }) 12 | export class UiModule {} 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-nx-ssr", 3 | "version": "0.0.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "ng": "ng", 7 | "start": "ng serve", 8 | "build": "npm run build:web && npm run build:server", 9 | "build:web": "ng build --prod --app=web", 10 | "build:server": "ng build --prod --app=server --output-hashing=false", 11 | "postbuild:server": "webpack --config webpack.server.config.js --progress --colors", 12 | "docker:build": "docker build -t beeman/angular-nx-ssr:latest .", 13 | "docker:run": "docker run -it -p 4000:4000 --name angular-nx-ssr --rm beeman/angular-nx-ssr:latest", 14 | "now:deploy": "now --docker", 15 | "now:alias": "now alias", 16 | "now": "npm run now:deploy && npm run now:alias", 17 | "serve": "node dist/apps/server/server", 18 | "test": "ng test", 19 | "lint": "ng lint", 20 | "e2e": "ng e2e", 21 | "format": "prettier --single-quote --print-width 120 --write \"{apps,libs}/**/*.ts\"" 22 | }, 23 | "publishConfig": { 24 | "access": "public" 25 | }, 26 | "private": false, 27 | "dependencies": { 28 | "@angular/animations": "5.0.0", 29 | "@angular/common": "5.0.0", 30 | "@angular/compiler": "5.0.0", 31 | "@angular/core": "5.0.0", 32 | "@angular/forms": "5.0.0", 33 | "@angular/http": "5.0.0", 34 | "@angular/platform-browser": "5.0.0", 35 | "@angular/platform-browser-dynamic": "5.0.0", 36 | "@angular/platform-server": "^5.0.0", 37 | "@angular/router": "5.0.0", 38 | "@ngrx/effects": "^4.1.0", 39 | "@ngrx/router-store": "^4.1.0", 40 | "@ngrx/store": "^4.1.0", 41 | "@ngrx/store-devtools": "4.0.0", 42 | "@nguniversal/express-engine": "^5.0.0-beta.5", 43 | "@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5", 44 | "@nrwl/nx": "0.1.1", 45 | "bootswatch": "^4.0.0-beta.2", 46 | "core-js": "^2.4.1", 47 | "font-awesome": "^4.7.0", 48 | "rxjs": "^5.5.2", 49 | "ts-loader": "^3.1.1", 50 | "zone.js": "^0.8.14" 51 | }, 52 | "devDependencies": { 53 | "@angular/cli": "nrwl/fix-cli-build", 54 | "@angular/compiler-cli": "5.0.0", 55 | "@angular/language-service": "5.0.0", 56 | "@nrwl/schematics": "0.1.1", 57 | "@types/jasmine": "~2.5.53", 58 | "@types/jasminewd2": "~2.0.2", 59 | "@types/node": "~6.0.60", 60 | "codelyzer": "~3.1.1", 61 | "jasmine-core": "~2.6.2", 62 | "jasmine-spec-reporter": "~4.1.0", 63 | "karma": "~1.7.0", 64 | "karma-chrome-launcher": "~2.1.1", 65 | "karma-cli": "~1.0.1", 66 | "karma-coverage-istanbul-reporter": "^1.2.1", 67 | "karma-jasmine": "~1.1.0", 68 | "karma-jasmine-html-reporter": "^0.2.2", 69 | "prettier": "1.7.4", 70 | "protractor": "~5.1.2", 71 | "ts-node": "~3.2.0", 72 | "tslint": "~5.3.2", 73 | "typescript": "2.4.2" 74 | }, 75 | "now": { 76 | "name": "angular-nx-ssr", 77 | "alias": [ 78 | "angular-nx-ssr" 79 | ] 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './apps/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: './tsconfig.e2e.json' 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /server.ts: -------------------------------------------------------------------------------- 1 | // These are important and needed before anything else 2 | import 'zone.js/dist/zone-node'; 3 | import 'reflect-metadata'; 4 | 5 | // import { renderModuleFactory } from '@angular/platform-server'; 6 | import { ngExpressEngine } from '@nguniversal/express-engine'; 7 | 8 | // import { enableProdMode } from '@angular/core'; 9 | 10 | import * as express from 'express'; 11 | import { join } from 'path'; 12 | 13 | // enableProdMode(); 14 | 15 | // Express server 16 | const app = express(); 17 | 18 | const PORT = process.env.PORT || 4000; 19 | 20 | const DIST_FOLDER = join(process.cwd(), 'dist', 'apps'); 21 | 22 | const webFolder = (file = '') => join(DIST_FOLDER, 'web', file) 23 | 24 | const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/apps/server/main.bundle'); 25 | const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader'); 26 | 27 | app.engine('html', ngExpressEngine({ 28 | bootstrap: AppServerModuleNgFactory, 29 | providers: [ 30 | provideModuleMap(LAZY_MODULE_MAP) 31 | ] 32 | })); 33 | 34 | app.set('view engine', 'html'); 35 | app.set('views', webFolder()); 36 | 37 | // Server static files from /browser 38 | app.get('*.*', express.static(webFolder())); 39 | 40 | // All regular routes use the Universal engine 41 | app.get('*', (req = '', res) => { 42 | res.render(webFolder('index.html'), { req }); 43 | }); 44 | 45 | // Start up the Node server 46 | app.listen(PORT, () => { 47 | console.log(`Node server listening on http://localhost:${PORT}`); 48 | }); 49 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | require('zone.js/dist/long-stack-trace-zone'); 3 | require('zone.js/dist/proxy.js'); 4 | require('zone.js/dist/sync-test'); 5 | require('zone.js/dist/jasmine-patch'); 6 | require('zone.js/dist/async-test'); 7 | require('zone.js/dist/fake-async-test'); 8 | const getTestBed = require('@angular/core/testing').getTestBed; 9 | const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule; 10 | const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing').platformBrowserDynamicTesting; 11 | 12 | // Prevent Karma from running prematurely. 13 | __karma__.loaded = function () {}; 14 | 15 | // First, initialize the Angular testing environment. 16 | getTestBed().initTestEnvironment( 17 | BrowserDynamicTestingModule, 18 | platformBrowserDynamicTesting() 19 | ); 20 | // Then we find all the tests. 21 | const contextApps = require.context('./apps', true, /\.spec\.ts$/); 22 | // And load the modules. 23 | contextApps.keys().map(contextApps); 24 | 25 | const contextLibs = require.context('./libs', true, /\.spec\.ts$/); 26 | // And load the modules. 27 | contextLibs.keys().map(contextLibs); 28 | 29 | // Finally, start Karma to run the tests. 30 | __karma__.start(); 31 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/app", 5 | "module": "es2015", 6 | "baseUrl": ".", 7 | "paths": { 8 | "@angular-nx-ssr/*": [ 9 | "libs/*" 10 | ] 11 | } 12 | }, 13 | "exclude": [ 14 | "**/*.spec.ts", 15 | "**/*.e2e-spec.ts", 16 | "node_modules", 17 | "tmp" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/e2e", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ], 12 | "baseUrl": ".", 13 | "paths": { 14 | "@angular-nx-ssr/*": [ 15 | "libs/*" 16 | ] 17 | } 18 | }, 19 | "exclude": [ 20 | "**/*.spec.ts", 21 | "node_modules", 22 | "tmp" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "sourceMap": true, 5 | "declaration": false, 6 | "moduleResolution": "node", 7 | "emitDecoratorMetadata": true, 8 | "experimentalDecorators": true, 9 | "target": "es5", 10 | "typeRoots": [ 11 | "node_modules/@types" 12 | ], 13 | "lib": [ 14 | "es2017", 15 | "dom" 16 | ], 17 | "baseUrl": ".", 18 | "paths": { 19 | "@angular-nx-ssr/*": [ 20 | "libs/*" 21 | ] 22 | } 23 | }, 24 | "exclude": [ 25 | "node_modules", 26 | "tmp" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /tsconfig.server.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/app", 5 | "module": "commonjs", 6 | "baseUrl": ".", 7 | "paths": { 8 | "@angular-nx-ssr/*": [ 9 | "libs/*" 10 | ] 11 | } 12 | }, 13 | "exclude": [ 14 | "**/*.spec.ts", 15 | "**/*.e2e-spec.ts", 16 | "node_modules", 17 | "tmp" 18 | ], 19 | "angularCompilerOptions": { 20 | "entryModule": "./apps/web/src/app/app.server.module#AppServerModule" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/spec", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "node" 10 | ], 11 | "baseUrl": ".", 12 | "paths": { 13 | "@angular-nx-ssr/*": [ 14 | "libs/*" 15 | ] 16 | } 17 | }, 18 | "include": [ 19 | "**/*.ts" 20 | ], 21 | "exclude": [ 22 | "node_modules", 23 | "tmp" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer", 4 | "node_modules/@nrwl/schematics/src/tslint" 5 | ], 6 | "rules": { 7 | "arrow-return-shorthand": true, 8 | "callable-types": true, 9 | "class-name": true, 10 | "comment-format": [ 11 | true, 12 | "check-space" 13 | ], 14 | "curly": true, 15 | "eofline": true, 16 | "forin": true, 17 | "import-blacklist": [ 18 | true, 19 | "rxjs" 20 | ], 21 | "import-spacing": true, 22 | "indent": [ 23 | true, 24 | "spaces" 25 | ], 26 | "interface-over-type-literal": true, 27 | "label-position": true, 28 | "max-line-length": [ 29 | true, 30 | 140 31 | ], 32 | "member-access": false, 33 | "member-ordering": [ 34 | true, 35 | { 36 | "order": [ 37 | "static-field", 38 | "instance-field", 39 | "static-method", 40 | "instance-method" 41 | ] 42 | } 43 | ], 44 | "no-arg": true, 45 | "no-bitwise": true, 46 | "no-console": [ 47 | true, 48 | "debug", 49 | "info", 50 | "time", 51 | "timeEnd", 52 | "trace" 53 | ], 54 | "no-construct": true, 55 | "no-debugger": true, 56 | "no-duplicate-super": true, 57 | "no-empty": false, 58 | "no-empty-interface": true, 59 | "no-eval": true, 60 | "no-inferrable-types": [ 61 | true, 62 | "ignore-params" 63 | ], 64 | "no-misused-new": true, 65 | "no-non-null-assertion": true, 66 | "no-shadowed-variable": true, 67 | "no-string-literal": false, 68 | "no-string-throw": true, 69 | "no-switch-case-fall-through": true, 70 | "no-unnecessary-initializer": true, 71 | "no-unused-expression": true, 72 | "no-use-before-declare": true, 73 | "no-var-keyword": true, 74 | "object-literal-sort-keys": false, 75 | "prefer-const": true, 76 | "radix": true, 77 | "semicolon": [ 78 | true, 79 | "always" 80 | ], 81 | "triple-equals": [ 82 | true, 83 | "allow-null-check" 84 | ], 85 | "typeof-compare": true, 86 | "unified-signatures": true, 87 | "variable-name": false, 88 | "directive-selector": [ 89 | true, 90 | "attribute", 91 | "app", 92 | "camelCase" 93 | ], 94 | "component-selector": [ 95 | true, 96 | "element", 97 | "app", 98 | "kebab-case" 99 | ], 100 | "use-input-property-decorator": true, 101 | "use-output-property-decorator": true, 102 | "use-host-property-decorator": true, 103 | "no-input-rename": true, 104 | "no-output-rename": true, 105 | "use-life-cycle-interface": true, 106 | "use-pipe-transform-interface": true, 107 | "component-class-suffix": true, 108 | "directive-class-suffix": true, 109 | "no-access-missing-member": true, 110 | "templates-use-public": true, 111 | "invoke-injectable": true, 112 | 113 | "nx-enforce-module-boundaries": [ 114 | true, 115 | { 116 | "npmScope": "angular-nx-ssr", 117 | "lazyLoad": [] 118 | } 119 | ] 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /webpack.server.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | 4 | module.exports = { 5 | entry: { server: './server.ts' }, 6 | resolve: { extensions: ['.js', '.ts'] }, 7 | target: 'node', 8 | // this makes sure we include node_modules and other 3rd party libraries 9 | externals: [/(node_modules|main\..*\.js)/], 10 | output: { 11 | path: path.join(__dirname, 'dist', 'apps', 'server'), 12 | filename: '[name].js' 13 | }, 14 | module: { 15 | rules: [ 16 | { test: /\.ts$/, loader: 'ts-loader' } 17 | ] 18 | }, 19 | plugins: [ 20 | // Temporary Fix for issue: https://github.com/angular/angular/issues/11580 21 | // for "WARNING Critical dependency: the request of a dependency is an expression" 22 | new webpack.ContextReplacementPlugin( 23 | /(.+)?angular(\\|\/)core(.+)?/, 24 | path.join(__dirname, 'apps', 'web'), // location of your src 25 | {} // a map of your routes 26 | ), 27 | new webpack.ContextReplacementPlugin( 28 | /(.+)?express(\\|\/)(.+)?/, 29 | path.join(__dirname, 'apps', 'web'), 30 | {} 31 | ) 32 | ] 33 | } 34 | --------------------------------------------------------------------------------