Skip to content

Commit

Permalink
✨ Use generated nest app for ssr.
Browse files Browse the repository at this point in the history
  • Loading branch information
servrox committed May 27, 2019
1 parent fea1eb6 commit b448fd2
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 10,172 deletions.
46 changes: 19 additions & 27 deletions angular.json
Expand Up @@ -17,7 +17,7 @@
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/browser",
"outputPath": "dist/apps/ng-test-app",
"index": "apps/ng-test-app/src/index.html",
"main": "apps/ng-test-app/src/main.ts",
"polyfills": "apps/ng-test-app/src/polyfills.ts",
Expand All @@ -34,12 +34,10 @@
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "apps/ng-test-app/src/environments/environment.ts",
"with": "apps/ng-test-app/src/environments/environment.prod.ts"
}
],
"fileReplacements": [{
"replace": "apps/ng-test-app/src/environments/environment.ts",
"with": "apps/ng-test-app/src/environments/environment.prod.ts"
}],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
Expand All @@ -49,13 +47,11 @@
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
"budgets": [{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}]
}
}
},
Expand Down Expand Up @@ -99,18 +95,16 @@
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/server",
"outputPath": "dist/apps/ng-test-app-server",
"main": "apps/ng-test-app/src/main.server.ts",
"tsConfig": "apps/ng-test-app/tsconfig.server.json"
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "apps/ng-test-app/src/environments/environment.ts",
"with": "apps/ng-test-app/src/environments/environment.prod.ts"
}
]
"fileReplacements": [{
"replace": "apps/ng-test-app/src/environments/environment.ts",
"with": "apps/ng-test-app/src/environments/environment.prod.ts"
}]
}
}
}
Expand Down Expand Up @@ -167,12 +161,10 @@
"optimization": true,
"extractLicenses": true,
"inspect": false,
"fileReplacements": [
{
"replace": "apps/nest-test-app/src/environments/environment.ts",
"with": "apps/nest-test-app/src/environments/environment.prod.ts"
}
]
"fileReplacements": [{
"replace": "apps/nest-test-app/src/environments/environment.ts",
"with": "apps/nest-test-app/src/environments/environment.prod.ts"
}]
}
}
},
Expand Down
15 changes: 13 additions & 2 deletions apps/nest-test-app/src/app/app.module.ts
@@ -1,11 +1,22 @@
import { Module } from '@nestjs/common';
import { AngularUniversalModule, applyDomino } from '@nestjs/ng-universal';
import { join } from 'path';

import { AppController } from './app.controller';
import { AppService } from './app.service';

const BROWSER_DIR = join(process.cwd(), 'dist', 'apps', 'ng-test-app');
applyDomino(global, join(BROWSER_DIR, 'index.html'));

@Module({
imports: [],
imports: [
AngularUniversalModule.forRoot({
viewsPath: BROWSER_DIR,
bundle: require('../../../../dist/apps/ng-test-app-server/main'),
liveReload: true
})
],
controllers: [AppController],
providers: [AppService],
providers: [AppService]
})
export class AppModule {}
12 changes: 5 additions & 7 deletions apps/nest-test-app/src/main.ts
@@ -1,17 +1,15 @@
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
**/

import { enableProdMode } from '@angular/core';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app/app.module';

enableProdMode();

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.port || 3333;
// const port = process.env.port || 3333;
const port = process.env.port || 4000;
await app.listen(port, () => {
console.log(`Listening at http://localhost:${port}/${globalPrefix}`);
});
Expand Down
18 changes: 11 additions & 7 deletions apps/nest-test-app/tsconfig.json
@@ -1,12 +1,16 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": [
"node",
"jest"
]
"types": ["node", "jest"],
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "../../dist/apps/server-app"
},
"include": [
"**/*.ts"
]
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist"]
}
Expand Up @@ -5,11 +5,11 @@ const WebpackConfigFactory = require('@nestjs/ng-universal')

const config = WebpackConfigFactory.create(webpack, {
// Nest server for SSR
server: './server/main.ts'
server: './apps/nest-test-app/src/main.ts'
});
config.output = {
// Puts the output at the root of the dist folder
path: path.join(__dirname, 'dist'),
path: path.join(__dirname, '../../dist/apps/ng-test-app-server'),
filename: '[name].js'
};
config.plugins = [
Expand Down
16 changes: 16 additions & 0 deletions apps/ng-test-app/serve-script.js
@@ -0,0 +1,16 @@
const {
LiveReloadCompiler
} = require('@nestjs/ng-universal');

const compiler = new LiveReloadCompiler({
projectName: 'ng-test-app',
tsServerConfigFile: 'apps/nest-test-app/tsconfig.json',
watchDir: 'dist',
serverBundlePath: 'dist/apps/ng-test-app-server/main.js',
serverFilePath: 'dist/apps/server-app/main',
mainBundlePath: 'dist/apps/ng-test-app/main.js',
indexFilePath: 'dist/apps/ng-test-app/index.html',
outputDir: 'dist',
watchSsr: true
});
compiler.run();
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -25,9 +25,9 @@
"workspace-schematic": "./node_modules/.bin/nx workspace-schematic",
"dep-graph": "./node_modules/.bin/nx dep-graph",
"help": "./node_modules/.bin/nx help",
"serve": "node serve-script",
"compile:server": "webpack --config webpack.server.config.js --progress --colors",
"serve:ssr": "node dist/server",
"serve": "node apps/ng-test-app/serve-script",
"compile:server": "webpack --config apps/nest-test-app/webpack.server.config.js --progress --colors",
"serve:ssr": "node dist/apps/ng-test-app-server/server",
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
"build:client-and-server-bundles": "ng build --prod && ng run ng-test-app:server:production",
"go": "npm run build:ssr && npm run serve:ssr"
Expand Down
6 changes: 0 additions & 6 deletions serve-script.js

This file was deleted.

17 changes: 0 additions & 17 deletions server/app.module.ts

This file was deleted.

12 changes: 0 additions & 12 deletions server/main.ts

This file was deleted.

13 changes: 0 additions & 13 deletions server/tsconfig.json

This file was deleted.

0 comments on commit b448fd2

Please sign in to comment.