This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathcreate_app.ts
295 lines (240 loc) · 8.66 KB
/
create_app.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as fs from 'fs';
import * as path from 'path';
import { posixify, stringify, walk, write_if_changed } from '../utils';
import { Page, PageComponent, ManifestData } from '../interfaces';
export function create_app({
bundler,
manifest_data,
dev_port,
dev,
cwd,
src,
dest,
routes,
output
}: {
bundler: string;
manifest_data: ManifestData;
dev_port?: number;
dev: boolean;
cwd: string;
src: string;
dest: string;
routes: string;
output: string;
}) {
if (!fs.existsSync(output)) fs.mkdirSync(output);
const path_to_routes = path.relative(`${output}/internal`, routes);
const client_manifest = generate_client_manifest(manifest_data, path_to_routes, bundler, dev, dev_port);
const server_manifest = generate_server_manifest(manifest_data, path_to_routes, cwd, src, dest, dev);
const app = generate_app(manifest_data, path_to_routes);
write_if_changed(`${output}/internal/manifest-client.mjs`, client_manifest);
write_if_changed(`${output}/internal/manifest-server.mjs`, server_manifest);
write_if_changed(`${output}/internal/App.svelte`, app);
}
export function create_serviceworker_manifest({ manifest_data, output, client_files, static_files }: {
manifest_data: ManifestData;
output: string;
client_files: string[];
static_files: string;
}) {
let files: string[] = ['service-worker-index.html'];
if (fs.existsSync(static_files)) {
files = files.concat(walk(static_files));
} else {
// TODO remove in a future version
if (fs.existsSync('assets')) {
throw new Error('As of Sapper 0.21, the assets/ directory should become static/');
}
}
const code = `
// This file is generated by Sapper — do not edit it!
export const timestamp = ${Date.now()};
export const files = [\n\t${files.map((x: string) => stringify('/' + x)).join(',\n\t')}\n];
export { files as assets }; // legacy
export const shell = [\n\t${client_files.map((x: string) => stringify('/' + x)).join(',\n\t')}\n];
export const routes = [\n\t${manifest_data.pages.map((r: Page) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
`.replace(/^\t\t/gm, '').trim();
write_if_changed(`${output}/service-worker.js`, code);
}
function create_param_match(param: string, i: number) {
return /^\.{3}.+$/.test(param)
? `${param.replace(/.{3}/, '')}: d(match[${i + 1}]).split('/')`
: `${param}: d(match[${i + 1}])`;
}
function generate_client_manifest(
manifest_data: ManifestData,
path_to_routes: string,
bundler: string,
dev: boolean,
dev_port?: number
) {
const page_ids = new Set(manifest_data.pages.map(page =>
page.pattern.toString()));
const server_routes_to_ignore = manifest_data.server_routes.filter(route =>
!page_ids.has(route.pattern.toString()));
const component_indexes: Record<string, number> = {};
const components = `[
${manifest_data.components.map((component, i) => {
const annotation = bundler === 'webpack'
? `/* webpackChunkName: "${component.name}" */ `
: '';
const source = get_file(path_to_routes, component);
component_indexes[component.name] = i;
return `{
js: () => import(${annotation}${stringify(source)})
}`;
}).join(',\n\t\t\t\t')}
]`.replace(/^\t/gm, '');
let needs_decode = false;
let routes = `[
${manifest_data.pages.map(page => `{
// ${page.parts[page.parts.length - 1].component.file}
pattern: ${page.pattern},
parts: [
${page.parts.map(part => {
const missing_layout = !part;
if (missing_layout) return 'null';
if (part.params.length > 0) {
needs_decode = true;
const props = part.params.map(create_param_match);
return `{ i: ${component_indexes[part.component.name]}, params: match => ({ ${props.join(', ')} }) }`;
}
return `{ i: ${component_indexes[part.component.name]} }`;
}).join(',\n\t\t\t\t\t\t')}
]
}`).join(',\n\n\t\t\t\t')}
]`.replace(/^\t/gm, '');
if (needs_decode) {
routes = `(d => ${routes})(decodeURIComponent)`;
}
return `
// This file is generated by Sapper — do not edit it!
// webpack does not support export * as root_comp yet so do a two line import/export
import * as root_comp from '${stringify(get_file(path_to_routes, manifest_data.root), false)}';
export { root_comp };
export { default as ErrorComponent } from '${stringify(get_file(path_to_routes, manifest_data.error), false)}';
export const ignore = [${server_routes_to_ignore.map(route => route.pattern).join(', ')}];
export const components = ${components};
export const routes = ${routes};
${dev ? `if (typeof window !== 'undefined') {
import(${stringify(posixify(path.resolve(__dirname, '../sapper-dev-client.js')))}).then(client => {
client.connect(${dev_port});
});
}` : ''}
`.replace(/^\t{2}/gm, '').trim();
}
function generate_server_manifest(
manifest_data: ManifestData,
path_to_routes: string,
cwd: string,
src: string,
dest: string,
dev: boolean
) {
const imports = [].concat(
manifest_data.server_routes.map((route, i) =>
`import * as route_${i} from ${stringify(posixify(`${path_to_routes}/${route.file}`))};`),
manifest_data.components.map((component, i) =>
`import * as component_${i} from ${stringify(get_file(path_to_routes, component))};`),
`import * as root_comp from ${stringify(get_file(path_to_routes, manifest_data.root))};`,
`import error from ${stringify(get_file(path_to_routes, manifest_data.error))};`
);
const component_lookup: Record<string, number> = {};
manifest_data.components.forEach((component, i) => {
component_lookup[component.name] = i;
});
const build_dir = posixify(path.normalize(path.relative(cwd, dest)));
const src_dir = posixify(path.normalize(path.relative(cwd, src)));
return `
// This file is generated by Sapper — do not edit it!
${imports.join('\n')}
const d = decodeURIComponent;
export const manifest = {
server_routes: [
${manifest_data.server_routes.map((route, i) => `{
// ${route.file}
pattern: ${route.pattern},
handlers: route_${i},
params: ${route.params.length > 0
? `match => ({ ${route.params.map(create_param_match).join(', ')} })`
: '() => ({})'}
}`).join(',\n\n\t\t\t\t')}
],
pages: [
${manifest_data.pages.map(page => `{
// ${page.parts[page.parts.length - 1].component.file}
pattern: ${page.pattern},
parts: [
${page.parts.map(part => {
if (part === null) return 'null';
const props = [
`name: "${part.component.name}"`,
`file: ${stringify(part.component.file)}`,
`component: component_${component_lookup[part.component.name]}`
].filter(Boolean);
if (part.params.length > 0) {
const params = part.params.map(create_param_match);
props.push(`params: match => ({ ${params.join(', ')} })`);
}
return `{ ${props.join(', ')} }`;
}).join(',\n\t\t\t\t\t\t')}
]
}`).join(',\n\n\t\t\t\t')}
],
root_comp,
error
};
export const build_dir = ${JSON.stringify(build_dir)};
export const src_dir = ${JSON.stringify(src_dir)};
export const dev = ${dev ? 'true' : 'false'};
`.replace(/^\t{2}/gm, '').trim();
}
function generate_app(manifest_data: ManifestData, path_to_routes: string) {
// TODO remove default layout altogether
const max_depth = Math.max(...manifest_data.pages.map(page => page.parts.filter(Boolean).length));
const levels = [];
for (let i = 0; i < max_depth; i += 1) {
levels.push(i + 1);
}
let l = max_depth;
let pyramid = `<svelte:component this="{level${l}.component}" {...level${l}.props}/>`;
while (l-- > 1) {
pyramid = `
<svelte:component this="{level${l}.component}" segment="{segments[${l}]}" {...level${l}.props}>
{#if level${l + 1}}
${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
{/if}
</svelte:component>
`.replace(/^\t\t\t/gm, '').trim();
}
return `
<!-- This file is generated by Sapper — do not edit it! -->
<script>
import { setContext, afterUpdate } from 'svelte';
import { CONTEXT_KEY } from './shared';
import Layout from '${get_file(path_to_routes, manifest_data.root)}';
import Error from '${get_file(path_to_routes, manifest_data.error)}';
export let stores;
export let error;
export let status;
export let segments;
export let level0;
${levels.map(l => `export let level${l} = null;`).join('\n\t\t\t')}
export let notify;
afterUpdate(notify);
setContext(CONTEXT_KEY, stores);
</script>
<Layout segment="{segments[0]}" {...level0.props}>
{#if error}
<Error {error} {status}/>
{:else}
${pyramid.replace(/\n/g, '\n\t\t\t\t')}
{/if}
</Layout>
`.replace(/^\t\t/gm, '').trim();
}
function get_file(path_to_routes: string, component: PageComponent) {
if (component.default) return `./${component.type}.svelte`;
return posixify(`${path_to_routes}/${component.file}`);
}