Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 9b77859

Browse files
committed
Merge pull request #23 from jeffbcross/universal-worker
feature(universal) add universal support
2 parents 93db47b + 6bad4cb commit 9b77859

File tree

19 files changed

+1731
-32
lines changed

19 files changed

+1731
-32
lines changed

.vscode/launch.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "dist/app/main-server.js",
9+
"stopOnEntry": false,
10+
"args": [],
11+
"cwd": ".",
12+
"runtimeExecutable": null,
13+
"runtimeArgs": [
14+
"--nolazy"
15+
],
16+
"env": {
17+
"NODE_ENV": "development"
18+
},
19+
"externalConsole": false,
20+
"sourceMaps": false,
21+
"outDir": null
22+
},
23+
{
24+
"name": "Attach",
25+
"type": "node",
26+
"request": "attach",
27+
"port": 5858
28+
}
29+
]
30+
}

DEVELOPER.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ Or
1919
```
2020
$ npm install -g karma
2121
$ karma start
22-
```
22+
```
23+
24+
### Run the Universal server locally:
25+
26+
```
27+
$ gulp
28+
$ npm run start_universal
29+
```
30+
31+
Then navigate to localhost:3000

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "karma start"
7+
"test": "karma start",
8+
"start_universal": "node dist/app/main-server.js"
89
},
910
"author": "",
1011
"license": "MIT",
1112
"dependencies": {
1213
"angular2": "^2.0.0-beta.1",
14+
"angular2-universal-preview": "^0.32.0",
1315
"es6-promise": "^3.0.2",
1416
"es6-shim": "^0.33.13",
17+
"express": "^4.13.3",
1518
"firebase": "^2.3.2",
1619
"reflect-metadata": "0.1.2",
1720
"rxjs": "5.0.0-beta.0",
@@ -23,6 +26,7 @@
2326
"gulp-inline": "^0.1.0",
2427
"gulp-minify-css": "^1.2.2",
2528
"gulp-sass": "^2.1.1",
29+
"gulp-typescript": "^2.10.0",
2630
"gulp-util": "^3.0.7",
2731
"jasmine-core": "^2.3.4",
2832
"karma": "^0.13.15",
@@ -33,6 +37,7 @@
3337
"systemjs": "^0.19.6",
3438
"systemjs-builder": "^0.14.11",
3539
"ts-node": "^0.5.4",
40+
"tsd": "^0.6.5",
3641
"typescript": "^1.7.3",
3742
"typescript-node": "^0.1.3"
3843
}

src/components/toast.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {Component} from 'angular2/core';
88
})
99
export class Toast {
1010
visible = false;
11-
show(message){
11+
message: string;
12+
show(message:string){
1213
this.message = message;
1314
this.visible = true;
1415
setTimeout(() => {
File renamed without changes.

src/main-server.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// <reference path="../typings/tsd.d.ts" />
2+
3+
import * as path from 'path';
4+
import * as express from 'express';
5+
import {SERVER_LOCATION_PROVIDERS, ng2engine} from 'angular2-universal-preview/dist/server';
6+
7+
import {provide} from 'angular2/core';
8+
import {APP_BASE_HREF, ROUTER_PROVIDERS} from 'angular2/router';
9+
10+
import {SHARED_PROVIDERS} from './shared-providers';
11+
12+
// Angular 2
13+
import {App} from './app/app';
14+
15+
let app = express();
16+
let root = path.join(path.resolve(__dirname, '..'));
17+
18+
// Express View
19+
app.engine('.ng2.html', ng2engine);
20+
app.set('views', root);
21+
app.set('view engine', 'ng2.html');
22+
23+
// Serve static files
24+
app.use(express.static(root));
25+
26+
// Routes
27+
app.use('/', (req, res) => {
28+
res.render('index', { App, providers: [
29+
ROUTER_PROVIDERS,
30+
SERVER_LOCATION_PROVIDERS,
31+
provide(APP_BASE_HREF, {useValue: `http://localhost:3000${req.baseUrl}`}),
32+
SHARED_PROVIDERS
33+
] });
34+
});
35+
36+
// Server
37+
app.listen(3000, () => {
38+
console.log('Listen on http://localhost:3000');
39+
});

