mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
further changes for angular 10
This commit is contained in:
30
angular.json
30
angular.json
@@ -17,6 +17,7 @@
|
||||
"build": {
|
||||
"builder": "@angular-builders/custom-webpack:browser",
|
||||
"options": {
|
||||
"preserveSymlinks": true,
|
||||
"customWebpackConfig": {
|
||||
"path": "./webpack/webpack.browser.ts",
|
||||
"mergeStrategies": {
|
||||
@@ -37,7 +38,7 @@
|
||||
"main": "src/main.browser.ts",
|
||||
"polyfills": "src/polyfills.ts",
|
||||
"tsConfig": "tsconfig.app.json",
|
||||
"aot": false,
|
||||
"aot": true,
|
||||
"assets": [
|
||||
"src/assets",
|
||||
"src/robots.txt"
|
||||
@@ -159,6 +160,33 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"serve-ssr": {
|
||||
"builder": "@nguniversal/builders:ssr-dev-server",
|
||||
"options": {
|
||||
"browserTarget": "dspace-angular:build",
|
||||
"serverTarget": "dspace-angular:server",
|
||||
"port": 4000
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"browserTarget": "dspace-angular:build:production",
|
||||
"serverTarget": "dspace-angular:server:production"
|
||||
}
|
||||
}
|
||||
},
|
||||
"prerender": {
|
||||
"builder": "@nguniversal/builders:prerender",
|
||||
"options": {
|
||||
"browserTarget": "dspace-angular:build:production",
|
||||
"serverTarget": "dspace-angular:server:production",
|
||||
"routes": [
|
||||
"/"
|
||||
]
|
||||
},
|
||||
"configurations": {
|
||||
"production": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,14 +24,14 @@
|
||||
"build:prod": "yarn run build:ssr",
|
||||
"build:ssr": "yarn run build:client-and-server-bundles && yarn run compile:server",
|
||||
"ng-high-memory": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng",
|
||||
"build:client-and-server-bundles": "npm run ng-high-memory -- build --prod && npm run ng-high-memory -- run dspace-angular:server:production --bundleDependencies all",
|
||||
"build:client-and-server-bundles": "npm run ng-high-memory -- build --prod && npm run ng-high-memory -- run dspace-angular:server:production --bundleDependencies true",
|
||||
"test:watch": "npm-run-all --parallel config:test:watch test",
|
||||
"test": "npm run ng-high-memory -- test --sourceMap=true --watch=true",
|
||||
"test:headless": "npm run ng-high-memory -- test --watch=false --sourceMap=true --browsers=ChromeHeadless --code-coverage",
|
||||
"lint": "ng lint",
|
||||
"e2e": "ng e2e",
|
||||
"e2e:ci": "ng e2e --protractor-config=./e2e/protractor-ci.conf.js",
|
||||
"compile:server": "webpack --config webpack.server.config.js --progress --colors",
|
||||
"compile:server": "webpack --config webpack.server.config.js --progress --color",
|
||||
"serve:ssr": "node dist/server",
|
||||
"clean:coverage": "rimraf coverage",
|
||||
"clean:dist": "rimraf dist",
|
||||
@@ -130,6 +130,7 @@
|
||||
"@fortawesome/fontawesome-free": "^5.5.0",
|
||||
"@ngrx/store-devtools": "^10.0.1",
|
||||
"@ngtools/webpack": "10.2.0",
|
||||
"@nguniversal/builders": "~10.1.0",
|
||||
"@types/deep-freeze": "0.1.1",
|
||||
"@types/express": "^4.17.9",
|
||||
"@types/file-saver": "^1.3.0",
|
||||
@@ -149,7 +150,7 @@
|
||||
"html-webpack-plugin": "^4.5.0",
|
||||
"http-proxy-middleware": "^1.0.5",
|
||||
"jasmine-core": "^3.6.0",
|
||||
"jasmine-marbles": "0.3.1",
|
||||
"jasmine-marbles": "0.6.0",
|
||||
"jasmine-spec-reporter": "^6.0.0",
|
||||
"karma": "^5.2.3",
|
||||
"karma-chrome-launcher": "^3.1.0",
|
||||
|
212
server.ts
212
server.ts
@@ -15,6 +15,7 @@
|
||||
* import for `ngExpressEngine`.
|
||||
*/
|
||||
|
||||
import 'zone.js/dist/zone-node';
|
||||
import 'reflect-metadata';
|
||||
import 'rxjs';
|
||||
|
||||
@@ -25,113 +26,119 @@ import * as morgan from 'morgan';
|
||||
import * as express from 'express';
|
||||
import * as bodyParser from 'body-parser';
|
||||
import * as compression from 'compression';
|
||||
import * as cookieParser from 'cookie-parser';
|
||||
import { join } from 'path';
|
||||
|
||||
import { enableProdMode, NgModuleFactory, Type } from '@angular/core';
|
||||
|
||||
import { enableProdMode } from '@angular/core';
|
||||
import { existsSync } from 'fs';
|
||||
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
||||
import { environment } from './src/environments/environment';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
import { hasValue, hasNoValue } from './src/app/shared/empty.util';
|
||||
import { APP_BASE_HREF } from '@angular/common';
|
||||
|
||||
/*
|
||||
* Set path for the browser application's dist folder
|
||||
*/
|
||||
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
|
||||
|
||||
const indexHtml = existsSync(join(DIST_FOLDER, 'index.html')) ? 'index.html' : 'index';
|
||||
|
||||
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
|
||||
const { ServerAppModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap } = require('./dist/server/main');
|
||||
const { ServerAppModule, ngExpressEngine } = require('./dist/server/main');
|
||||
|
||||
/*
|
||||
* Create a new express application
|
||||
*/
|
||||
const app = express();
|
||||
const cookieParser = require('cookie-parser');
|
||||
|
||||
/*
|
||||
* If production mode is enabled in the environment file:
|
||||
* - Enable Angular's production mode
|
||||
* - Enable compression for response bodies. See [compression](https://github.com/expressjs/compression)
|
||||
*/
|
||||
if (environment.production) {
|
||||
enableProdMode();
|
||||
app.use(compression());
|
||||
// The Express app is exported so that it can be used by serverless Functions.
|
||||
export function app() {
|
||||
|
||||
/*
|
||||
* Create a new express application
|
||||
*/
|
||||
const server = express();
|
||||
|
||||
|
||||
/*
|
||||
* If production mode is enabled in the environment file:
|
||||
* - Enable Angular's production mode
|
||||
* - Enable compression for response bodies. See [compression](https://github.com/expressjs/compression)
|
||||
*/
|
||||
if (environment.production) {
|
||||
enableProdMode();
|
||||
server.use(compression());
|
||||
}
|
||||
|
||||
/*
|
||||
* Enable request logging
|
||||
* See [morgan](https://github.com/expressjs/morgan)
|
||||
*/
|
||||
server.use(morgan('dev'));
|
||||
|
||||
/*
|
||||
* Add cookie parser middleware
|
||||
* See [morgan](https://github.com/expressjs/cookie-parser)
|
||||
*/
|
||||
server.use(cookieParser());
|
||||
|
||||
/*
|
||||
* Add parser for request bodies
|
||||
* See [morgan](https://github.com/expressjs/body-parser)
|
||||
*/
|
||||
server.use(bodyParser.json());
|
||||
|
||||
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
|
||||
server.engine('html', (_, options, callback) =>
|
||||
ngExpressEngine({
|
||||
bootstrap: ServerAppModule,
|
||||
providers: [
|
||||
{
|
||||
provide: REQUEST,
|
||||
useValue: (options as any).req,
|
||||
},
|
||||
{
|
||||
provide: RESPONSE,
|
||||
useValue: (options as any).req.res,
|
||||
},
|
||||
],
|
||||
})(_, (options as any), callback)
|
||||
);
|
||||
|
||||
/*
|
||||
* Register the view engines for html and ejs
|
||||
*/
|
||||
server.set('view engine', 'html');
|
||||
|
||||
/*
|
||||
* Set views folder path to directory where template files are stored
|
||||
*/
|
||||
server.set('views', DIST_FOLDER);
|
||||
|
||||
/**
|
||||
* Proxy the sitemaps
|
||||
*/
|
||||
server.use('/sitemap**', createProxyMiddleware({ target: `${environment.rest.baseUrl}/sitemaps`, changeOrigin: true }));
|
||||
|
||||
// TODO: implement data requests securely
|
||||
server.get('/api/**', (req, res) => {
|
||||
res.status(404).send('data requests are not yet supported');
|
||||
});
|
||||
|
||||
/*
|
||||
* Serve static resources (images, i18n messages, …)
|
||||
*/
|
||||
server.get('*.*', cacheControl, express.static(DIST_FOLDER, { index: false }));
|
||||
|
||||
// Register the ngApp callback function to handle incoming requests
|
||||
server.get('*', ngApp);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
/*
|
||||
* Enable request logging
|
||||
* See [morgan](https://github.com/expressjs/morgan)
|
||||
*/
|
||||
app.use(morgan('dev'));
|
||||
|
||||
/*
|
||||
* Add cookie parser middleware
|
||||
* See [morgan](https://github.com/expressjs/cookie-parser)
|
||||
*/
|
||||
app.use(cookieParser());
|
||||
|
||||
/*
|
||||
* Add parser for request bodies
|
||||
* See [morgan](https://github.com/expressjs/body-parser)
|
||||
*/
|
||||
app.use(bodyParser.json());
|
||||
|
||||
/*
|
||||
* Render html pages by running angular server side
|
||||
*/
|
||||
app.engine('html', (_, options, callback) =>
|
||||
ngExpressEngine({
|
||||
bootstrap: ServerAppModuleNgFactory,
|
||||
providers: [
|
||||
{
|
||||
provide: REQUEST,
|
||||
useValue: (options as any).req,
|
||||
},
|
||||
{
|
||||
provide: RESPONSE,
|
||||
useValue: (options as any).req.res,
|
||||
},
|
||||
provideModuleMap(LAZY_MODULE_MAP)
|
||||
],
|
||||
})(_, (options as any), callback)
|
||||
);
|
||||
|
||||
/*
|
||||
* Register the view engines for html and ejs
|
||||
*/
|
||||
app.set('view engine', 'html');
|
||||
|
||||
/*
|
||||
* Set views folder path to directory where template files are stored
|
||||
*/
|
||||
app.set('views', DIST_FOLDER);
|
||||
|
||||
/**
|
||||
* Proxy the sitemaps
|
||||
*/
|
||||
app.use('/sitemap**', createProxyMiddleware({ target: `${environment.rest.baseUrl}/sitemaps`, changeOrigin: true }));
|
||||
|
||||
/*
|
||||
* Adds a cache control header to the response
|
||||
* The cache control value can be configured in the environments file and defaults to max-age=60
|
||||
*/
|
||||
function cacheControl(req, res, next) {
|
||||
// instruct browser to revalidate
|
||||
res.header('Cache-Control', environment.cache.control || 'max-age=60');
|
||||
next();
|
||||
}
|
||||
|
||||
/*
|
||||
* Serve static resources (images, i18n messages, …)
|
||||
*/
|
||||
app.get('*.*', cacheControl, express.static(DIST_FOLDER, { index: false }));
|
||||
|
||||
/*
|
||||
* The callback function to serve server side angular
|
||||
*/
|
||||
function ngApp(req, res) {
|
||||
if (environment.universal.preboot) {
|
||||
res.render(DIST_FOLDER + '/index.html', {
|
||||
res.render(indexHtml, {
|
||||
req,
|
||||
res,
|
||||
preboot: environment.universal.preboot,
|
||||
@@ -139,7 +146,8 @@ function ngApp(req, res) {
|
||||
time: environment.universal.time,
|
||||
baseUrl: environment.ui.nameSpace,
|
||||
originUrl: environment.ui.baseUrl,
|
||||
requestUrl: req.originalUrl
|
||||
requestUrl: req.originalUrl,
|
||||
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }]
|
||||
}, (err, data) => {
|
||||
if (hasNoValue(err) && hasValue(data)) {
|
||||
res.send(data);
|
||||
@@ -150,16 +158,23 @@ function ngApp(req, res) {
|
||||
}
|
||||
res.sendFile(DIST_FOLDER + '/index.html');
|
||||
}
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// If preboot is disabled, just serve the client
|
||||
console.log('Universal off, serving for direct CSR');
|
||||
res.sendFile(DIST_FOLDER + '/index.html');
|
||||
res.sendFile(indexHtml);
|
||||
}
|
||||
}
|
||||
|
||||
// Register the ngApp callback function to handle incoming requests
|
||||
app.get('*', ngApp);
|
||||
/*
|
||||
* Adds a cache control header to the response
|
||||
* The cache control value can be configured in the environments file and defaults to max-age=60
|
||||
*/
|
||||
function cacheControl(req, res, next) {
|
||||
// instruct browser to revalidate
|
||||
res.header('Cache-Control', environment.cache.control || 'max-age=60');
|
||||
next();
|
||||
}
|
||||
|
||||
/*
|
||||
* Callback function for when the server has started
|
||||
@@ -181,6 +196,17 @@ function createHttpsServer(keys) {
|
||||
});
|
||||
}
|
||||
|
||||
function run() {
|
||||
const port = environment.ui.port || 4000;
|
||||
const host = environment.ui.host || '/';
|
||||
|
||||
// Start up the Node server
|
||||
const server = app();
|
||||
server.listen(port, host, () => {
|
||||
serverStarted();
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* If SSL is enabled
|
||||
* - Read credentials from configuration files
|
||||
@@ -220,7 +246,7 @@ if (environment.ui.ssl) {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
app.listen(environment.ui.port, environment.ui.host, () => {
|
||||
serverStarted();
|
||||
});
|
||||
run();
|
||||
}
|
||||
|
||||
export * from './src/main.server';
|
||||
|
@@ -16,3 +16,4 @@ if (environment.production) {
|
||||
|
||||
export { ServerAppModule } from './modules/app/server-app.module';
|
||||
export { ngExpressEngine } from '@nguniversal/express-engine';
|
||||
export { renderModule, renderModuleFactory } from '@angular/platform-server';
|
||||
|
@@ -2,7 +2,6 @@
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./out-tsc/app",
|
||||
"module": "esnext",
|
||||
"types": []
|
||||
},
|
||||
"files": [
|
||||
|
@@ -5,7 +5,7 @@
|
||||
"rewriteTsconfig": false
|
||||
},
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"baseUrl": "./",
|
||||
"outDir": "./dist/out-tsc",
|
||||
"declaration": false,
|
||||
"downlevelIteration": true,
|
||||
@@ -32,7 +32,8 @@
|
||||
"es6",
|
||||
"es2015",
|
||||
"es2016",
|
||||
"es2017"
|
||||
"es2017",
|
||||
"es2018"
|
||||
],
|
||||
"types": [
|
||||
"node",
|
||||
@@ -41,6 +42,7 @@
|
||||
},
|
||||
"angularCompilerOptions": {
|
||||
"fullTemplateTypeCheck": true,
|
||||
"strictInjectionParameters": true
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true
|
||||
}
|
||||
}
|
||||
|
@@ -2,8 +2,12 @@
|
||||
"extends": "./tsconfig.app.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./out-tsc/app-server",
|
||||
"module": "esnext"
|
||||
"module": "commonjs"
|
||||
},
|
||||
"files": [
|
||||
"src/main.server.ts",
|
||||
"server.ts"
|
||||
],
|
||||
"angularCompilerOptions": {
|
||||
"entryModule": "./src/modules/app/server-app.module#ServerAppModule"
|
||||
}
|
||||
|
Reference in New Issue
Block a user