@sveltejs/kit 1.0.0-next.41 → 1.0.0-next.410

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 (129) hide show
  1. package/README.md +12 -9
  2. package/package.json +97 -63
  3. package/src/cli.js +119 -0
  4. package/src/core/adapt/builder.js +207 -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 +3 -0
  10. package/src/core/generate_manifest/index.js +99 -0
  11. package/src/core/prerender/crawl.js +194 -0
  12. package/src/core/prerender/prerender.js +378 -0
  13. package/src/core/prerender/queue.js +80 -0
  14. package/src/core/sync/create_manifest_data/index.js +492 -0
  15. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  16. package/src/core/sync/sync.js +59 -0
  17. package/src/core/sync/utils.js +97 -0
  18. package/src/core/sync/write_ambient.js +87 -0
  19. package/src/core/sync/write_client_manifest.js +82 -0
  20. package/src/core/sync/write_matchers.js +25 -0
  21. package/src/core/sync/write_root.js +88 -0
  22. package/src/core/sync/write_tsconfig.js +189 -0
  23. package/src/core/sync/write_types.js +723 -0
  24. package/src/core/utils.js +58 -0
  25. package/src/hooks.js +26 -0
  26. package/src/index/index.js +45 -0
  27. package/src/index/private.js +33 -0
  28. package/src/node/index.js +145 -0
  29. package/src/node/polyfills.js +40 -0
  30. package/src/packaging/index.js +218 -0
  31. package/src/packaging/types.d.ts +8 -0
  32. package/src/packaging/typescript.js +150 -0
  33. package/src/packaging/utils.js +143 -0
  34. package/src/runtime/app/env.js +11 -0
  35. package/src/runtime/app/navigation.js +22 -0
  36. package/src/runtime/app/paths.js +1 -0
  37. package/src/runtime/app/stores.js +94 -0
  38. package/src/runtime/client/ambient.d.ts +17 -0
  39. package/src/runtime/client/client.js +1269 -0
  40. package/src/runtime/client/fetcher.js +60 -0
  41. package/src/runtime/client/parse.js +36 -0
  42. package/src/runtime/client/singletons.js +11 -0
  43. package/src/runtime/client/start.js +48 -0
  44. package/src/runtime/client/types.d.ts +106 -0
  45. package/src/runtime/client/utils.js +113 -0
  46. package/src/runtime/components/error.svelte +16 -0
  47. package/{assets → src/runtime}/components/layout.svelte +0 -0
  48. package/src/runtime/env/dynamic/private.js +1 -0
  49. package/src/runtime/env/dynamic/public.js +1 -0
  50. package/src/runtime/env-private.js +7 -0
  51. package/src/runtime/env-public.js +7 -0
  52. package/src/runtime/env.js +6 -0
  53. package/src/runtime/hash.js +16 -0
  54. package/src/runtime/paths.js +11 -0
  55. package/src/runtime/server/endpoint.js +42 -0
  56. package/src/runtime/server/index.js +434 -0
  57. package/src/runtime/server/page/cookie.js +25 -0
  58. package/src/runtime/server/page/crypto.js +239 -0
  59. package/src/runtime/server/page/csp.js +249 -0
  60. package/src/runtime/server/page/fetch.js +265 -0
  61. package/src/runtime/server/page/index.js +418 -0
  62. package/src/runtime/server/page/load_data.js +94 -0
  63. package/src/runtime/server/page/render.js +357 -0
  64. package/src/runtime/server/page/respond_with_error.js +105 -0
  65. package/src/runtime/server/page/types.d.ts +44 -0
  66. package/src/runtime/server/utils.js +116 -0
  67. package/src/utils/error.js +22 -0
  68. package/src/utils/escape.js +104 -0
  69. package/src/utils/filesystem.js +108 -0
  70. package/src/utils/http.js +55 -0
  71. package/src/utils/misc.js +1 -0
  72. package/src/utils/routing.js +107 -0
  73. package/src/utils/url.js +97 -0
  74. package/src/vite/build/build_server.js +335 -0
  75. package/src/vite/build/build_service_worker.js +90 -0
  76. package/src/vite/build/utils.js +153 -0
  77. package/src/vite/dev/index.js +565 -0
  78. package/src/vite/index.js +540 -0
  79. package/src/vite/preview/index.js +186 -0
  80. package/src/vite/types.d.ts +3 -0
  81. package/src/vite/utils.js +335 -0
  82. package/svelte-kit.js +1 -1
  83. package/types/ambient.d.ts +368 -0
  84. package/types/index.d.ts +345 -0
  85. package/types/internal.d.ts +313 -0
  86. package/types/private.d.ts +236 -0
  87. package/CHANGELOG.md +0 -419
  88. package/assets/components/error.svelte +0 -13
  89. package/assets/runtime/app/env.js +0 -5
  90. package/assets/runtime/app/navigation.js +0 -41
  91. package/assets/runtime/app/paths.js +0 -1
  92. package/assets/runtime/app/stores.js +0 -93
  93. package/assets/runtime/chunks/utils.js +0 -19
  94. package/assets/runtime/internal/singletons.js +0 -23
  95. package/assets/runtime/internal/start.js +0 -770
  96. package/assets/runtime/paths.js +0 -12
  97. package/dist/api.js +0 -28
  98. package/dist/api.js.map +0 -1
  99. package/dist/chunks/index.js +0 -3519
  100. package/dist/chunks/index2.js +0 -587
  101. package/dist/chunks/index3.js +0 -246
  102. package/dist/chunks/index4.js +0 -524
  103. package/dist/chunks/index5.js +0 -761
  104. package/dist/chunks/index6.js +0 -322
  105. package/dist/chunks/standard.js +0 -99
  106. package/dist/chunks/utils.js +0 -83
  107. package/dist/cli.js +0 -546
  108. package/dist/cli.js.map +0 -1
  109. package/dist/create_app.js +0 -592
  110. package/dist/create_app.js.map +0 -1
  111. package/dist/index.js +0 -392
  112. package/dist/index.js.map +0 -1
  113. package/dist/index2.js +0 -3519
  114. package/dist/index2.js.map +0 -1
  115. package/dist/index3.js +0 -320
  116. package/dist/index3.js.map +0 -1
  117. package/dist/index4.js +0 -323
  118. package/dist/index4.js.map +0 -1
  119. package/dist/index5.js +0 -247
  120. package/dist/index5.js.map +0 -1
  121. package/dist/index6.js +0 -761
  122. package/dist/index6.js.map +0 -1
  123. package/dist/renderer.js +0 -2499
  124. package/dist/renderer.js.map +0 -1
  125. package/dist/ssr.js +0 -2581
  126. package/dist/standard.js +0 -100
  127. package/dist/standard.js.map +0 -1
  128. package/dist/utils.js +0 -84
  129. package/dist/utils.js.map +0 -1
