UNPKG

3.05 kBJavaScriptView Raw
1import { CommonEngine } from '@nguniversal/common/engine';
2import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
3
4/**
5 * @license
6 * Copyright Google LLC All Rights Reserved.
7 *
8 * Use of this source code is governed by an MIT-style license that can be
9 * found in the LICENSE file at https://angular.io/license
10 */
11/**
12 * This is an express engine for handling Angular Applications
13 */
14function ngExpressEngine(setupOptions) {
15 const engine = new CommonEngine(setupOptions.bootstrap, setupOptions.providers);
16 return function (filePath, options, callback) {
17 var _a, _b, _c, _d, _e, _f, _g, _h;
18 try {
19 const renderOptions = Object.assign({}, options);
20 if (!setupOptions.bootstrap && !renderOptions.bootstrap) {
21 throw new Error('You must pass in a NgModule to be bootstrapped');
22 }
23 const { req } = renderOptions;
24 const res = (_a = renderOptions.res) !== null && _a !== void 0 ? _a : req.res;
25 renderOptions.url =
26 (_b = renderOptions.url) !== null && _b !== void 0 ? _b : `${req.protocol}://${req.get('host') || ''}${req.baseUrl}${req.url}`;
27 renderOptions.documentFilePath = (_c = renderOptions.documentFilePath) !== null && _c !== void 0 ? _c : filePath;
28 renderOptions.providers = [...((_d = renderOptions.providers) !== null && _d !== void 0 ? _d : []), getReqResProviders(req, res)];
29 // eslint-disable-next-line @typescript-eslint/no-unused-expressions
30 renderOptions.publicPath =
31 (_f = (_e = renderOptions.publicPath) !== null && _e !== void 0 ? _e : setupOptions.publicPath) !== null && _f !== void 0 ? _f : (_g = options.settings) === null || _g === void 0 ? void 0 : _g.views;
32 renderOptions.inlineCriticalCss =
33 (_h = renderOptions.inlineCriticalCss) !== null && _h !== void 0 ? _h : setupOptions.inlineCriticalCss;
34 engine
35 .render(renderOptions)
36 .then((html) => callback(null, html))
37 .catch(callback);
38 }
39 catch (err) {
40 callback(err);
41 }
42 };
43}
44/**
45 * Get providers of the request and response
46 */
47function getReqResProviders(req, res) {
48 const providers = [
49 {
50 provide: REQUEST,
51 useValue: req,
52 },
53 ];
54 if (res) {
55 providers.push({
56 provide: RESPONSE,
57 useValue: res,
58 });
59 }
60 return providers;
61}
62
63/**
64 * @license
65 * Copyright Google LLC All Rights Reserved.
66 *
67 * Use of this source code is governed by an MIT-style license that can be
68 * found in the LICENSE file at https://angular.io/license
69 */
70
71/**
72 * @license
73 * Copyright Google LLC All Rights Reserved.
74 *
75 * Use of this source code is governed by an MIT-style license that can be
76 * found in the LICENSE file at https://angular.io/license
77 */
78
79/**
80 * Generated bundle index. Do not edit.
81 */
82
83export { ngExpressEngine };
84//# sourceMappingURL=express-engine.mjs.map