-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
221 lines (181 loc) · 4.76 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
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
import fs from 'fs';
import path from 'path';
import { mkdirp } from '../filesystem/index.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);
}
}
const s = JSON.stringify;
/** @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 dir = `${output}/generated`;
const base = path.relative(cwd, dir);
write_if_changed(`${dir}/manifest.js`, generate_client_manifest(manifest_data, base));
write_if_changed(`${dir}/root.svelte`, generate_app(manifest_data, base));
}
/**
* @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, '');
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,
`[${route.parts.map((part) => `components[${component_indexes[part]}]`).join(', ')}]`,
params
]
.filter(Boolean)
.join(', ');
return `// ${route.parts[route.parts.length - 1]}\n\t[${tuple}]`;
} else {
return `// ${route.file}\n\t[${route.pattern}]`;
}
})
.join(',\n\n\t')}
]`.replace(/^\t/gm, '');
return trim(`
import * as layout from ${s(get_path(manifest_data.layout))};
const components = ${components};
const d = decodeURIComponent;
const empty = () => ({});
export const routes = ${routes};
export { layout };
`);
}
/**
* @param {ManifestData} manifest_data
* @param {string} base
*/
function generate_app(manifest_data, base) {
// TODO remove default layout altogether
const max_depth = Math.max(
...manifest_data.routes.map((route) =>
route.type === 'page' ? route.parts.filter(Boolean).length : 0
)
);
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-- > 1) {
pyramid = `
<svelte:component this={components[${l}]} {...(props_${l} || {})}>
{#if components[${l + 1}]}
${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
{/if}
</svelte:component>
`
.replace(/^\t\t\t/gm, '')
.trim();
}
const error_file = path.relative(base, manifest_data.error);
return trim(`
<!-- This file is generated by @sveltejs/kit — do not edit it! -->
<script>
import { setContext, afterUpdate, onMount } from 'svelte';
import ErrorComponent from ${s(error_file)};
// error handling
export let status = undefined;
export let error = undefined;
// stores
export let stores;
export let page;
export let components;
${levels.map((l) => `export let props_${l} = null;`).join('\n\t\t\t')}
const Layout = components[0];
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;
}
});
mounted = true;
return unsubscribe;
});
</script>
<Layout {...(props_0 || {})}>
{#if error}
<ErrorComponent {status} {error}/>
{:else}
${pyramid.replace(/\n/g, '\n\t\t\t\t')}
{/if}
</Layout>
{#if mounted}
<div id="svelte-announcer" aria-live="assertive" aria-atomic="true">
{#if navigated}
Navigated to {title}
{/if}
</div>
{/if}
<style>
#svelte-announcer {
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;
}
</style>
`);
}