@@ -0,0 +1,335 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { mkdirp, posixify } from '../../utils/filesystem.js';
4
+ import { get_vite_config, merge_vite_configs, resolve_entry } from '../utils.js';
5
+ import { load_template } from '../../core/config/index.js';
6
+ import { runtime_directory } from '../../core/utils.js';
7
+ import { create_build, find_deps, get_default_config, is_http_method } from './utils.js';
8
+ import { s } from '../../utils/misc.js';
9
+
10
+ /**
11
+ * @param {{
12
+ * hooks: string;
13
+ * config: import('types').ValidatedConfig;
14
+ * has_service_worker: boolean;
15
+ * runtime: string;
16
+ * template: string;
17
+ * }} opts
18
+ */
19
+ const server_template = ({ config, hooks, has_service_worker, runtime, template }) => `
20
+ import root from '__GENERATED__/root.svelte';
21
+ import { respond } from '${runtime}/server/index.js';
22
+ import { set_paths, assets, base } from '${runtime}/paths.js';
23
+ import { set_prerendering } from '${runtime}/env.js';
24
+ import { set_private_env } from '${runtime}/env-private.js';
25
+ import { set_public_env } from '${runtime}/env-public.js';
26
+
27
+ const template = ({ head, body, assets, nonce }) => ${s(template)
28
+ .replace('%sveltekit.head%', '" + head + "')
29
+ .replace('%sveltekit.body%', '" + body + "')
30
+ .replace(/%sveltekit\.assets%/g, '" + assets + "')
31
+ .replace(/%sveltekit\.nonce%/g, '" + nonce + "')};
32
+
33
+ let read = null;
34
+
35
+ set_paths(${s(config.kit.paths)});
36
+
37
+ let default_protocol = 'https';
38
+
39
+ // allow paths to be globally overridden
40
+ // in svelte-kit preview and in prerendering
41
+ export function override(settings) {
42
+ default_protocol = settings.protocol || default_protocol;
43
+ set_paths(settings.paths);
44
+ set_prerendering(settings.prerendering);
45
+ read = settings.read;
46
+ }
47
+
48
+ export class Server {
49
+ constructor(manifest) {
50
+ this.options = {
51
+ csp: ${s(config.kit.csp)},
52
+ dev: false,
53
+ get_stack: error => String(error), // for security
54
+ handle_error: (error, event) => {
55
+ this.options.hooks.handleError({
56
+ error,
57
+ event,
58
+
59
+ // TODO remove for 1.0
60
+ // @ts-expect-error
61
+ get request() {
62
+ throw new Error('request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details');
63
+ }
64
+ });
65
+ error.stack = this.options.get_stack(error);
66
+ },
67
+ hooks: null,
68
+ hydrate: ${s(config.kit.browser.hydrate)},
69
+ manifest,
70
+ method_override: ${s(config.kit.methodOverride)},
71
+ paths: { base, assets },
72
+ prefix: assets + '/',
73
+ prerender: {
74
+ default: ${config.kit.prerender.default},
75
+ enabled: ${config.kit.prerender.enabled}
76
+ },
77
+ public_env: {},
78
+ read,
79
+ root,
80
+ service_worker: ${has_service_worker ? "base + '/service-worker.js'" : 'null'},
81
+ router: ${s(config.kit.browser.router)},
82
+ template,
83
+ template_contains_nonce: ${template.includes('%sveltekit.nonce%')},
84
+ trailing_slash: ${s(config.kit.trailingSlash)}
85
+ };
86
+ }
87
+
88
+ init({ env }) {
89
+ const entries = Object.entries(env);
90
+
91
+ const prv = Object.fromEntries(entries.filter(([k]) => !k.startsWith('${
92
+ config.kit.env.publicPrefix
93
+ }')));
94
+
95
+ const pub = Object.fromEntries(entries.filter(([k]) => k.startsWith('${
96
+ config.kit.env.publicPrefix
97
+ }')));
98
+
99
+ set_private_env(prv);
100
+ set_public_env(pub);
101
+
102
+ this.options.public_env = pub;
103
+ }
104
+
105
+ async respond(request, options = {}) {
106
+ if (!(request instanceof Request)) {
107
+ throw new Error('The first argument to server.respond must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details');
108
+ }
109
+
110
+ if (!this.options.hooks) {
111
+ const module = await import(${s(hooks)});
112
+ this.options.hooks = {
113
+ getSession: module.getSession || (() => ({})),
114
+ handle: module.handle || (({ event, resolve }) => resolve(event)),
115
+ handleError: module.handleError || (({ error }) => console.error(error.stack)),
116
+ externalFetch: module.externalFetch || fetch
117
+ };
118
+ }
119
+
120
+ return respond(request, this.options, options);
121
+ }
122
+ }
123
+ `;
124
+
125
+ /**
126
+ * @param {{
127
+ * cwd: string;
128
+ * config: import('types').ValidatedConfig;
129
+ * vite_config: import('vite').ResolvedConfig;
130
+ * vite_config_env: import('vite').ConfigEnv;
131
+ * manifest_data: import('types').ManifestData;
132
+ * build_dir: string;
133
+ * output_dir: string;
134
+ * service_worker_entry_file: string | null;
135
+ * }} options
136
+ * @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
137
+ */
138
+ export async function build_server(options, client) {
139
+ const {
140
+ cwd,
141
+ config,
142
+ vite_config,
143
+ vite_config_env,
144
+ manifest_data,
145
+ build_dir,
146
+ output_dir,
147
+ service_worker_entry_file
148
+ } = options;
149
+
150
+ let hooks_file = resolve_entry(config.kit.files.hooks);
151
+ if (!hooks_file || !fs.existsSync(hooks_file)) {
152
+ hooks_file = path.join(config.kit.outDir, 'build/hooks.js');
153
+ fs.writeFileSync(hooks_file, '');
154
+ }
155
+
156
+ /** @type {Record<string, string>} */
157
+ const input = {
158
+ index: `${build_dir}/index.js`
159
+ };
160
+
161
+ // add entry points for every endpoint...
162
+ manifest_data.routes.forEach((route) => {
163
+ if (route.type === 'endpoint') {
164
+ const resolved = path.resolve(cwd, route.file);
165
+ const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));
166
+ const name = posixify(path.join('entries/endpoints', relative.replace(/\.js$/, '')));
167
+ input[name] = resolved;
168
+ }
169
+ });
170
+
171
+ // ...and every component used by pages...
172
+ manifest_data.nodes.forEach((node) => {
173
+ for (const file of [node.component, node.shared, node.server]) {
174
+ if (file) {
175
+ const resolved = path.resolve(cwd, file);
176
+ const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));
177
+
178
+ const name = relative.startsWith('..')
179
+ ? posixify(path.join('entries/fallbacks', path.basename(file)))
180
+ : posixify(path.join('entries/pages', relative.replace(/\.js$/, '')));
181
+ input[name] = resolved;
182
+ }
183
+ }
184
+ });
185
+
186
+ // ...and every matcher
187
+ Object.entries(manifest_data.matchers).forEach(([key, file]) => {
188
+ const name = posixify(path.join('entries/matchers', key));
189
+ input[name] = path.resolve(cwd, file);
190
+ });
191
+
192
+ /** @type {(file: string) => string} */
193
+ const app_relative = (file) => {
194
+ const relative_file = path.relative(build_dir, path.resolve(cwd, file));
195
+ return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
196
+ };
197
+
198
+ fs.writeFileSync(
199
+ input.index,
200
+ server_template({
201
+ config,
202
+ hooks: app_relative(hooks_file),
203
+ has_service_worker: config.kit.serviceWorker.register && !!service_worker_entry_file,
204
+ runtime: posixify(path.relative(build_dir, runtime_directory)),
205
+ template: load_template(cwd, config)
206
+ })
207
+ );
208
+
209
+ const merged_config = merge_vite_configs(
210
+ get_default_config({ config, input, ssr: true, outDir: `${output_dir}/server` }),
211
+ await get_vite_config(vite_config, vite_config_env)
212
+ );
213
+
214
+ const { chunks } = await create_build(merged_config);
215
+
216
+ /** @type {import('vite').Manifest} */
217
+ const vite_manifest = JSON.parse(
218
+ fs.readFileSync(`${output_dir}/server/${vite_config.build.manifest}`, 'utf-8')
219
+ );
220
+
221
+ mkdirp(`${output_dir}/server/nodes`);
222
+ mkdirp(`${output_dir}/server/stylesheets`);
223
+
224
+ const stylesheet_lookup = new Map();
225
+
226
+ client.assets.forEach((asset) => {
227
+ if (asset.fileName.endsWith('.css')) {
228
+ if (asset.source.length < config.kit.inlineStyleThreshold) {
229
+ const index = stylesheet_lookup.size;
230
+ const file = `${output_dir}/server/stylesheets/${index}.js`;
231
+
232
+ fs.writeFileSync(file, `// ${asset.fileName}\nexport default ${s(asset.source)};`);
233
+ stylesheet_lookup.set(asset.fileName, index);
234
+ }
235
+ }
236
+ });
237
+
238
+ manifest_data.nodes.forEach((node, i) => {
239
+ /** @type {string[]} */
240
+ const imports = [];
241
+
242
+ /** @type {string[]} */
243
+ const exports = [`export const index = ${i};`];
244
+
245
+ /** @type {string[]} */
246
+ const imported = [];
247
+
248
+ /** @type {string[]} */
249
+ const stylesheets = [];
250
+
251
+ if (node.component) {
252
+ const entry = find_deps(client.vite_manifest, node.component, true);
253
+
254
+ imported.push(...entry.imports);
255
+ stylesheets.push(...entry.stylesheets);
256
+
257
+ exports.push(
258
+ `export { default as component } from '../${vite_manifest[node.component].file}';`,
259
+ `export const file = '${entry.file}';` // TODO what is this?
260
+ );
261
+ }
262
+
263
+ if (node.shared) {
264
+ const entry = find_deps(client.vite_manifest, node.shared, true);
265
+
266
+ imported.push(...entry.imports);
267
+ stylesheets.push(...entry.stylesheets);
268
+
269
+ imports.push(`import * as shared from '../${vite_manifest[node.shared].file}';`);
270
+ exports.push(`export { shared };`);
271
+ }
272
+
273
+ if (node.server) {
274
+ imports.push(`import * as server from '../${vite_manifest[node.server].file}';`);
275
+ exports.push(`export { server };`);
276
+ }
277
+
278
+ exports.push(
279
+ `export const imports = ${s(imported)};`,
280
+ `export const stylesheets = ${s(stylesheets)};`
281
+ );
282
+
283
+ /** @type {string[]} */
284
+ const styles = [];
285
+
286
+ stylesheets.forEach((file) => {
287
+ if (stylesheet_lookup.has(file)) {
288
+ const index = stylesheet_lookup.get(file);
289
+ const name = `stylesheet_${index}`;
290
+ imports.push(`import ${name} from '../stylesheets/${index}.js';`);
291
+ styles.push(`\t${s(file)}: ${name}`);
292
+ }
293
+ });
294
+
295
+ if (styles.length > 0) {
296
+ exports.push(`export const inline_styles = () => ({\n${styles.join(',\n')}\n});`);
297
+ }
298
+
299
+ const out = `${output_dir}/server/nodes/${i}.js`;
300
+ fs.writeFileSync(out, `${imports.join('\n')}\n\n${exports.join('\n')}\n`);
301
+ });
302
+
303
+ return {
304
+ chunks,
305
+ vite_manifest,
306
+ methods: get_methods(cwd, chunks, manifest_data)
307
+ };
308
+ }
309
+
310
+ /**
311
+ * @param {string} cwd
312
+ * @param {import('rollup').OutputChunk[]} output
313
+ * @param {import('types').ManifestData} manifest_data
314
+ */
315
+ function get_methods(cwd, output, manifest_data) {
316
+ /** @type {Record<string, string[]>} */
317
+ const lookup = {};
318
+ output.forEach((chunk) => {
319
+ if (!chunk.facadeModuleId) return;
320
+ const id = chunk.facadeModuleId.slice(cwd.length + 1);
321
+ lookup[id] = chunk.exports;
322
+ });
323
+
324
+ /** @type {Record<string, import('types').HttpMethod[]>} */
325
+ const methods = {};
326
+ manifest_data.routes.forEach((route) => {
327
+ const file = route.type === 'endpoint' ? route.file : route.leaf.server;
328
+
329
+ if (file && lookup[file]) {
330
+ methods[file] = lookup[file].filter(is_http_method);
331
+ }
332
+ });
333
+
334
+ return methods;
335
+ }
@@ -0,0 +1,90 @@
1
+ import fs from 'fs';
2
+ import * as vite from 'vite';
3
+ import { s } from '../../utils/misc.js';
4
+ import { assets_base } from './utils.js';
5
+
6
+ /**
7
+ * @param {{
8
+ * config: import('types').ValidatedConfig;
9
+ * vite_config: import('vite').ResolvedConfig;
10
+ * vite_config_env: import('vite').ConfigEnv;
11
+ * manifest_data: import('types').ManifestData;
12
+ * output_dir: string;
13
+ * service_worker_entry_file: string | null;
14
+ * }} options
15
+ * @param {import('types').Prerendered} prerendered
16
+ * @param {import('vite').Manifest} client_manifest
17
+ */
18
+ export async function build_service_worker(
19
+ { config, manifest_data, output_dir, service_worker_entry_file },
20
+ prerendered,
21
+ client_manifest
22
+ ) {
23
+ const build = new Set();
24
+ for (const key in client_manifest) {
25
+ const { file, css = [], assets = [] } = client_manifest[key];
26
+ build.add(file);
27
+ css.forEach((file) => build.add(file));
28
+ assets.forEach((file) => build.add(file));
29
+ }
30
+
31
+ const service_worker = `${config.kit.outDir}/generated/service-worker.js`;
32
+
33
+ fs.writeFileSync(
34
+ service_worker,
35
+ `
36
+ // TODO remove for 1.0
37
+ export const timestamp = {
38
+ toString: () => {
39
+ throw new Error('\`timestamp\` has been removed from $service-worker. Use \`version\` instead');
40
+ }
41
+ };
42
+
43
+ export const build = [
44
+ ${Array.from(build)
45
+ .map((file) => `${s(`${config.kit.paths.base}/${file}`)}`)
46
+ .join(',\n\t\t\t\t')}
47
+ ];
48
+
49
+ export const files = [
50
+ ${manifest_data.assets
51
+ .filter((asset) => config.kit.serviceWorker.files(asset.file))
52
+ .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
53
+ .join(',\n\t\t\t\t')}
54
+ ];
55
+
56
+ export const prerendered = [
57
+ ${prerendered.paths.map((path) => s(path)).join(',\n\t\t\t\t')}
58
+ ];
59
+
60
+ export const version = ${s(config.kit.version.name)};
61
+ `
62
+ .replace(/^\t{3}/gm, '')
63
+ .trim()
64
+ );
65
+
66
+ await vite.build({
67
+ base: assets_base(config.kit),
68
+ build: {
69
+ lib: {
70
+ entry: /** @type {string} */ (service_worker_entry_file),
71
+ name: 'app',
72
+ formats: ['es']
73
+ },
74
+ rollupOptions: {
75
+ output: {
76
+ entryFileNames: 'service-worker.js'
77
+ }
78
+ },
79
+ outDir: `${output_dir}/client`,
80
+ emptyOutDir: false
81
+ },
82
+ configFile: false,
83
+ resolve: {
84
+ alias: {
85
+ '$service-worker': service_worker,
86
+ $lib: config.kit.files.lib
87
+ }
88
+ }
89
+ });
90
+ }
@@ -0,0 +1,153 @@
1
+ import * as vite from 'vite';
2
+ import { get_aliases } from '../utils.js';
3
+
4
+ /**
5
+ * @typedef {import('rollup').RollupOutput} RollupOutput
6
+ * @typedef {import('rollup').OutputChunk} OutputChunk
7
+ * @typedef {import('rollup').OutputAsset} OutputAsset
8
+ */
9
+
10
+ /**
11
+ * Invokes Vite.
12
+ * @param {import('vite').UserConfig} config
13
+ */
14
+ export async function create_build(config) {
15
+ const { output } = /** @type {RollupOutput} */ (
16
+ await vite.build({ ...config, configFile: false })
17
+ );
18
+
19
+ const chunks = output.filter(
20
+ /** @returns {output is OutputChunk} */ (output) => output.type === 'chunk'
21
+ );
22
+
23
+ const assets = output.filter(
24
+ /** @returns {output is OutputAsset} */ (output) => output.type === 'asset'
25
+ );
26
+
27
+ return { chunks, assets };
28
+ }
29
+
30
+ /**
31
+ * Adds transitive JS and CSS dependencies to the js and css inputs.
32
+ * @param {import('vite').Manifest} manifest
33
+ * @param {string} entry
34
+ * @param {boolean} add_dynamic_css
35
+ */
36
+ export function find_deps(manifest, entry, add_dynamic_css) {
37
+ /** @type {Set<string>} */
38
+ const seen = new Set();
39
+
40
+ /** @type {Set<string>} */
41
+ const imports = new Set();
42
+
43
+ /** @type {Set<string>} */
44
+ const stylesheets = new Set();
45
+
46
+ /**
47
+ * @param {string} file
48
+ * @param {boolean} add_js
49
+ */
50
+ function traverse(file, add_js) {
51
+ if (seen.has(file)) return;
52
+ seen.add(file);
53
+
54
+ const chunk = manifest[file];
55
+
56
+ if (add_js) imports.add(chunk.file);
57
+
58
+ if (chunk.css) {
59
+ chunk.css.forEach((file) => stylesheets.add(file));
60
+ }
61
+
62
+ if (chunk.imports) {
63
+ chunk.imports.forEach((file) => traverse(file, add_js));
64
+ }
65
+
66
+ if (add_dynamic_css && chunk.dynamicImports) {
67
+ chunk.dynamicImports.forEach((file) => traverse(file, false));
68
+ }
69
+ }
70
+
71
+ traverse(entry, true);
72
+
73
+ return {
74
+ file: manifest[entry].file,
75
+ imports: Array.from(imports),
76
+ stylesheets: Array.from(stylesheets)
77
+ };
78
+ }
79
+
80
+ /**
81
+ * The Vite configuration that we use by default.
82
+ * @param {{
83
+ * config: import('types').ValidatedConfig;
84
+ * input: Record<string, string>;
85
+ * ssr: boolean;
86
+ * outDir: string;
87
+ * }} options
88
+ * @return {import('vite').UserConfig}
89
+ */
90
+ export const get_default_config = function ({ config, input, ssr, outDir }) {
91
+ return {
92
+ appType: 'custom',
93
+ base: assets_base(config.kit),
94
+ build: {
95
+ cssCodeSplit: true,
96
+ // don't use the default name to avoid collisions with 'static/manifest.json'
97
+ manifest: 'vite-manifest.json',
98
+ outDir,
99
+ polyfillModulePreload: false,
100
+ rollupOptions: {
101
+ input,
102
+ output: {
103
+ format: 'esm',
104
+ entryFileNames: ssr ? '[name].js' : `${config.kit.appDir}/immutable/[name]-[hash].js`,
105
+ chunkFileNames: ssr
106
+ ? 'chunks/[name].js'
107
+ : `${config.kit.appDir}/immutable/chunks/[name]-[hash].js`,
108
+ assetFileNames: `${config.kit.appDir}/immutable/assets/[name]-[hash][extname]`
109
+ },
110
+ preserveEntrySignatures: 'strict'
111
+ },
112
+ ssr,
113
+ target: ssr ? 'node14.8' : undefined
114
+ },
115
+ define: {
116
+ __SVELTEKIT_ADAPTER_NAME__: JSON.stringify(config.kit.adapter?.name),
117
+ __SVELTEKIT_APP_VERSION__: JSON.stringify(config.kit.version.name),
118
+ __SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`),
119
+ __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval),
120
+ __SVELTEKIT_DEV__: 'false'
121
+ },
122
+ publicDir: ssr ? false : config.kit.files.assets,
123
+ resolve: {
124
+ alias: get_aliases(config.kit)
125
+ },
126
+ ssr: {
127
+ noExternal: ['@sveltejs/kit']
128
+ }
129
+ };
130
+ };
131
+
132
+ /**
133
+ * @param {import('types').ValidatedKitConfig} config
134
+ * @returns {string}
135
+ */
136
+ export function assets_base(config) {
137
+ // TODO this is so that Vite's preloading works. Unfortunately, it fails
138
+ // during `svelte-kit preview`, because we use a local asset path. This
139
+ // may be fixed in Vite 3: https://github.com/vitejs/vite/issues/2009
140
+ const { base, assets } = config.paths;
141
+ return `${assets || base}/`;
142
+ }
143
+
144
+ const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
145
+
146
+ // If we'd written this in TypeScript, it could be easy...
147
+ /**
148
+ * @param {string} str
149
+ * @returns {str is import('types').HttpMethod}
150
+ */
151
+ export function is_http_method(str) {
152
+ return method_names.has(str);
153
+ }