-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
write_client_manifest.js
153 lines (122 loc) · 4.4 KB
/
write_client_manifest.js
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
import { relative_path, resolve_entry } from '../../utils/filesystem.js';
import { s } from '../../utils/misc.js';
import { dedent, write_if_changed } from './utils.js';
/**
* Writes the client manifest to disk. The manifest is used to power the router. It contains the
* list of routes and corresponding Svelte components (i.e. pages and layouts).
* @param {import('types').ValidatedKitConfig} kit
* @param {import('types').ManifestData} manifest_data
* @param {string} output
* @param {Array<{ has_server_load: boolean }>} [metadata]
*/
export function write_client_manifest(kit, manifest_data, output, metadata) {
/**
* Creates a module that exports a `CSRPageNode`
* @param {import('types').PageNode} node
*/
function generate_node(node) {
const declarations = [];
if (node.universal) {
declarations.push(
`import * as universal from ${s(relative_path(`${output}/nodes`, node.universal))};`,
`export { universal };`
);
}
if (node.component) {
declarations.push(
`export { default as component } from ${s(
relative_path(`${output}/nodes`, node.component)
)};`
);
}
return declarations.join('\n');
}
/** @type {Map<import('types').PageNode, number>} */
const indices = new Map();
const nodes = manifest_data.nodes
.map((node, i) => {
indices.set(node, i);
write_if_changed(`${output}/nodes/${i}.js`, generate_node(node));
return `() => import('./nodes/${i}')`;
})
.join(',\n');
const layouts_with_server_load = new Set();
const dictionary = dedent`
{
${manifest_data.routes
.map((route) => {
if (route.page) {
const errors = route.page.errors.slice(1).map((n) => n ?? '');
const layouts = route.page.layouts.slice(1).map((n) => n ?? '');
while (layouts.at(-1) === '') layouts.pop();
while (errors.at(-1) === '') errors.pop();
let leaf_has_server_load = false;
if (route.leaf) {
if (metadata) {
const i = /** @type {number} */ (indices.get(route.leaf));
leaf_has_server_load = metadata[i].has_server_load;
} else if (route.leaf.server) {
leaf_has_server_load = true;
}
}
// Encode whether or not the route uses server data
// using the ones' complement, to save space
const array = [`${leaf_has_server_load ? '~' : ''}${route.page.leaf}`];
// Encode whether or not the layout uses server data.
// It's a different method compared to pages because layouts
// are reused across pages, so we save space by doing it this way.
route.page.layouts.forEach((layout) => {
if (layout == undefined) return;
let layout_has_server_load = false;
if (metadata) {
layout_has_server_load = metadata[layout].has_server_load;
} else if (manifest_data.nodes[layout].server) {
layout_has_server_load = true;
}
if (layout_has_server_load) {
layouts_with_server_load.add(layout);
}
});
// only include non-root layout/error nodes if they exist
if (layouts.length > 0 || errors.length > 0) array.push(`[${layouts.join(',')}]`);
if (errors.length > 0) array.push(`[${errors.join(',')}]`);
return `${s(route.id)}: [${array.join(',')}]`;
}
})
.filter(Boolean)
.join(',\n')}
}
`;
const hooks_file = resolve_entry(kit.files.hooks.client);
write_if_changed(
`${output}/app.js`,
dedent`
${hooks_file ? `import * as client_hooks from '${relative_path(output, hooks_file)}';` : ''}
export { matchers } from './matchers.js';
export const nodes = [
${nodes}
];
export const server_loads = [${[...layouts_with_server_load].join(',')}];
export const dictionary = ${dictionary};
export const hooks = {
handleError: ${
hooks_file ? 'client_hooks.handleError || ' : ''
}(({ error }) => { console.error(error) }),
};
export { default as root } from '../root.svelte';
`
);
// write matchers to a separate module so that we don't
// need to worry about name conflicts
const imports = [];
const matchers = [];
for (const key in manifest_data.matchers) {
const src = manifest_data.matchers[key];
imports.push(`import { match as ${key} } from ${s(relative_path(output, src))};`);
matchers.push(key);
}
const module = imports.length
? `${imports.join('\n')}\n\nexport const matchers = { ${matchers.join(', ')} };`
: 'export const matchers = {};';
write_if_changed(`${output}/matchers.js`, module);
}