-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.ts
259 lines (253 loc) · 7.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* eslint-disable consistent-return */
import path from 'path';
import fs from 'fs-extra';
import { parseBuildPerf } from '../utils';
import externalHelpers from '../externalHelpers';
import { HookOptions } from './types';
import prepareShortcodeParser from '../utils/prepareShortcodeParser';
import { displayPerfTimings } from '../utils/perf';
const hooks: Array<HookOptions> = [
{
hook: 'bootstrap',
name: 'elderAddExternalHelpers',
description: 'Adds external helpers to helpers object',
priority: 1,
run: async ({ helpers, query, settings }) => {
let additionalHelpers = {};
try {
additionalHelpers = await externalHelpers({ helpers, query, settings });
} catch (err) {
console.error(err);
}
if (additionalHelpers) {
return {
helpers: {
...helpers,
...additionalHelpers,
},
};
}
},
},
{
hook: 'middleware',
name: 'elderExpressLikeMiddleware',
description: 'An express like middleware so requests can be served by Elder.js',
priority: 1,
run: async ({ req, next, res, request, router }) => {
return router({ req, res, next, request });
},
},
{
hook: 'shortcodes',
name: 'elderProcessShortcodes',
description:
"Builds the shortcode parser, parses shortcodes from the html returned by the route's html and appends anything needed to the stacks.",
priority: 50,
run: async ({
helpers,
data,
settings,
request,
query,
cssStack,
headStack,
customJsStack,
layoutHtml,
shortcodes,
allRequests,
perf,
}) => {
const ShortcodeParser = prepareShortcodeParser({
perf,
shortcodes,
helpers,
data,
settings,
request,
query,
cssStack,
headStack,
customJsStack,
allRequests,
});
const html = await ShortcodeParser.parse(layoutHtml);
return {
layoutHtml: html,
headStack,
cssStack,
customJsStack,
};
},
},
{
hook: 'stacks',
name: 'elderAddMetaCharsetToHead',
description: `Adds <meta charset="UTF-8" /> to the head.`,
priority: 100,
run: async ({ headStack }) => {
return {
headStack: [
...headStack,
{
source: 'elderAddMetaCharsetToHead',
string: `<meta charset="UTF-8" />`,
priority: 100,
},
],
};
},
},
{
hook: 'stacks',
name: 'elderAddMetaViewportToHead',
description: `Adds <meta name="viewport" content="width=device-width, initial-scale=1" /> to the head.`,
priority: 90,
run: async ({ headStack }) => {
return {
headStack: [
...headStack,
{
source: 'elderAddMetaViewportToHead',
string: `<meta name="viewport" content="width=device-width, initial-scale=1" />`,
priority: 90,
},
],
};
},
},
{
hook: 'stacks',
name: 'elderAddCssFileToHead',
description: `Adds the css found in the svelte files to the head if 'css' in your 'elder.config.js' file is set to 'file'.`,
priority: 100,
run: async ({ headStack, settings }) => {
if (settings.css === 'file' && settings.$$internal.publicCssFile) {
return {
headStack: [
...headStack,
{
source: 'elderAddCssFileToHead',
string: `<link rel="stylesheet" href="${settings.$$internal.publicCssFile}" media="all" />`,
priority: 30,
},
],
};
}
if (settings.css === 'lazy' && settings.$$internal.publicCssFile) {
return {
headStack: [
...headStack,
{
source: 'elderAddCssFileToHead',
string: `<link rel="preload" href="${settings.$$internal.publicCssFile}" as="style" /><link rel="stylesheet" href="${settings.$$internal.publicCssFile}" media="print" onload="this.media='all'" /><noscript><link rel="stylesheet" href="${settings.$$internal.publicCssFile}" media="all" /></noscript>`,
priority: 30,
},
],
};
}
},
},
{
hook: 'compileHtml',
name: 'elderCompileHtml',
description: 'Creates an HTML string out of the Svelte layout and stacks.',
priority: 50,
run: async ({
request,
settings,
htmlAttributesString,
bodyAttributesString,
headString,
footerString,
layoutHtml,
}) => {
const htmlAttString =
htmlAttributesString && htmlAttributesString.length > 0 ? htmlAttributesString : `lang="${settings.lang}"`;
const bodyAttString =
bodyAttributesString && bodyAttributesString.length > 0 ? bodyAttributesString : `class="${request.route}"`;
return {
htmlString: `<!DOCTYPE html><html ${htmlAttString}><head>${headString}</head><body ${bodyAttString}>${layoutHtml}${footerString}</body></html>`,
};
},
},
{
hook: 'error',
name: 'elderConsoleLogErrors',
description: 'Log any errors to the console.',
priority: 1,
run: async ({ errors, request, settings }) => {
if (!settings.worker) {
console.error(request.permalink, errors);
}
},
},
{
hook: 'requestComplete',
name: 'elderWriteHtmlFileToPublic',
description: 'Write the html output to public.',
priority: 1,
run: async ({ settings, request, htmlString, errors }) => {
if (settings.build) {
const file = path.resolve(settings.distDir, `.${request.permalink}/index.html`);
try {
fs.outputFileSync(file, htmlString);
} catch (e) {
console.log(e);
return {
errors: [...errors, e],
};
}
}
return null;
},
},
{
hook: 'requestComplete',
name: 'elderDisplayRequestTime',
description: 'Page generating timings and logging.',
priority: 50,
run: async ({ timings, request, settings }) => {
if (!settings.build && process.env.NODE_ENV !== 'production') {
if (settings.debug.performance) {
console.log(`${Math.round(timings.slice(-1)[0].duration * 10) / 10}ms: \t ${request.permalink}`);
displayPerfTimings([...timings]);
} else {
console.log(request.req.path);
}
}
},
},
{
hook: 'buildComplete',
name: 'elderShowParsedBuildTimes',
description: 'A breakdown of the average times of different stages of the build.',
priority: 50,
run: async ({ timings, settings }) => {
if (settings.debug.performance) {
const buildTimings = parseBuildPerf(timings);
console.log(buildTimings);
}
},
},
{
hook: 'buildComplete',
name: 'elderWriteBuildErrors',
description: 'Writes out any errors of a build to a JSON file in the ___ELDER___ folder.',
priority: 50,
run: async ({ errors, settings }) => {
if (errors && errors.length > 0) {
const buildOutputLocation = path.resolve(settings.rootDir, `./___ELDER___/build-errors-${Date.now()}.json`);
console.log(
`Errors during Elder.js build. Writing details on the ${errors.length} build errors to: ${buildOutputLocation}`,
);
fs.writeJSONSync(buildOutputLocation, { settings, buildErrors: errors });
errors.forEach((error) => {
console.error(error);
console.error(`------`);
});
}
},
},
];
export default hooks;