/
index.js
191 lines (156 loc) · 4.54 KB
/
index.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
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
import fs from 'fs';
import path from 'path';
import { s } from '../../utils/misc.js';
import { mkdirp } from '../../utils/filesystem.js';
/** @type {Map<string, string>} */
const previous_contents = new Map();
/**
* @param {string} file
* @param {string} code
*/
export function write_if_changed(file, code) {
if (code !== previous_contents.get(file)) {
previous_contents.set(file, code);
mkdirp(path.dirname(file));
fs.writeFileSync(file, code);
}
}
/** @typedef {import('types/internal').ManifestData} ManifestData */
/**
* @param {{
* manifest_data: ManifestData;
* output: string;
* cwd: string;
* }} options
*/
export function create_app({ manifest_data, output, cwd = process.cwd() }) {
const base = path.relative(cwd, output);
write_if_changed(`${output}/manifest.js`, generate_client_manifest(manifest_data, base));
write_if_changed(`${output}/root.svelte`, generate_app(manifest_data));
}
/**
* @param {string} str
*/
function trim(str) {
return str.replace(/^\t\t/gm, '').trim();
}
/**
* @param {ManifestData} manifest_data
* @param {string} base
*/
function generate_client_manifest(manifest_data, base) {
/** @type {Record<string, number>} */
const component_indexes = {};
/** @param {string} c */
const get_path = (c) => path.relative(base, c);
const components = `[
${manifest_data.components
.map((component, i) => {
component_indexes[component] = i;
return `() => import(${s(get_path(component))})`;
})
.join(',\n\t\t\t\t')}
]`.replace(/^\t/gm, '');
/** @param {string[]} parts */
const get_indices = (parts) =>
`[${parts.map((part) => (part ? `c[${component_indexes[part]}]` : '')).join(', ')}]`;
const routes = `[
${manifest_data.routes
.map((route) => {
if (route.type === 'page') {
const params =
route.params.length > 0 &&
'(m) => ({ ' +
route.params
.map((param, i) => {
return param.startsWith('...')
? `${param.slice(3)}: d(m[${i + 1}] || '')`
: `${param}: d(m[${i + 1}])`;
})
.join(', ') +
'})';
const tuple = [route.pattern, get_indices(route.a), get_indices(route.b)];
// optional items
if (params || route.shadow) tuple.push(params || 'null');
if (route.shadow) tuple.push('1');
return `// ${route.a[route.a.length - 1]}\n\t\t[${tuple.join(', ')}]`;
}
})
.filter(Boolean)
.join(',\n\n\t\t')}
]`.replace(/^\t/gm, '');
return trim(`
const c = ${components};
const d = decodeURIComponent;
export const routes = ${routes};
// we import the root layout/error components eagerly, so that
// connectivity errors after initialisation don't nuke the app
export const fallback = [c[0](), c[1]()];
`);
}
/**
* @param {ManifestData} manifest_data
*/
function generate_app(manifest_data) {
// TODO remove default layout altogether
const max_depth = Math.max(
...manifest_data.routes.map((route) =>
route.type === 'page' ? route.a.filter(Boolean).length : 0
),
1
);
const levels = [];
for (let i = 0; i <= max_depth; i += 1) {
levels.push(i);
}
let l = max_depth;
let pyramid = `<svelte:component this={components[${l}]} {...(props_${l} || {})}/>`;
while (l--) {
pyramid = `
{#if components[${l + 1}]}
<svelte:component this={components[${l}]} {...(props_${l} || {})}>
${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
</svelte:component>
{:else}
<svelte:component this={components[${l}]} {...(props_${l} || {})} />
{/if}
`
.replace(/^\t\t\t/gm, '')
.trim();
}
return trim(`
<!-- This file is generated by @sveltejs/kit — do not edit it! -->
<script>
import { setContext, afterUpdate, onMount } from 'svelte';
// stores
export let stores;
export let page;
export let components;
${levels.map((l) => `export let props_${l} = null;`).join('\n\t\t\t')}
setContext('__svelte__', stores);
$: stores.page.set(page);
afterUpdate(stores.page.notify);
let mounted = false;
let navigated = false;
let title = null;
onMount(() => {
const unsubscribe = stores.page.subscribe(() => {
if (mounted) {
navigated = true;
title = document.title || 'untitled page';
}
});
mounted = true;
return unsubscribe;
});
</script>
${pyramid.replace(/\n/g, '\n\t\t')}
{#if mounted}
<div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0; top: 0; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px">
{#if navigated}
{title}
{/if}
</div>
{/if}
`);
}