Skip to content

Commit

Permalink
feat(): update to ng 9 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 7, 2020
1 parent b4ac083 commit 10a8fc7
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 234 deletions.
1 change: 0 additions & 1 deletion lib/angular-universal.constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const ANGULAR_UNIVERSAL_OPTIONS = 'ANGULAR_UNIVERSAL_OPTIONS';
export const LIVE_RELOAD_SCRIPT = `<script src="http://localhost:35729/livereload.js?snipver=1"></script>`;
22 changes: 13 additions & 9 deletions lib/angular-universal.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { APP_BASE_HREF } from '@angular/common';
import { DynamicModule, Inject, Module, OnModuleInit } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { readFileSync } from 'fs';
import { existsSync } from 'fs';
import { join } from 'path';
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { ANGULAR_UNIVERSAL_OPTIONS } from './angular-universal.constants';
import { angularUniversalProviders } from './angular-universal.providers';
import { AngularUniversalOptions } from './interfaces/angular-universal-options.interface';
Expand All @@ -19,23 +19,23 @@ export class AngularUniversalModule implements OnModuleInit {
) {}

static forRoot(options: AngularUniversalOptions): DynamicModule {
const indexHtml = existsSync(join(options.viewsPath, 'index.original.html'))
? 'index.original.html'
: 'index';

options = {
templatePath: join(options.viewsPath, 'index.html'),
templatePath: indexHtml,
rootStaticPath: '*.*',
renderPath: '*',
...options
};
const template = readFileSync(options.templatePath).toString();

return {
module: AngularUniversalModule,
providers: [
{
provide: ANGULAR_UNIVERSAL_OPTIONS,
useValue: {
...options,
template
}
useValue: options
}
]
};
Expand All @@ -51,7 +51,11 @@ export class AngularUniversalModule implements OnModuleInit {
}
const app = httpAdapter.getInstance();
app.get(this.ngOptions.renderPath, (req, res) =>
res.render(this.ngOptions.templatePath, { req, res })
res.render(this.ngOptions.templatePath, {
req,
res,
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }]
})
);
}
}
1 change: 0 additions & 1 deletion lib/compiler/index.ts

This file was deleted.

108 changes: 0 additions & 108 deletions lib/compiler/live-reload-compiler.ts

This file was deleted.

2 changes: 0 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from './angular-universal.module';
export * from './cache/in-memory-cache.storage';
export * from './compiler';
export * from './interfaces/angular-universal-options.interface';
export * from './interfaces/cache-storage.interface';
export * from './utils/domino.utils';
export * from './webpack-config.factory';
13 changes: 2 additions & 11 deletions lib/interfaces/angular-universal-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ export interface AngularUniversalOptions {
* The directory where the module should look for client bundle (Angular app).
*/
viewsPath: string;
/**
* Bundle file (webpack output with "AppServerModuleNgFactory")
*/
bundle: {
AppServerModuleNgFactory;
LAZY_MODULE_MAP;
provideModuleMap;
ngExpressEngine;
};
/**
* Path to index file.
* Default: {viewsPaths}/index.html
Expand Down Expand Up @@ -43,7 +34,7 @@ export interface AngularUniversalOptions {
storage?: CacheStorage;
};
/**
* Enable live-reload (auto-browser refresh)
* Module to bootstrap
*/
liveReload?: boolean;
bootstrap: any;
}
29 changes: 6 additions & 23 deletions lib/utils/setup-universal.utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { LIVE_RELOAD_SCRIPT } from '../angular-universal.constants';
import { InMemoryCacheStorage } from '../cache/in-memory-cache.storage';
import { AngularUniversalOptions } from '../interfaces/angular-universal-options.interface';

const DEFAULT_CACHE_EXPIRATION_TIME = 60000; // 60 seconds

export function setupUniversal(
app: any,
ngOptions: AngularUniversalOptions & { template: string }
) {
const {
AppServerModuleNgFactory,
LAZY_MODULE_MAP,
provideModuleMap,
ngExpressEngine
} = ngOptions.bundle;

export function setupUniversal(app: any, ngOptions: AngularUniversalOptions) {
const cacheOptions = getCacheOptions(ngOptions);

app.engine('html', (_, options, callback) => {
const originalUrl = options.req.originalUrl;
if (cacheOptions.isEnabled) {
Expand All @@ -27,11 +18,8 @@ export function setupUniversal(
}

ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
document: ngOptions.template,
url: options.req.url,
bootstrap: ngOptions.bootstrap,
providers: [
provideModuleMap(LAZY_MODULE_MAP),
{
provide: 'serverUrl',
useValue: `${options.req.protocol}://${options.req.get('host')}`
Expand All @@ -42,18 +30,13 @@ export function setupUniversal(
if (cacheOptions.isEnabled) {
cacheOptions.storage.set(originalUrl, html, cacheOptions.expiresIn);
}
if (ngOptions.liveReload) {
const headTagIndex = html.indexOf('<head>');
html =
html.substr(0, headTagIndex + 6) +
LIVE_RELOAD_SCRIPT +
html.substr(headTagIndex + 7, html.length);
}
callback(null, html);
});
});

app.set('view engine', 'html');
app.set('views', ngOptions.viewsPath);

// Serve static files
app.get(
ngOptions.rootStaticPath,
Expand Down
58 changes: 0 additions & 58 deletions lib/webpack-config.factory.ts

This file was deleted.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
"publish:next": "npm publish --access public --tag next"
},
"peerDependencies": {
"@angular/platform-server": "^8.0.0",
"@angular/platform-server": "^9.0.0",
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nguniversal/common": "^8.0.0",
"@nguniversal/express-engine": "^8.0.0",
"express": "^4.16.4",
"@nguniversal/common": "^9.0.0",
"@nguniversal/express-engine": "^9.0.0",
"express": "^4.17.1",
"zone.js": "^0.8.29 || ^0.9.0 || ^0.10.0"
},
"devDependencies": {
"@angular/platform-server": "8.2.12",
"@angular/platform-server": "9.0.0",
"@nestjs/common": "6.8.5",
"@nestjs/core": "6.8.5",
"@nguniversal/common": "8.1.1",
"@nguniversal/express-engine": "8.1.1",
"@nguniversal/common": "9.0.0",
"@nguniversal/express-engine": "9.0.0",
"@types/memory-cache": "0.2.0",
"@types/node": "11.15.0",
"express": "4.17.1",
Expand Down
6 changes: 0 additions & 6 deletions schematics/install/files/root/serve-script.js

This file was deleted.

8 changes: 0 additions & 8 deletions schematics/install/files/root/webpack.server.config.js

This file was deleted.

0 comments on commit 10a8fc7

Please sign in to comment.