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

Universal support #23

Merged
merged 6 commits into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "dist/app/main-server.js",
"stopOnEntry": false,
"args": [],
"cwd": ".",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
11 changes: 10 additions & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ Or
```
$ npm install -g karma
$ karma start
```
```

### Run the Universal server locally:

```
$ gulp
$ npm run start_universal
```

Then navigate to localhost:3000
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "karma start"
"test": "karma start",
"start_universal": "node dist/app/main-server.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"angular2": "^2.0.0-beta.1",
"angular2-universal-preview": "^0.32.0",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.13",
"express": "^4.13.3",
"firebase": "^2.3.2",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
Expand All @@ -23,6 +26,7 @@
"gulp-inline": "^0.1.0",
"gulp-minify-css": "^1.2.2",
"gulp-sass": "^2.1.1",
"gulp-typescript": "^2.10.0",
"gulp-util": "^3.0.7",
"jasmine-core": "^2.3.4",
"karma": "^0.13.15",
Expand All @@ -33,6 +37,7 @@
"systemjs": "^0.19.6",
"systemjs-builder": "^0.14.11",
"ts-node": "^0.5.4",
"tsd": "^0.6.5",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the repo should use typings. tsd will be deprecated soon. (you also want to .gitignore typings/ folder. see upgrade commit angular/universal-starter@cfb7b52)

"typescript": "^1.7.3",
"typescript-node": "^0.1.3"
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {Component} from 'angular2/core';
})
export class Toast {
visible = false;
show(message){
message: string;
show(message:string){
this.message = message;
this.visible = true;
setTimeout(() => {
Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions src/main-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference path="../typings/tsd.d.ts" />

import * as path from 'path';
import * as express from 'express';
import {SERVER_LOCATION_PROVIDERS, ng2engine} from 'angular2-universal-preview/dist/server';

import {provide} from 'angular2/core';
import {APP_BASE_HREF, ROUTER_PROVIDERS} from 'angular2/router';

import {SHARED_PROVIDERS} from './shared-providers';

// Angular 2
import {App} from './app/app';

let app = express();
let root = path.join(path.resolve(__dirname, '..'));

// Express View
app.engine('.ng2.html', ng2engine);
app.set('views', root);
app.set('view engine', 'ng2.html');

// Serve static files
app.use(express.static(root));

// Routes
app.use('/', (req, res) => {
res.render('index', { App, providers: [
ROUTER_PROVIDERS,
SERVER_LOCATION_PROVIDERS,
provide(APP_BASE_HREF, {useValue: `http://localhost:3000${req.baseUrl}`}),
SHARED_PROVIDERS
] });
});

// Server
app.listen(3000, () => {
console.log('Listen on http://localhost:3000');
});
15 changes: 5 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {bootstrap} from 'angular2/platform/browser';
import {platform, provide} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {ROUTER_PROVIDERS, LocationStrategy, PathLocationStrategy} from 'angular2/router';

import {AuthService} from './services/Auth';
import {Backend, BackendConfig} from './services/Backend';
import {Nav} from './services/Nav';

const FIREBASE_URL = 'https://ng2-forum-demo.firebaseio.com';
import {SHARED_PROVIDERS} from './shared-providers';

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

bootstrap(App,[
bootstrap(App, [
ROUTER_PROVIDERS,
AuthService,
Backend,
provide(BackendConfig, {useValue: {url: FIREBASE_URL }})
SHARED_PROVIDERS,
provide(LocationStrategy, {useClass: PathLocationStrategy})
]);

17 changes: 11 additions & 6 deletions src/services/Backend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Firebase from 'firebase';
/// <reference path="../../typings/tsd.d.ts" />

import * as Firebase from 'firebase';
import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import {ReplaySubject} from 'rxjs/subject/ReplaySubject';
Expand All @@ -13,11 +15,14 @@ export class Backend {
authState: ReplaySubject<any> = new ReplaySubject(1);
ref: Firebase;
constructor(config: BackendConfig){
this.ref = new Firebase(config.url);
try {
this.ref = new Firebase(config.url);
} catch(e) {
console.error('something went wrong', config.url, e);
}
}
authenticate(){
let authRequest = new Observable(obs => {

this.ref.authWithOAuthPopup('github', (err, res) => {
if(err){
obs.error(err);
Expand All @@ -26,10 +31,10 @@ export class Backend {
obs.next(res);
}
})

});

authRequest.subscribe(this.authState);

}
}
12 changes: 12 additions & 0 deletions src/shared-providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {provide} from 'angular2/core';

import {AuthService} from './services/Auth';
import {Backend, BackendConfig} from './services/Backend';

export const FIREBASE_URL = 'https://ng2-forum-demo.firebaseio.com';

export const SHARED_PROVIDERS = [
AuthService,
Backend,
provide(BackendConfig, {useValue: {url: FIREBASE_URL }})
];
14 changes: 7 additions & 7 deletions tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ const inline = require('gulp-inline');
const minifyCSS = require('gulp-minify-css');

export const build = (gulp, config) => {
gulp.task('default', ['compile:sass','compile:app','copy:dev'], () => {

gulp.task('default', ['compile:sass','compile:app','compile:main-server','copy:dev'], () => {

gulp.src(config.index)
.pipe(inline({
base: 'dist',
css: minifyCSS,
disabledTypes: ['img', 'js'], // Only inline css files
disabledTypes: ['img', 'js'], // Only inline css files
}))
.pipe(gulp.dest('dist/'));

});


}
19 changes: 16 additions & 3 deletions tasks/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ declare var require;

var Builder = require('systemjs-builder');
var util = require('gulp-util');
var ts = require('gulp-typescript');

export const compile = (gulp, config) => {

gulp.task('compile:main-server', () => {
return gulp.src('src/**/*.ts')
.pipe(ts({
noImplicitAny: false,
typescript: require('typescript'),
module: 'commonjs',
emitDecoratorMetadata: true,
experimentalDecorators: true,
moduleResolution: 'node'
}))
.pipe(gulp.dest('dist/app'));
});

gulp.task('compile:app', ['compile:vendor'], () => {

let builder = new Builder();

return builder.loadConfig(config.system.configFile)
Expand Down Expand Up @@ -34,4 +47,4 @@ export const compile = (gulp, config) => {
return builder.buildStatic('app/ng2-service-worker', 'dist/worker.js', {minify: util.env.production})
})
});
}
}
11 changes: 8 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
"target": "es5",
"noImplicitAny": false,
"outDir": "build",
"rootDir": ".",
"rootDir": "src",
"sourceMap": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node"
"moduleResolution": "node",
"noResolve": false
},
"files": [
"src/main.ts",
"src/main-server.ts"
],
"exclude": [
"node_modules"
"node_modules/"
]
}
21 changes: 21 additions & 0 deletions tsd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"firebase/firebase.d.ts": {
"commit": "2cad4a3cff770c37b40496188c246b1a60e87e2d"
},
"mime/mime.d.ts": {
"commit": "2cad4a3cff770c37b40496188c246b1a60e87e2d"
},
"serve-static/serve-static.d.ts": {
"commit": "2cad4a3cff770c37b40496188c246b1a60e87e2d"
},
"express/express.d.ts": {
"commit": "2cad4a3cff770c37b40496188c246b1a60e87e2d"
}
}
}
9 changes: 9 additions & 0 deletions typings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Eventually typings should not be included in version control.

Right now they must be, because the installed typings have to
be manually updated to prevent duplication with node.d.ts typings
included with angular2.

Local edits: remove reference to node.d.ts from express.d.ts.

(@jeffbcross 2016-01-21)
Loading