/
task-prerender.ts
49 lines (39 loc) · 1.39 KB
/
task-prerender.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
import type { BuildResultsComponentGraph, Config, Diagnostic } from '../declarations';
import type { CoreCompiler } from './load-compiler';
import { catchError } from '@utils';
import { startupCompilerLog } from './logs';
export const taskPrerender = async (coreCompiler: CoreCompiler, config: Config) => {
startupCompilerLog(coreCompiler, config);
const hydrateAppFilePath = config.flags.unknownArgs[0];
if (typeof hydrateAppFilePath !== 'string') {
config.logger.error(`Missing hydrate app script path`);
return config.sys.exit(1);
}
const srcIndexHtmlPath = config.srcIndexHtml;
const diagnostics = await runPrerenderTask(coreCompiler, config, hydrateAppFilePath, null, srcIndexHtmlPath);
config.logger.printDiagnostics(diagnostics);
if (diagnostics.some(d => d.level === 'error')) {
return config.sys.exit(1);
}
};
export const runPrerenderTask = async (
coreCompiler: CoreCompiler,
config: Config,
hydrateAppFilePath: string,
componentGraph: BuildResultsComponentGraph,
srcIndexHtmlPath: string,
) => {
const diagnostics: Diagnostic[] = [];
try {
const prerenderer = await coreCompiler.createPrerenderer(config);
const results = await prerenderer.start({
hydrateAppFilePath,
componentGraph,
srcIndexHtmlPath,
});
diagnostics.push(...results.diagnostics);
} catch (e) {
catchError(diagnostics, e);
}
return diagnostics;
};