1 | import { CommonEngine } from '@nguniversal/common/engine';
|
2 | import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function 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 |
|
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 |
|
46 |
|
47 | function 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 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export { ngExpressEngine };
|
84 |
|