src/main.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import {bootstrap} from 'angular2/platform/browser';
22
import {platform, provide} from 'angular2/core';
3-
import {ROUTER_PROVIDERS} from 'angular2/router';
3+
import {ROUTER_PROVIDERS, LocationStrategy, PathLocationStrategy} from 'angular2/router';
44

5-
import {AuthService} from './services/Auth';
6-
import {Backend, BackendConfig} from './services/Backend';
7-
import {Nav} from './services/Nav';
8-
9-
const FIREBASE_URL = 'https://ng2-forum-demo.firebaseio.com';
5+
import {SHARED_PROVIDERS} from './shared-providers';
106

117
import {App} from './app/app';
128

13-
bootstrap(App,[
9+
bootstrap(App, [
1410
ROUTER_PROVIDERS,
15-
AuthService,
16-
Backend,
17-
provide(BackendConfig, {useValue: {url: FIREBASE_URL }})
11+
SHARED_PROVIDERS,
12+
provide(LocationStrategy, {useClass: PathLocationStrategy})
1813
]);
1914

src/services/Backend.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import Firebase from 'firebase';
1+
/// <reference path="../../typings/tsd.d.ts" />
2+
3+
import * as Firebase from 'firebase';
24
import {Injectable} from 'angular2/core';
35
import {Observable} from 'rxjs/Observable';
46
import {ReplaySubject} from 'rxjs/subject/ReplaySubject';
@@ -13,11 +15,14 @@ export class Backend {
1315
authState: ReplaySubject<any> = new ReplaySubject(1);
1416
ref: Firebase;
1517
constructor(config: BackendConfig){
16-
this.ref = new Firebase(config.url);
18+
try {
19+
this.ref = new Firebase(config.url);
20+
} catch(e) {
21+
console.error('something went wrong', config.url, e);
22+
}
1723
}
1824
authenticate(){
1925
let authRequest = new Observable(obs => {
20-
2126
this.ref.authWithOAuthPopup('github', (err, res) => {
2227
if(err){
2328
obs.error(err);
@@ -26,10 +31,10 @@ export class Backend {
2631
obs.next(res);
2732
}
2833
})
29-
34+
3035
});
31-
36+
3237
authRequest.subscribe(this.authState);
33-
38+
3439
}
3540
}

src/shared-providers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {provide} from 'angular2/core';
2+
3+
import {AuthService} from './services/Auth';
4+
import {Backend, BackendConfig} from './services/Backend';
5+
6+
export const FIREBASE_URL = 'https://ng2-forum-demo.firebaseio.com';
7+
8+
export const SHARED_PROVIDERS = [
9+
AuthService,
10+
Backend,
11+
provide(BackendConfig, {useValue: {url: FIREBASE_URL }})
12+
];

tasks/build.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ const inline = require('gulp-inline');
55
const minifyCSS = require('gulp-minify-css');
66

77
export const build = (gulp, config) => {
8-
9-
gulp.task('default', ['compile:sass','compile:app','copy:dev'], () => {
10-
8+
9+
gulp.task('default', ['compile:sass','compile:app','compile:main-server','copy:dev'], () => {
10+
1111
gulp.src(config.index)
1212
.pipe(inline({
1313
base: 'dist',
1414
css: minifyCSS,
15-
disabledTypes: ['img', 'js'], // Only inline css files
15+
disabledTypes: ['img', 'js'], // Only inline css files
1616
}))
1717
.pipe(gulp.dest('dist/'));
18-
18+
1919
});
20-
21-
20+
21+
2222
}

0 commit comments

Comments
 (0)