@sveltejs/kit 1.0.0-next.44 → 1.0.0-next.440

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/README.md +12 -9
  2. package/package.json +94 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +223 -0
  5. package/src/core/adapt/index.js +19 -0
  6. package/src/core/config/index.js +86 -0
  7. package/src/core/config/options.js +488 -0
  8. package/src/core/config/types.d.ts +1 -0
  9. package/src/core/constants.js +5 -0
  10. package/src/core/env.js +97 -0
  11. package/src/core/generate_manifest/index.js +78 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +380 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +450 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +33 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +92 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +91 -0
  23. package/src/core/sync/write_tsconfig.js +195 -0
  24. package/src/core/sync/write_types/index.js +577 -0
  25. package/src/core/sync/write_types/test/layout/+layout.js +5 -0
  26. package/src/core/sync/write_types/test/layout/+layout.server.js +5 -0
  27. package/src/core/sync/write_types/test/layout/+layout.svelte +0 -0
  28. package/src/core/sync/write_types/test/layout/+page.js +5 -0
  29. package/src/core/sync/write_types/test/layout/+page.server.js +5 -0
  30. package/src/core/sync/write_types/test/layout/+page.svelte +0 -0
  31. package/src/core/sync/write_types/test/layout/_expected/$types.d.ts +67 -0
  32. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.server.js +5 -0
  33. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.svelte +0 -0
  34. package/src/core/sync/write_types/test/layout-advanced/(main)/+page.js +5 -0
  35. package/src/core/sync/write_types/test/layout-advanced/(main)/+page@.svelte +0 -0
  36. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.js +5 -0
  37. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.svelte +0 -0
  38. package/src/core/sync/write_types/test/layout-advanced/+layout.js +5 -0
  39. package/src/core/sync/write_types/test/layout-advanced/+layout.svelte +0 -0
  40. package/src/core/sync/write_types/test/layout-advanced/_expected/$types.d.ts +32 -0
  41. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/$types.d.ts +42 -0
  42. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts +32 -0
  43. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.js +5 -0
  44. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.server.js +5 -0
  45. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.svelte +0 -0
  46. package/src/core/sync/write_types/test/simple-page-server-and-shared/_expected/$types.d.ts +44 -0
  47. package/src/core/sync/write_types/test/simple-page-server-only/+page.server.js +5 -0
  48. package/src/core/sync/write_types/test/simple-page-server-only/+page.svelte +0 -0
  49. package/src/core/sync/write_types/test/simple-page-server-only/_expected/$types.d.ts +30 -0
  50. package/src/core/sync/write_types/test/simple-page-shared-only/+page.js +5 -0
  51. package/src/core/sync/write_types/test/simple-page-shared-only/+page.svelte +0 -0
  52. package/src/core/sync/write_types/test/simple-page-shared-only/_expected/$types.d.ts +33 -0
  53. package/src/core/sync/write_types/test/slugs/+layout.js +1 -0
  54. package/src/core/sync/write_types/test/slugs/+layout.svelte +1 -0
  55. package/src/core/sync/write_types/test/slugs/[...rest]/+page.js +3 -0
  56. package/src/core/sync/write_types/test/slugs/[...rest]/+page.svelte +0 -0
  57. package/src/core/sync/write_types/test/slugs/[slug]/+page.js +3 -0
  58. package/src/core/sync/write_types/test/slugs/[slug]/+page.svelte +0 -0
  59. package/src/core/sync/write_types/test/slugs/_expected/$types.d.ts +32 -0
  60. package/src/core/sync/write_types/test/slugs/_expected/[...rest]/$types.d.ts +29 -0
  61. package/src/core/sync/write_types/test/slugs/_expected/[slug]/$types.d.ts +29 -0
  62. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.js +1 -0
  63. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.svelte +1 -0
  64. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/$types.d.ts +30 -0
  65. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/$types.d.ts +32 -0
  66. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[...rest]/$types.d.ts +36 -0
  67. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[slug]/$types.d.ts +17 -0
  68. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.js +1 -0
  69. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.svelte +1 -0
  70. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.js +3 -0
  71. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.svelte +0 -0
  72. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[slug]/+page@.svelte +0 -0
  73. package/src/core/utils.js +70 -0
  74. package/src/hooks.js +26 -0
  75. package/src/index/index.js +45 -0
  76. package/src/index/private.js +33 -0
  77. package/src/node/index.js +145 -0
  78. package/src/node/polyfills.js +40 -0
  79. package/src/runtime/app/env.js +11 -0
  80. package/src/runtime/app/navigation.js +22 -0
  81. package/src/runtime/app/paths.js +1 -0
  82. package/src/runtime/app/stores.js +102 -0
  83. package/src/runtime/client/ambient.d.ts +18 -0
  84. package/src/runtime/client/client.js +1362 -0
  85. package/src/runtime/client/fetcher.js +60 -0
  86. package/src/runtime/client/parse.js +41 -0
  87. package/src/runtime/client/singletons.js +21 -0
  88. package/src/runtime/client/start.js +46 -0
  89. package/src/runtime/client/types.d.ts +86 -0
  90. package/src/runtime/client/utils.js +113 -0
  91. package/src/runtime/components/error.svelte +16 -0
  92. package/{assets → src/runtime}/components/layout.svelte +0 -0
  93. package/src/runtime/env/dynamic/private.js +1 -0
  94. package/src/runtime/env/dynamic/public.js +1 -0
  95. package/src/runtime/env-private.js +7 -0
  96. package/src/runtime/env-public.js +7 -0
  97. package/src/runtime/env.js +6 -0
  98. package/src/runtime/hash.js +16 -0
  99. package/src/runtime/paths.js +11 -0
  100. package/src/runtime/server/endpoint.js +50 -0
  101. package/src/runtime/server/index.js +489 -0
  102. package/src/runtime/server/page/cookie.js +25 -0
  103. package/src/runtime/server/page/crypto.js +239 -0
  104. package/src/runtime/server/page/csp.js +249 -0
  105. package/src/runtime/server/page/fetch.js +266 -0
  106. package/src/runtime/server/page/index.js +408 -0
  107. package/src/runtime/server/page/load_data.js +168 -0
  108. package/src/runtime/server/page/render.js +361 -0
  109. package/src/runtime/server/page/respond_with_error.js +95 -0
  110. package/src/runtime/server/page/types.d.ts +44 -0
  111. package/src/runtime/server/utils.js +116 -0
  112. package/src/utils/array.js +9 -0
  113. package/src/utils/error.js +22 -0
  114. package/src/utils/escape.js +104 -0
  115. package/src/utils/filesystem.js +108 -0
  116. package/src/utils/functions.js +16 -0
  117. package/src/utils/http.js +55 -0
  118. package/src/utils/misc.js +1 -0
  119. package/src/utils/routing.js +146 -0
  120. package/src/utils/url.js +142 -0
  121. package/src/vite/build/build_server.js +348 -0
  122. package/src/vite/build/build_service_worker.js +90 -0
  123. package/src/vite/build/utils.js +160 -0
  124. package/src/vite/dev/index.js +543 -0
  125. package/src/vite/index.js +578 -0
  126. package/src/vite/preview/index.js +186 -0
  127. package/src/vite/types.d.ts +3 -0
  128. package/src/vite/utils.js +345 -0
  129. package/svelte-kit.js +1 -1
  130. package/types/ambient.d.ts +366 -0
  131. package/types/index.d.ts +345 -0
  132. package/types/internal.d.ts +374 -0
  133. package/types/private.d.ts +209 -0
  134. package/CHANGELOG.md +0 -437
  135. package/assets/components/error.svelte +0 -13
  136. package/assets/runtime/app/env.js +0 -5
  137. package/assets/runtime/app/navigation.js +0 -41
  138. package/assets/runtime/app/paths.js +0 -1
  139. package/assets/runtime/app/stores.js +0 -93
  140. package/assets/runtime/chunks/utils.js +0 -19
  141. package/assets/runtime/internal/singletons.js +0 -23
  142. package/assets/runtime/internal/start.js +0 -770
  143. package/assets/runtime/paths.js +0 -12
  144. package/dist/chunks/index.js +0 -3521
  145. package/dist/chunks/index2.js +0 -587
  146. package/dist/chunks/index3.js +0 -246
  147. package/dist/chunks/index4.js +0 -545
  148. package/dist/chunks/index5.js +0 -761
  149. package/dist/chunks/index6.js +0 -322
  150. package/dist/chunks/standard.js +0 -99
  151. package/dist/chunks/utils.js +0 -83
  152. package/dist/cli.js +0 -546
  153. package/dist/ssr.js +0 -2580
@@ -1,545 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { r as rimraf, c as copy_assets } from './utils.js';
4
- import { c as create_manifest_data, a as create_app } from './index2.js';
5
- import vite from 'vite';
6
- import svelte from '@svitejs/vite-plugin-svelte';
7
- import '../cli.js';
8
- import 'sade';
9
- import 'url';
10
- import './standard.js';
11
-
12
- /** @param {any} value */
13
- const s = (value) => JSON.stringify(value);
14
-
15
- /** @typedef {Record<string, {
16
- * file: string;
17
- * css: string[];
18
- * imports: string[];
19
- * }>} ClientManifest */
20
-
21
- /**
22
- * @param {import('../../types').ValidatedConfig} config
23
- * @param {{
24
- * cwd?: string;
25
- * runtime?: string;
26
- * }} [opts]
27
- */
28
- async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/kit/ssr' } = {}) {
29
- const build_dir = path.resolve(cwd, '.svelte/build');
30
-
31
- rimraf(build_dir);
32
-
33
- const options = {
34
- cwd,
35
- config,
36
- build_dir,
37
- base:
38
- config.kit.paths.assets === '/.'
39
- ? `/${config.kit.appDir}/`
40
- : `${config.kit.paths.assets}/${config.kit.appDir}/`,
41
- manifest: create_manifest_data({
42
- config,
43
- output: build_dir,
44
- cwd
45
- }),
46
- output_dir: path.resolve(cwd, '.svelte/output'),
47
- client_entry_file: '.svelte/build/runtime/internal/start.js',
48
- service_worker_entry_file: resolve_entry(config.kit.files.serviceWorker)
49
- };
50
-
51
- const client_manifest = await build_client(options);
52
- await build_server(options, client_manifest, runtime);
53
-
54
- if (options.service_worker_entry_file) {
55
- const { base, assets } = config.kit.paths;
56
-
57
- if (assets !== base && assets !== '/.') {
58
- throw new Error('Cannot use service worker alongside config.kit.paths.assets');
59
- }
60
-
61
- await build_service_worker(options, client_manifest);
62
- }
63
- }
64
-
65
- /**
66
- * @param {{
67
- * cwd: string;
68
- * base: string;
69
- * config: import('../../types').ValidatedConfig
70
- * manifest: import('../../types').ManifestData
71
- * build_dir: string;
72
- * output_dir: string;
73
- * client_entry_file: string;
74
- * service_worker_entry_file: string;
75
- * }} options
76
- */
77
- async function build_client({
78
- cwd,
79
- base,
80
- config,
81
- manifest,
82
- build_dir,
83
- output_dir,
84
- client_entry_file,
85
- service_worker_entry_file
86
- }) {
87
- create_app({
88
- manifest_data: manifest,
89
- output: build_dir,
90
- cwd
91
- });
92
-
93
- copy_assets(build_dir);
94
-
95
- process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : '';
96
- process.env.VITE_SVELTEKIT_SERVICE_WORKER = service_worker_entry_file ? '/service-worker.js' : '';
97
-
98
- const client_out_dir = `${output_dir}/client/${config.kit.appDir}`;
99
- const client_manifest_file = `${client_out_dir}/manifest.json`;
100
-
101
- /** @type {Record<string, string>} */
102
- const input = {
103
- start: path.resolve(cwd, client_entry_file)
104
- };
105
-
106
- manifest.pages.forEach((page) => {
107
- page.parts.forEach((file) => {
108
- const resolved = path.resolve(cwd, file);
109
- const relative = path.relative(config.kit.files.routes, resolved);
110
- input[path.join('pages', relative)] = resolved;
111
- });
112
- });
113
-
114
- manifest.endpoints.forEach((endpoint) => {
115
- const resolved = path.resolve(cwd, endpoint.file);
116
- const relative = path.relative(config.kit.files.routes, resolved);
117
- input[path.join('endpoints', relative)] = resolved;
118
- });
119
-
120
- // client build
121
- await vite.build({
122
- root: cwd,
123
- base,
124
- build: {
125
- cssCodeSplit: true,
126
- manifest: true,
127
- lib: {
128
- // TODO i'm not convinced this block is necessary if we're
129
- // providing inputs explicitly via rollupOptions, but without
130
- // it Vite complains about the dynamic import polyfill
131
- entry: client_entry_file,
132
- name: 'app',
133
- formats: ['es']
134
- },
135
- outDir: client_out_dir,
136
- rollupOptions: {
137
- input,
138
- output: {
139
- entryFileNames: '[name]-[hash].js',
140
- chunkFileNames: 'chunks/[name]-[hash].js',
141
- assetFileNames: 'assets/[name]-[hash][extname]'
142
- },
143
- preserveEntrySignatures: 'strict'
144
- }
145
- },
146
- resolve: {
147
- alias: {
148
- $app: path.resolve(`${build_dir}/runtime/app`)
149
- }
150
- },
151
- plugins: [
152
- svelte({
153
- emitCss: true,
154
- compilerOptions: {
155
- dev: true,
156
- hydratable: true
157
- },
158
- hot: true,
159
- extensions: config.extensions
160
- })
161
- ]
162
- });
163
-
164
- /** @type {ClientManifest} */
165
- const client_manifest = JSON.parse(fs.readFileSync(client_manifest_file, 'utf-8'));
166
- fs.unlinkSync(client_manifest_file);
167
-
168
- return client_manifest;
169
- }
170
-
171
- /**
172
- * @param {{
173
- * cwd: string;
174
- * base: string;
175
- * config: import('../../types').ValidatedConfig
176
- * manifest: import('../../types').ManifestData
177
- * build_dir: string;
178
- * output_dir: string;
179
- * client_entry_file: string;
180
- * service_worker_entry_file: string;
181
- * }} options
182
- * @param {ClientManifest} client_manifest
183
- * @param {string} runtime
184
- */
185
- async function build_server(
186
- { cwd, base, config, manifest, build_dir, output_dir, client_entry_file },
187
- client_manifest,
188
- runtime
189
- ) {
190
- let setup_file = resolve_entry(config.kit.files.setup);
191
- if (!fs.existsSync(setup_file)) {
192
- setup_file = path.resolve(cwd, '.svelte/build/setup.js');
193
- fs.writeFileSync(setup_file, '');
194
- }
195
-
196
- const app_file = `${build_dir}/app.js`;
197
-
198
- /** @type {(file: string) => string} */
199
- const app_relative = (file) => {
200
- const relative_file = path.relative(build_dir, path.resolve(cwd, file));
201
- return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
202
- };
203
-
204
- const component_indexes = new Map();
205
- manifest.components.forEach((c, i) => {
206
- component_indexes.set(c, i);
207
- });
208
-
209
- /** @param {string} c */
210
- const stringify_component = (c) => `() => import(${s(`${app_relative(c)}`)})`;
211
-
212
- const entry = `${config.kit.paths.assets}/${config.kit.appDir}/${client_manifest[client_entry_file].file}`;
213
-
214
- /** @type {Set<string>} */
215
- const common_js_deps = new Set();
216
-
217
- /** @type {Set<string>} */
218
- const common_css_deps = new Set();
219
-
220
- /** @type {Map<string, Set<string>>} */
221
- const js_deps_by_file = new Map();
222
-
223
- /** @type {Map<string, Set<string>>} */
224
- const css_deps_by_file = new Map();
225
-
226
- /**
227
- * @param {string} file
228
- * @param {Set<string>} js_deps
229
- * @param {Set<string>} css_deps
230
- */
231
- function find_deps(file, js_deps, css_deps) {
232
- const chunk = client_manifest[file];
233
-
234
- js_deps.add(chunk.file);
235
-
236
- if (chunk.css) {
237
- chunk.css.forEach((file) => css_deps.add(file));
238
- }
239
-
240
- if (chunk.imports) {
241
- chunk.imports.forEach((file) => find_deps(file, js_deps, css_deps));
242
- }
243
- }
244
-
245
- find_deps(client_entry_file, common_js_deps, common_css_deps);
246
-
247
- // TODO ideally we wouldn't embed the css_lookup, but this is the easiest
248
- // way to be able to inline CSS into AMP documents. if we come up with
249
- // something better, we could use it for non-AMP documents too, as
250
- // critical CSS below a certain threshold _should_ be inlined
251
-
252
- /** @type {Record<string, string>} */
253
- const amp_css_lookup = {};
254
-
255
- [client_entry_file, ...manifest.components].forEach((file) => {
256
- const js_deps = new Set();
257
- const css_deps = new Set();
258
-
259
- js_deps_by_file.set(file, js_deps);
260
- css_deps_by_file.set(file, css_deps);
261
-
262
- find_deps(file, js_deps, css_deps);
263
-
264
- css_deps.forEach((file) => {
265
- const resolved = `${output_dir}/client/${config.kit.appDir}/${file}`;
266
- const contents = fs.readFileSync(resolved, 'utf-8');
267
-
268
- amp_css_lookup[file] = contents;
269
- });
270
- });
271
-
272
- config.kit.paths.assets === '/.'
273
- ? `/${config.kit.appDir}`
274
- : `${config.kit.paths.assets}/${config.kit.appDir}`;
275
-
276
- // prettier-ignore
277
- fs.writeFileSync(
278
- app_file,
279
- `
280
- import { ssr } from '${runtime}';
281
- import root from './generated/root.svelte';
282
- import { set_paths } from './runtime/paths.js';
283
- import * as setup from ${s(app_relative(setup_file))};
284
-
285
- const template = ({ head, body }) => ${s(fs.readFileSync(config.kit.files.template, 'utf-8'))
286
- .replace('%svelte.head%', '" + head + "')
287
- .replace('%svelte.body%', '" + body + "')};
288
-
289
- set_paths(${s(config.kit.paths)});
290
-
291
- // allow paths to be overridden in svelte-kit start
292
- export function init({ paths }) {
293
- set_paths(paths);
294
- }
295
-
296
- const d = decodeURIComponent;
297
- const empty = () => ({});
298
-
299
- const components = [
300
- ${manifest.components.map((c) => stringify_component(c)).join(',\n\t\t\t\t')}
301
- ];
302
-
303
- ${config.kit.amp ? `
304
- const amp_css_lookup = ${s(amp_css_lookup)};` : ''}
305
-
306
- const manifest = {
307
- assets: ${s(manifest.assets)},
308
- layout: ${stringify_component(manifest.layout)},
309
- error: ${stringify_component(manifest.error)},
310
- pages: [
311
- ${manifest.pages
312
- .map((data) => {
313
- const params = get_params(data.params);
314
- const parts = data.parts.map(c => `components[${component_indexes.get(c)}]`);
315
-
316
- const js_deps = new Set(common_js_deps);
317
- const css_deps = new Set(common_css_deps);
318
-
319
- for (const file of data.parts) {
320
- js_deps_by_file.get(file).forEach(asset => {
321
- js_deps.add(asset);
322
- });
323
-
324
- css_deps_by_file.get(file).forEach(asset => {
325
- css_deps.add(asset);
326
- });
327
- }
328
-
329
- return `{
330
- pattern: ${data.pattern},
331
- params: ${params},
332
- parts: [${parts.join(', ')}],
333
- css: [${Array.from(css_deps).map(s).join(', ')}],
334
- js: [${Array.from(js_deps).map(s).join(', ')}]
335
- }`;
336
- })
337
- .join(',\n\t\t\t\t\t')}
338
- ],
339
- endpoints: [
340
- ${manifest.endpoints
341
- .map((data) => {
342
- const params = get_params(data.params);
343
- const load = `() => import(${s(app_relative(data.file))})`;
344
-
345
- return `{ pattern: ${data.pattern}, params: ${params}, load: ${load} }`;
346
- })
347
- .join(',\n\t\t\t\t\t')}
348
- ]
349
- };
350
-
351
- export function render(request, {
352
- paths = ${s(config.kit.paths)},
353
- local = false,
354
- only_prerender = false,
355
- get_static_file
356
- } = {}) {
357
- return ssr(request, {
358
- paths,
359
- local,
360
- template,
361
- manifest,
362
- target: ${s(config.kit.target)},${
363
- config.kit.startGlobal ? `\n\t\t\t\t\tstart_global: ${s(config.kit.startGlobal)},` : ''
364
- }
365
- entry: ${s(entry)},
366
- root,
367
- setup,
368
- dev: false,
369
- amp: ${config.kit.amp},
370
- only_prerender,
371
- app_dir: ${s(config.kit.appDir)},
372
- host: ${s(config.kit.host)},
373
- host_header: ${s(config.kit.hostHeader)},
374
- get_stack: error => error.stack,
375
- get_static_file,
376
- get_amp_css: dep => amp_css_lookup[dep]
377
- });
378
- }
379
- `
380
- .replace(/^\t{3}/gm, '')
381
- .trim()
382
- );
383
-
384
- await vite.build({
385
- root: cwd,
386
- base,
387
- build: {
388
- ssr: true,
389
- lib: {
390
- entry: app_file,
391
- name: 'app',
392
- formats: ['es']
393
- },
394
- outDir: `${output_dir}/server`
395
- },
396
- resolve: {
397
- alias: {
398
- $app: path.resolve(`${build_dir}/runtime/app`)
399
- }
400
- },
401
- plugins: [
402
- svelte({
403
- emitCss: true,
404
- compilerOptions: {
405
- dev: true,
406
- hydratable: true
407
- },
408
- hot: true,
409
- extensions: config.extensions
410
- })
411
- ],
412
- ssr: {
413
- noExternal: ['svelte']
414
- },
415
- optimizeDeps: {
416
- entries: []
417
- }
418
- });
419
- }
420
-
421
- /**
422
- * @param {{
423
- * cwd: string;
424
- * base: string;
425
- * config: import('../../types').ValidatedConfig
426
- * manifest: import('../../types').ManifestData
427
- * build_dir: string;
428
- * output_dir: string;
429
- * client_entry_file: string;
430
- * service_worker_entry_file: string;
431
- * }} options
432
- * @param {ClientManifest} client_manifest
433
- */
434
- async function build_service_worker(
435
- { cwd, base, config, manifest, build_dir, output_dir, service_worker_entry_file },
436
- client_manifest
437
- ) {
438
- // TODO add any assets referenced in template .html file, e.g. favicon?
439
- const app_files = new Set();
440
- for (const key in client_manifest) {
441
- const { file, css } = client_manifest[key];
442
- app_files.add(file);
443
- if (css) {
444
- css.forEach((file) => {
445
- app_files.add(file);
446
- });
447
- }
448
- }
449
-
450
- fs.writeFileSync(
451
- `${build_dir}/runtime/service-worker.js`,
452
- `
453
- export const timestamp = ${Date.now()};
454
-
455
- export const build = [
456
- ${Array.from(app_files)
457
- .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
458
- .join(',\n\t\t\t\t')}
459
- ];
460
-
461
- export const assets = [
462
- ${manifest.assets
463
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
464
- .join(',\n\t\t\t\t')}
465
- ];
466
- `
467
- .replace(/^\t{3}/gm, '')
468
- .trim()
469
- );
470
-
471
- await vite.build({
472
- root: cwd,
473
- base,
474
- build: {
475
- lib: {
476
- entry: service_worker_entry_file,
477
- name: 'app',
478
- formats: ['es']
479
- },
480
- rollupOptions: {
481
- output: {
482
- entryFileNames: 'service-worker.js'
483
- }
484
- },
485
- outDir: `${output_dir}/client`,
486
- emptyOutDir: false
487
- },
488
- resolve: {
489
- alias: {
490
- '$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
491
- }
492
- },
493
- optimizeDeps: {
494
- entries: []
495
- }
496
- });
497
- }
498
-
499
- /**
500
- * @param {string} entry
501
- * @returns {string}
502
- */
503
- function resolve_entry(entry) {
504
- if (fs.existsSync(entry)) {
505
- const stats = fs.statSync(entry);
506
- if (stats.isDirectory()) {
507
- return resolve_entry(path.join(entry, 'index'));
508
- }
509
-
510
- return entry;
511
- } else {
512
- const dir = path.dirname(entry);
513
-
514
- if (fs.existsSync(dir)) {
515
- const base = path.basename(entry);
516
- const files = fs.readdirSync(dir);
517
-
518
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
519
-
520
- if (found) return path.join(dir, found);
521
- }
522
- }
523
-
524
- return null;
525
- }
526
-
527
- /** @param {string[]} array */
528
- function get_params(array) {
529
- // given an array of params like `['x', 'y', 'z']` for
530
- // src/routes/[x]/[y]/[z]/svelte, create a function
531
- // that turns a RexExpMatchArray into ({ x, y, z })
532
- return array.length
533
- ? '(m) => ({ ' +
534
- array
535
- .map((param, i) => {
536
- return param.startsWith('...')
537
- ? `${param.slice(3)}: d(m[${i + 1}]).split('/')`
538
- : `${param}: d(m[${i + 1}])`;
539
- })
540
- .join(', ') +
541
- '})'
542
- : 'empty';
543
- }
544
-
545
- export { build };