@sveltejs/kit 1.0.0-next.403 → 1.0.0-next.407
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.
- package/package.json +24 -25
- package/{dist → src}/cli.js +19 -18
- package/{dist/chunks/index3.js → src/core/adapt/builder.js} +52 -63
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/{dist/chunks/index.js → src/core/config/options.js} +7 -194
- package/src/core/config/types.d.ts +1 -0
- package/src/core/constants.js +3 -0
- package/src/core/generate_manifest/index.js +99 -0
- package/src/core/prerender/crawl.js +194 -0
- package/src/core/prerender/prerender.js +378 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +492 -0
- package/src/core/sync/create_manifest_data/types.d.ts +40 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +97 -0
- package/src/core/sync/write_ambient.js +87 -0
- package/src/core/sync/write_client_manifest.js +82 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +88 -0
- package/{dist/chunks → src/core/sync}/write_tsconfig.js +24 -108
- package/src/core/sync/write_types.js +738 -0
- package/src/core/utils.js +58 -0
- package/{dist → src}/hooks.js +1 -3
- package/src/index/index.js +45 -0
- package/src/index/private.js +33 -0
- package/src/node/index.js +145 -0
- package/src/node/polyfills.js +40 -0
- package/src/packaging/index.js +218 -0
- package/src/packaging/types.d.ts +8 -0
- package/src/packaging/typescript.js +150 -0
- package/src/packaging/utils.js +143 -0
- package/{assets → src/runtime}/app/env.js +3 -5
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/{assets → src/runtime}/app/stores.js +6 -9
- package/src/runtime/client/ambient.d.ts +17 -0
- package/{assets/client/start.js → src/runtime/client/client.js} +302 -878
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/{assets → src/runtime}/client/singletons.js +2 -4
- package/src/runtime/client/start.js +48 -0
- package/src/runtime/client/types.d.ts +106 -0
- package/src/runtime/client/utils.js +113 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/{assets → src/runtime}/env/dynamic/private.js +0 -0
- package/{assets → src/runtime}/env/dynamic/public.js +0 -0
- package/{assets → src/runtime}/env-private.js +2 -4
- package/{assets → src/runtime}/env-public.js +2 -4
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/{assets → src/runtime}/paths.js +3 -5
- package/src/runtime/server/endpoint.js +42 -0
- package/src/runtime/server/index.js +434 -0
- package/src/runtime/server/page/cookie.js +25 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +249 -0
- package/src/runtime/server/page/fetch.js +265 -0
- package/src/runtime/server/page/index.js +418 -0
- package/src/runtime/server/page/load_data.js +94 -0
- package/src/runtime/server/page/render.js +357 -0
- package/src/runtime/server/page/respond_with_error.js +105 -0
- package/src/runtime/server/page/types.d.ts +44 -0
- package/src/runtime/server/utils.js +116 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +104 -0
- package/{dist/chunks → src/utils}/filesystem.js +22 -24
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +107 -0
- package/src/utils/url.js +97 -0
- package/src/vite/build/build_server.js +335 -0
- package/src/vite/build/build_service_worker.js +90 -0
- package/src/vite/build/utils.js +153 -0
- package/src/vite/dev/index.js +565 -0
- package/src/vite/index.js +540 -0
- package/src/vite/preview/index.js +186 -0
- package/src/vite/types.d.ts +3 -0
- package/src/vite/utils.js +335 -0
- package/svelte-kit.js +1 -10
- package/types/ambient.d.ts +5 -12
- package/types/index.d.ts +91 -44
- package/types/internal.d.ts +50 -72
- package/types/private.d.ts +2 -1
- package/assets/app/navigation.js +0 -24
- package/assets/app/paths.js +0 -1
- package/assets/components/error.svelte +0 -29
- package/assets/env.js +0 -8
- package/assets/server/index.js +0 -3579
- package/dist/chunks/error.js +0 -12
- package/dist/chunks/index2.js +0 -15745
- package/dist/chunks/multipart-parser.js +0 -458
- package/dist/chunks/sync.js +0 -1366
- package/dist/chunks/utils.js +0 -66
- package/dist/node/polyfills.js +0 -17928
- package/dist/node.js +0 -348
- package/dist/prerender.js +0 -788
- package/dist/vite.js +0 -2520
package/dist/vite.js
DELETED
|
@@ -1,2520 +0,0 @@
|
|
|
1
|
-
import { fork } from 'node:child_process';
|
|
2
|
-
import fs$1 from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { a as load_template, $, l as load_config } from './chunks/index.js';
|
|
5
|
-
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
6
|
-
import * as vite from 'vite';
|
|
7
|
-
import { loadEnv } from 'vite';
|
|
8
|
-
import { p as posixify, m as mkdirp, r as rimraf } from './chunks/filesystem.js';
|
|
9
|
-
import { g as get_aliases, r as resolve_entry, s, m as merge_vite_configs, a as get_vite_config, i as init, b as get_env, u as update, p as prevent_illegal_vite_imports, c as parse_route_id, d as all, e as prevent_illegal_rollup_imports } from './chunks/sync.js';
|
|
10
|
-
import * as fs from 'fs';
|
|
11
|
-
import fs__default, { readdirSync, statSync } from 'fs';
|
|
12
|
-
import path__default, { resolve, join, normalize } from 'path';
|
|
13
|
-
import { g as get_runtime_directory, a as get_runtime_prefix, b as get_mime_lookup, l as logger } from './chunks/utils.js';
|
|
14
|
-
import * as qs from 'querystring';
|
|
15
|
-
import { URL as URL$1, pathToFileURL } from 'url';
|
|
16
|
-
import { getRequest, setResponse } from './node.js';
|
|
17
|
-
import { installPolyfills } from './node/polyfills.js';
|
|
18
|
-
import { c as coalesce_to_error } from './chunks/error.js';
|
|
19
|
-
import { fileURLToPath } from 'node:url';
|
|
20
|
-
import './chunks/write_tsconfig.js';
|
|
21
|
-
import 'assert';
|
|
22
|
-
import 'net';
|
|
23
|
-
import 'http';
|
|
24
|
-
import 'stream';
|
|
25
|
-
import 'buffer';
|
|
26
|
-
import 'util';
|
|
27
|
-
import 'stream/web';
|
|
28
|
-
import 'perf_hooks';
|
|
29
|
-
import 'util/types';
|
|
30
|
-
import 'events';
|
|
31
|
-
import 'tls';
|
|
32
|
-
import 'async_hooks';
|
|
33
|
-
import 'console';
|
|
34
|
-
import 'zlib';
|
|
35
|
-
import 'node:http';
|
|
36
|
-
import 'node:https';
|
|
37
|
-
import 'node:zlib';
|
|
38
|
-
import 'node:stream';
|
|
39
|
-
import 'node:buffer';
|
|
40
|
-
import 'node:util';
|
|
41
|
-
import 'node:net';
|
|
42
|
-
import 'crypto';
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @typedef {import('rollup').RollupOutput} RollupOutput
|
|
46
|
-
* @typedef {import('rollup').OutputChunk} OutputChunk
|
|
47
|
-
* @typedef {import('rollup').OutputAsset} OutputAsset
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Invokes Vite.
|
|
52
|
-
* @param {import('vite').UserConfig} config
|
|
53
|
-
*/
|
|
54
|
-
async function create_build(config) {
|
|
55
|
-
const { output } = /** @type {RollupOutput} */ (
|
|
56
|
-
await vite.build({ ...config, configFile: false })
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
const chunks = output.filter(
|
|
60
|
-
/** @returns {output is OutputChunk} */ (output) => output.type === 'chunk'
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
const assets = output.filter(
|
|
64
|
-
/** @returns {output is OutputAsset} */ (output) => output.type === 'asset'
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
return { chunks, assets };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Adds transitive JS and CSS dependencies to the js and css inputs.
|
|
72
|
-
* @param {import('vite').Manifest} manifest
|
|
73
|
-
* @param {string} entry
|
|
74
|
-
* @param {boolean} add_dynamic_css
|
|
75
|
-
*/
|
|
76
|
-
function find_deps$1(manifest, entry, add_dynamic_css) {
|
|
77
|
-
/** @type {Set<string>} */
|
|
78
|
-
const seen = new Set();
|
|
79
|
-
|
|
80
|
-
/** @type {Set<string>} */
|
|
81
|
-
const imports = new Set();
|
|
82
|
-
|
|
83
|
-
/** @type {Set<string>} */
|
|
84
|
-
const stylesheets = new Set();
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @param {string} file
|
|
88
|
-
* @param {boolean} add_js
|
|
89
|
-
*/
|
|
90
|
-
function traverse(file, add_js) {
|
|
91
|
-
if (seen.has(file)) return;
|
|
92
|
-
seen.add(file);
|
|
93
|
-
|
|
94
|
-
const chunk = manifest[file];
|
|
95
|
-
|
|
96
|
-
if (add_js) imports.add(chunk.file);
|
|
97
|
-
|
|
98
|
-
if (chunk.css) {
|
|
99
|
-
chunk.css.forEach((file) => stylesheets.add(file));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (chunk.imports) {
|
|
103
|
-
chunk.imports.forEach((file) => traverse(file, add_js));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (add_dynamic_css && chunk.dynamicImports) {
|
|
107
|
-
chunk.dynamicImports.forEach((file) => traverse(file, false));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
traverse(entry, true);
|
|
112
|
-
|
|
113
|
-
return {
|
|
114
|
-
file: manifest[entry].file,
|
|
115
|
-
imports: Array.from(imports),
|
|
116
|
-
stylesheets: Array.from(stylesheets)
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The Vite configuration that we use by default.
|
|
122
|
-
* @param {{
|
|
123
|
-
* config: import('types').ValidatedConfig;
|
|
124
|
-
* input: Record<string, string>;
|
|
125
|
-
* ssr: boolean;
|
|
126
|
-
* outDir: string;
|
|
127
|
-
* }} options
|
|
128
|
-
* @return {import('vite').UserConfig}
|
|
129
|
-
*/
|
|
130
|
-
const get_default_config = function ({ config, input, ssr, outDir }) {
|
|
131
|
-
return {
|
|
132
|
-
appType: 'custom',
|
|
133
|
-
base: assets_base(config.kit),
|
|
134
|
-
build: {
|
|
135
|
-
cssCodeSplit: true,
|
|
136
|
-
manifest: true,
|
|
137
|
-
outDir,
|
|
138
|
-
polyfillModulePreload: false,
|
|
139
|
-
rollupOptions: {
|
|
140
|
-
input,
|
|
141
|
-
output: {
|
|
142
|
-
format: 'esm',
|
|
143
|
-
entryFileNames: ssr ? '[name].js' : `${config.kit.appDir}/immutable/[name]-[hash].js`,
|
|
144
|
-
chunkFileNames: `${config.kit.appDir}/immutable/chunks/[name]-[hash].js`,
|
|
145
|
-
assetFileNames: `${config.kit.appDir}/immutable/assets/[name]-[hash][extname]`
|
|
146
|
-
},
|
|
147
|
-
preserveEntrySignatures: 'strict'
|
|
148
|
-
},
|
|
149
|
-
ssr,
|
|
150
|
-
target: ssr ? 'node14.8' : undefined
|
|
151
|
-
},
|
|
152
|
-
define: {
|
|
153
|
-
__SVELTEKIT_ADAPTER_NAME__: JSON.stringify(config.kit.adapter?.name),
|
|
154
|
-
__SVELTEKIT_APP_VERSION__: JSON.stringify(config.kit.version.name),
|
|
155
|
-
__SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`),
|
|
156
|
-
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval),
|
|
157
|
-
__SVELTEKIT_DEV__: 'false'
|
|
158
|
-
},
|
|
159
|
-
publicDir: ssr ? false : config.kit.files.assets,
|
|
160
|
-
resolve: {
|
|
161
|
-
alias: get_aliases(config.kit)
|
|
162
|
-
},
|
|
163
|
-
ssr: {
|
|
164
|
-
// when developing against the Kit src code, we want to ensure that
|
|
165
|
-
// our dependencies are bundled so that apps don't need to install
|
|
166
|
-
// them as peerDependencies
|
|
167
|
-
noExternal: []
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* @param {import('types').ValidatedKitConfig} config
|
|
175
|
-
* @returns {string}
|
|
176
|
-
*/
|
|
177
|
-
function assets_base(config) {
|
|
178
|
-
// TODO this is so that Vite's preloading works. Unfortunately, it fails
|
|
179
|
-
// during `svelte-kit preview`, because we use a local asset path. This
|
|
180
|
-
// may be fixed in Vite 3: https://github.com/vitejs/vite/issues/2009
|
|
181
|
-
const { base, assets } = config.paths;
|
|
182
|
-
return `${assets || base}/`;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* vite.config.js will contain vite-plugin-svelte-kit, which kicks off the server and service
|
|
187
|
-
* worker builds in a hook. When running the server and service worker builds we must remove
|
|
188
|
-
* the SvelteKit plugin so that we do not kick off additional instances of these builds.
|
|
189
|
-
* @param {import('vite').UserConfig} config
|
|
190
|
-
*/
|
|
191
|
-
function remove_svelte_kit(config) {
|
|
192
|
-
// TODO i feel like there's a more elegant way to do this
|
|
193
|
-
// @ts-expect-error - it can't handle infinite type expansion
|
|
194
|
-
config.plugins = (config.plugins || [])
|
|
195
|
-
.flat(Infinity)
|
|
196
|
-
.filter((plugin) => plugin.name !== 'vite-plugin-svelte-kit');
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
|
|
200
|
-
|
|
201
|
-
// If we'd written this in TypeScript, it could be easy...
|
|
202
|
-
/**
|
|
203
|
-
* @param {string} str
|
|
204
|
-
* @returns {str is import('types').HttpMethod}
|
|
205
|
-
*/
|
|
206
|
-
function is_http_method(str) {
|
|
207
|
-
return method_names.has(str);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* @param {{
|
|
212
|
-
* hooks: string;
|
|
213
|
-
* config: import('types').ValidatedConfig;
|
|
214
|
-
* has_service_worker: boolean;
|
|
215
|
-
* runtime: string;
|
|
216
|
-
* template: string;
|
|
217
|
-
* }} opts
|
|
218
|
-
*/
|
|
219
|
-
const server_template = ({ config, hooks, has_service_worker, runtime, template }) => `
|
|
220
|
-
import root from '__GENERATED__/root.svelte';
|
|
221
|
-
import { respond } from '${runtime}/server/index.js';
|
|
222
|
-
import { set_paths, assets, base } from '${runtime}/paths.js';
|
|
223
|
-
import { set_prerendering } from '${runtime}/env.js';
|
|
224
|
-
import { set_private_env } from '${runtime}/env-private.js';
|
|
225
|
-
import { set_public_env } from '${runtime}/env-public.js';
|
|
226
|
-
|
|
227
|
-
const template = ({ head, body, assets, nonce }) => ${s(template)
|
|
228
|
-
.replace('%sveltekit.head%', '" + head + "')
|
|
229
|
-
.replace('%sveltekit.body%', '" + body + "')
|
|
230
|
-
.replace(/%sveltekit\.assets%/g, '" + assets + "')
|
|
231
|
-
.replace(/%sveltekit\.nonce%/g, '" + nonce + "')};
|
|
232
|
-
|
|
233
|
-
let read = null;
|
|
234
|
-
|
|
235
|
-
set_paths(${s(config.kit.paths)});
|
|
236
|
-
|
|
237
|
-
let default_protocol = 'https';
|
|
238
|
-
|
|
239
|
-
// allow paths to be globally overridden
|
|
240
|
-
// in svelte-kit preview and in prerendering
|
|
241
|
-
export function override(settings) {
|
|
242
|
-
default_protocol = settings.protocol || default_protocol;
|
|
243
|
-
set_paths(settings.paths);
|
|
244
|
-
set_prerendering(settings.prerendering);
|
|
245
|
-
read = settings.read;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export class Server {
|
|
249
|
-
constructor(manifest) {
|
|
250
|
-
this.options = {
|
|
251
|
-
csp: ${s(config.kit.csp)},
|
|
252
|
-
dev: false,
|
|
253
|
-
get_stack: error => String(error), // for security
|
|
254
|
-
handle_error: (error, event) => {
|
|
255
|
-
this.options.hooks.handleError({
|
|
256
|
-
error,
|
|
257
|
-
event,
|
|
258
|
-
|
|
259
|
-
// TODO remove for 1.0
|
|
260
|
-
// @ts-expect-error
|
|
261
|
-
get request() {
|
|
262
|
-
throw new Error('request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details');
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
error.stack = this.options.get_stack(error);
|
|
266
|
-
},
|
|
267
|
-
hooks: null,
|
|
268
|
-
hydrate: ${s(config.kit.browser.hydrate)},
|
|
269
|
-
manifest,
|
|
270
|
-
method_override: ${s(config.kit.methodOverride)},
|
|
271
|
-
paths: { base, assets },
|
|
272
|
-
prefix: assets + '/',
|
|
273
|
-
prerender: {
|
|
274
|
-
default: ${config.kit.prerender.default},
|
|
275
|
-
enabled: ${config.kit.prerender.enabled}
|
|
276
|
-
},
|
|
277
|
-
public_env: {},
|
|
278
|
-
read,
|
|
279
|
-
root,
|
|
280
|
-
service_worker: ${has_service_worker ? "base + '/service-worker.js'" : 'null'},
|
|
281
|
-
router: ${s(config.kit.browser.router)},
|
|
282
|
-
template,
|
|
283
|
-
template_contains_nonce: ${template.includes('%sveltekit.nonce%')},
|
|
284
|
-
trailing_slash: ${s(config.kit.trailingSlash)}
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
init({ env }) {
|
|
289
|
-
const entries = Object.entries(env);
|
|
290
|
-
|
|
291
|
-
const prv = Object.fromEntries(Object.entries(env).filter(([k]) => !k.startsWith('${
|
|
292
|
-
config.kit.env.publicPrefix
|
|
293
|
-
}')));
|
|
294
|
-
|
|
295
|
-
const pub = Object.fromEntries(Object.entries(env).filter(([k]) => k.startsWith('${
|
|
296
|
-
config.kit.env.publicPrefix
|
|
297
|
-
}')));
|
|
298
|
-
|
|
299
|
-
set_private_env(prv);
|
|
300
|
-
set_public_env(pub);
|
|
301
|
-
|
|
302
|
-
this.options.public_env = pub;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
async respond(request, options = {}) {
|
|
306
|
-
if (!(request instanceof Request)) {
|
|
307
|
-
throw new Error('The first argument to server.respond must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details');
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (!this.options.hooks) {
|
|
311
|
-
const module = await import(${s(hooks)});
|
|
312
|
-
this.options.hooks = {
|
|
313
|
-
getSession: module.getSession || (() => ({})),
|
|
314
|
-
handle: module.handle || (({ event, resolve }) => resolve(event)),
|
|
315
|
-
handleError: module.handleError || (({ error }) => console.error(error.stack)),
|
|
316
|
-
externalFetch: module.externalFetch || fetch
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return respond(request, this.options, options);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
`;
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* @param {{
|
|
327
|
-
* cwd: string;
|
|
328
|
-
* config: import('types').ValidatedConfig;
|
|
329
|
-
* vite_config: import('vite').ResolvedConfig;
|
|
330
|
-
* vite_config_env: import('vite').ConfigEnv;
|
|
331
|
-
* manifest_data: import('types').ManifestData;
|
|
332
|
-
* build_dir: string;
|
|
333
|
-
* output_dir: string;
|
|
334
|
-
* service_worker_entry_file: string | null;
|
|
335
|
-
* }} options
|
|
336
|
-
* @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
|
|
337
|
-
*/
|
|
338
|
-
async function build_server(options, client) {
|
|
339
|
-
const {
|
|
340
|
-
cwd,
|
|
341
|
-
config,
|
|
342
|
-
vite_config,
|
|
343
|
-
vite_config_env,
|
|
344
|
-
manifest_data,
|
|
345
|
-
build_dir,
|
|
346
|
-
output_dir,
|
|
347
|
-
service_worker_entry_file
|
|
348
|
-
} = options;
|
|
349
|
-
|
|
350
|
-
let hooks_file = resolve_entry(config.kit.files.hooks);
|
|
351
|
-
if (!hooks_file || !fs__default.existsSync(hooks_file)) {
|
|
352
|
-
hooks_file = path__default.join(config.kit.outDir, 'build/hooks.js');
|
|
353
|
-
fs__default.writeFileSync(hooks_file, '');
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/** @type {Record<string, string>} */
|
|
357
|
-
const input = {
|
|
358
|
-
index: `${build_dir}/index.js`
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// add entry points for every endpoint...
|
|
362
|
-
manifest_data.routes.forEach((route) => {
|
|
363
|
-
const file = route.type === 'endpoint' ? route.file : route.shadow;
|
|
364
|
-
|
|
365
|
-
if (file) {
|
|
366
|
-
const resolved = path__default.resolve(cwd, file);
|
|
367
|
-
const relative = decodeURIComponent(path__default.relative(config.kit.files.routes, resolved));
|
|
368
|
-
const name = posixify(path__default.join('entries/endpoints', relative.replace(/\.js$/, '')));
|
|
369
|
-
input[name] = resolved;
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
// ...and every component used by pages...
|
|
374
|
-
manifest_data.components.forEach((file) => {
|
|
375
|
-
const resolved = path__default.resolve(cwd, file);
|
|
376
|
-
const relative = decodeURIComponent(path__default.relative(config.kit.files.routes, resolved));
|
|
377
|
-
|
|
378
|
-
const name = relative.startsWith('..')
|
|
379
|
-
? posixify(path__default.join('entries/fallbacks', path__default.basename(file)))
|
|
380
|
-
: posixify(path__default.join('entries/pages', relative));
|
|
381
|
-
input[name] = resolved;
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
// ...and every matcher
|
|
385
|
-
Object.entries(manifest_data.matchers).forEach(([key, file]) => {
|
|
386
|
-
const name = posixify(path__default.join('entries/matchers', key));
|
|
387
|
-
input[name] = path__default.resolve(cwd, file);
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
/** @type {(file: string) => string} */
|
|
391
|
-
const app_relative = (file) => {
|
|
392
|
-
const relative_file = path__default.relative(build_dir, path__default.resolve(cwd, file));
|
|
393
|
-
return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
fs__default.writeFileSync(
|
|
397
|
-
input.index,
|
|
398
|
-
server_template({
|
|
399
|
-
config,
|
|
400
|
-
hooks: app_relative(hooks_file),
|
|
401
|
-
has_service_worker: config.kit.serviceWorker.register && !!service_worker_entry_file,
|
|
402
|
-
runtime: posixify(path__default.relative(build_dir, get_runtime_directory(config.kit))),
|
|
403
|
-
template: load_template(cwd, config)
|
|
404
|
-
})
|
|
405
|
-
);
|
|
406
|
-
|
|
407
|
-
const merged_config = merge_vite_configs(
|
|
408
|
-
get_default_config({ config, input, ssr: true, outDir: `${output_dir}/server` }),
|
|
409
|
-
await get_vite_config(vite_config, vite_config_env)
|
|
410
|
-
);
|
|
411
|
-
|
|
412
|
-
remove_svelte_kit(merged_config);
|
|
413
|
-
|
|
414
|
-
const { chunks } = await create_build(merged_config);
|
|
415
|
-
|
|
416
|
-
/** @type {import('vite').Manifest} */
|
|
417
|
-
const vite_manifest = JSON.parse(fs__default.readFileSync(`${output_dir}/server/manifest.json`, 'utf-8'));
|
|
418
|
-
|
|
419
|
-
mkdirp(`${output_dir}/server/nodes`);
|
|
420
|
-
mkdirp(`${output_dir}/server/stylesheets`);
|
|
421
|
-
|
|
422
|
-
const stylesheet_lookup = new Map();
|
|
423
|
-
|
|
424
|
-
client.assets.forEach((asset) => {
|
|
425
|
-
if (asset.fileName.endsWith('.css')) {
|
|
426
|
-
if (asset.source.length < config.kit.inlineStyleThreshold) {
|
|
427
|
-
const index = stylesheet_lookup.size;
|
|
428
|
-
const file = `${output_dir}/server/stylesheets/${index}.js`;
|
|
429
|
-
|
|
430
|
-
fs__default.writeFileSync(file, `// ${asset.fileName}\nexport default ${s(asset.source)};`);
|
|
431
|
-
stylesheet_lookup.set(asset.fileName, index);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
manifest_data.components.forEach((component, i) => {
|
|
437
|
-
const entry = find_deps$1(client.vite_manifest, component, true);
|
|
438
|
-
|
|
439
|
-
const imports = [`import * as module from '../${vite_manifest[component].file}';`];
|
|
440
|
-
|
|
441
|
-
const exports = [
|
|
442
|
-
'export { module };',
|
|
443
|
-
`export const index = ${i};`,
|
|
444
|
-
`export const file = '${entry.file}';`,
|
|
445
|
-
`export const imports = ${s(entry.imports)};`,
|
|
446
|
-
`export const stylesheets = ${s(entry.stylesheets)};`
|
|
447
|
-
];
|
|
448
|
-
|
|
449
|
-
/** @type {string[]} */
|
|
450
|
-
const styles = [];
|
|
451
|
-
|
|
452
|
-
entry.stylesheets.forEach((file) => {
|
|
453
|
-
if (stylesheet_lookup.has(file)) {
|
|
454
|
-
const index = stylesheet_lookup.get(file);
|
|
455
|
-
const name = `stylesheet_${index}`;
|
|
456
|
-
imports.push(`import ${name} from '../stylesheets/${index}.js';`);
|
|
457
|
-
styles.push(`\t${s(file)}: ${name}`);
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
if (styles.length > 0) {
|
|
462
|
-
exports.push(`export const inline_styles = () => ({\n${styles.join(',\n')}\n});`);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
const out = `${output_dir}/server/nodes/${i}.js`;
|
|
466
|
-
fs__default.writeFileSync(out, `${imports.join('\n')}\n\n${exports.join('\n')}\n`);
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
return {
|
|
470
|
-
chunks,
|
|
471
|
-
vite_manifest,
|
|
472
|
-
methods: get_methods(cwd, chunks, manifest_data)
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* @param {string} cwd
|
|
478
|
-
* @param {import('rollup').OutputChunk[]} output
|
|
479
|
-
* @param {import('types').ManifestData} manifest_data
|
|
480
|
-
*/
|
|
481
|
-
function get_methods(cwd, output, manifest_data) {
|
|
482
|
-
/** @type {Record<string, string[]>} */
|
|
483
|
-
const lookup = {};
|
|
484
|
-
output.forEach((chunk) => {
|
|
485
|
-
if (!chunk.facadeModuleId) return;
|
|
486
|
-
const id = chunk.facadeModuleId.slice(cwd.length + 1);
|
|
487
|
-
lookup[id] = chunk.exports;
|
|
488
|
-
});
|
|
489
|
-
|
|
490
|
-
/** @type {Record<string, import('types').HttpMethod[]>} */
|
|
491
|
-
const methods = {};
|
|
492
|
-
manifest_data.routes.forEach((route) => {
|
|
493
|
-
const file = route.type === 'endpoint' ? route.file : route.shadow;
|
|
494
|
-
|
|
495
|
-
if (file && lookup[file]) {
|
|
496
|
-
methods[file] = lookup[file].filter(is_http_method);
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
return methods;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* @param {{
|
|
505
|
-
* config: import('types').ValidatedConfig;
|
|
506
|
-
* vite_config: import('vite').ResolvedConfig;
|
|
507
|
-
* vite_config_env: import('vite').ConfigEnv;
|
|
508
|
-
* manifest_data: import('types').ManifestData;
|
|
509
|
-
* output_dir: string;
|
|
510
|
-
* service_worker_entry_file: string | null;
|
|
511
|
-
* }} options
|
|
512
|
-
* @param {import('types').Prerendered} prerendered
|
|
513
|
-
* @param {import('vite').Manifest} client_manifest
|
|
514
|
-
*/
|
|
515
|
-
async function build_service_worker(
|
|
516
|
-
{ config, vite_config, vite_config_env, manifest_data, output_dir, service_worker_entry_file },
|
|
517
|
-
prerendered,
|
|
518
|
-
client_manifest
|
|
519
|
-
) {
|
|
520
|
-
const build = new Set();
|
|
521
|
-
for (const key in client_manifest) {
|
|
522
|
-
const { file, css = [], assets = [] } = client_manifest[key];
|
|
523
|
-
build.add(file);
|
|
524
|
-
css.forEach((file) => build.add(file));
|
|
525
|
-
assets.forEach((file) => build.add(file));
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
const service_worker = `${config.kit.outDir}/generated/service-worker.js`;
|
|
529
|
-
|
|
530
|
-
fs__default.writeFileSync(
|
|
531
|
-
service_worker,
|
|
532
|
-
`
|
|
533
|
-
// TODO remove for 1.0
|
|
534
|
-
export const timestamp = {
|
|
535
|
-
toString: () => {
|
|
536
|
-
throw new Error('\`timestamp\` has been removed from $service-worker. Use \`version\` instead');
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
|
|
540
|
-
export const build = [
|
|
541
|
-
${Array.from(build)
|
|
542
|
-
.map((file) => `${s(`${config.kit.paths.base}/${file}`)}`)
|
|
543
|
-
.join(',\n\t\t\t\t')}
|
|
544
|
-
];
|
|
545
|
-
|
|
546
|
-
export const files = [
|
|
547
|
-
${manifest_data.assets
|
|
548
|
-
.filter((asset) => config.kit.serviceWorker.files(asset.file))
|
|
549
|
-
.map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
|
|
550
|
-
.join(',\n\t\t\t\t')}
|
|
551
|
-
];
|
|
552
|
-
|
|
553
|
-
export const prerendered = [
|
|
554
|
-
${prerendered.paths.map((path) => s(path)).join(',\n\t\t\t\t')}
|
|
555
|
-
];
|
|
556
|
-
|
|
557
|
-
export const version = ${s(config.kit.version.name)};
|
|
558
|
-
`
|
|
559
|
-
.replace(/^\t{3}/gm, '')
|
|
560
|
-
.trim()
|
|
561
|
-
);
|
|
562
|
-
|
|
563
|
-
const merged_config = merge_vite_configs(await get_vite_config(vite_config, vite_config_env), {
|
|
564
|
-
base: assets_base(config.kit),
|
|
565
|
-
build: {
|
|
566
|
-
lib: {
|
|
567
|
-
entry: /** @type {string} */ (service_worker_entry_file),
|
|
568
|
-
name: 'app',
|
|
569
|
-
formats: ['es']
|
|
570
|
-
},
|
|
571
|
-
rollupOptions: {
|
|
572
|
-
output: {
|
|
573
|
-
entryFileNames: 'service-worker.js'
|
|
574
|
-
}
|
|
575
|
-
},
|
|
576
|
-
outDir: `${output_dir}/client`,
|
|
577
|
-
emptyOutDir: false
|
|
578
|
-
},
|
|
579
|
-
// @ts-expect-error
|
|
580
|
-
configFile: false,
|
|
581
|
-
resolve: {
|
|
582
|
-
alias: {
|
|
583
|
-
'$service-worker': service_worker,
|
|
584
|
-
$lib: config.kit.files.lib
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
});
|
|
588
|
-
|
|
589
|
-
remove_svelte_kit(merged_config);
|
|
590
|
-
|
|
591
|
-
await vite.build(merged_config);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
function totalist(dir, callback, pre='') {
|
|
595
|
-
dir = resolve('.', dir);
|
|
596
|
-
let arr = readdirSync(dir);
|
|
597
|
-
let i=0, abs, stats;
|
|
598
|
-
for (; i < arr.length; i++) {
|
|
599
|
-
abs = join(dir, arr[i]);
|
|
600
|
-
stats = statSync(abs);
|
|
601
|
-
stats.isDirectory()
|
|
602
|
-
? totalist(abs, callback, join(pre, arr[i]))
|
|
603
|
-
: callback(join(pre, arr[i]), abs, stats);
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
/**
|
|
608
|
-
* @typedef ParsedURL
|
|
609
|
-
* @type {import('.').ParsedURL}
|
|
610
|
-
*/
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* @typedef Request
|
|
614
|
-
* @property {string} url
|
|
615
|
-
* @property {ParsedURL} _parsedUrl
|
|
616
|
-
*/
|
|
617
|
-
|
|
618
|
-
/**
|
|
619
|
-
* @param {Request} req
|
|
620
|
-
* @returns {ParsedURL|void}
|
|
621
|
-
*/
|
|
622
|
-
function parse(req) {
|
|
623
|
-
let raw = req.url;
|
|
624
|
-
if (raw == null) return;
|
|
625
|
-
|
|
626
|
-
let prev = req._parsedUrl;
|
|
627
|
-
if (prev && prev.raw === raw) return prev;
|
|
628
|
-
|
|
629
|
-
let pathname=raw, search='', query;
|
|
630
|
-
|
|
631
|
-
if (raw.length > 1) {
|
|
632
|
-
let idx = raw.indexOf('?', 1);
|
|
633
|
-
|
|
634
|
-
if (idx !== -1) {
|
|
635
|
-
search = raw.substring(idx);
|
|
636
|
-
pathname = raw.substring(0, idx);
|
|
637
|
-
if (search.length > 1) {
|
|
638
|
-
query = qs.parse(search.substring(1));
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
return req._parsedUrl = { pathname, search, query, raw };
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
const mimes = {
|
|
647
|
-
"ez": "application/andrew-inset",
|
|
648
|
-
"aw": "application/applixware",
|
|
649
|
-
"atom": "application/atom+xml",
|
|
650
|
-
"atomcat": "application/atomcat+xml",
|
|
651
|
-
"atomdeleted": "application/atomdeleted+xml",
|
|
652
|
-
"atomsvc": "application/atomsvc+xml",
|
|
653
|
-
"dwd": "application/atsc-dwd+xml",
|
|
654
|
-
"held": "application/atsc-held+xml",
|
|
655
|
-
"rsat": "application/atsc-rsat+xml",
|
|
656
|
-
"bdoc": "application/bdoc",
|
|
657
|
-
"xcs": "application/calendar+xml",
|
|
658
|
-
"ccxml": "application/ccxml+xml",
|
|
659
|
-
"cdfx": "application/cdfx+xml",
|
|
660
|
-
"cdmia": "application/cdmi-capability",
|
|
661
|
-
"cdmic": "application/cdmi-container",
|
|
662
|
-
"cdmid": "application/cdmi-domain",
|
|
663
|
-
"cdmio": "application/cdmi-object",
|
|
664
|
-
"cdmiq": "application/cdmi-queue",
|
|
665
|
-
"cu": "application/cu-seeme",
|
|
666
|
-
"mpd": "application/dash+xml",
|
|
667
|
-
"davmount": "application/davmount+xml",
|
|
668
|
-
"dbk": "application/docbook+xml",
|
|
669
|
-
"dssc": "application/dssc+der",
|
|
670
|
-
"xdssc": "application/dssc+xml",
|
|
671
|
-
"es": "application/ecmascript",
|
|
672
|
-
"ecma": "application/ecmascript",
|
|
673
|
-
"emma": "application/emma+xml",
|
|
674
|
-
"emotionml": "application/emotionml+xml",
|
|
675
|
-
"epub": "application/epub+zip",
|
|
676
|
-
"exi": "application/exi",
|
|
677
|
-
"fdt": "application/fdt+xml",
|
|
678
|
-
"pfr": "application/font-tdpfr",
|
|
679
|
-
"geojson": "application/geo+json",
|
|
680
|
-
"gml": "application/gml+xml",
|
|
681
|
-
"gpx": "application/gpx+xml",
|
|
682
|
-
"gxf": "application/gxf",
|
|
683
|
-
"gz": "application/gzip",
|
|
684
|
-
"hjson": "application/hjson",
|
|
685
|
-
"stk": "application/hyperstudio",
|
|
686
|
-
"ink": "application/inkml+xml",
|
|
687
|
-
"inkml": "application/inkml+xml",
|
|
688
|
-
"ipfix": "application/ipfix",
|
|
689
|
-
"its": "application/its+xml",
|
|
690
|
-
"jar": "application/java-archive",
|
|
691
|
-
"war": "application/java-archive",
|
|
692
|
-
"ear": "application/java-archive",
|
|
693
|
-
"ser": "application/java-serialized-object",
|
|
694
|
-
"class": "application/java-vm",
|
|
695
|
-
"js": "application/javascript",
|
|
696
|
-
"mjs": "application/javascript",
|
|
697
|
-
"json": "application/json",
|
|
698
|
-
"map": "application/json",
|
|
699
|
-
"json5": "application/json5",
|
|
700
|
-
"jsonml": "application/jsonml+json",
|
|
701
|
-
"jsonld": "application/ld+json",
|
|
702
|
-
"lgr": "application/lgr+xml",
|
|
703
|
-
"lostxml": "application/lost+xml",
|
|
704
|
-
"hqx": "application/mac-binhex40",
|
|
705
|
-
"cpt": "application/mac-compactpro",
|
|
706
|
-
"mads": "application/mads+xml",
|
|
707
|
-
"webmanifest": "application/manifest+json",
|
|
708
|
-
"mrc": "application/marc",
|
|
709
|
-
"mrcx": "application/marcxml+xml",
|
|
710
|
-
"ma": "application/mathematica",
|
|
711
|
-
"nb": "application/mathematica",
|
|
712
|
-
"mb": "application/mathematica",
|
|
713
|
-
"mathml": "application/mathml+xml",
|
|
714
|
-
"mbox": "application/mbox",
|
|
715
|
-
"mscml": "application/mediaservercontrol+xml",
|
|
716
|
-
"metalink": "application/metalink+xml",
|
|
717
|
-
"meta4": "application/metalink4+xml",
|
|
718
|
-
"mets": "application/mets+xml",
|
|
719
|
-
"maei": "application/mmt-aei+xml",
|
|
720
|
-
"musd": "application/mmt-usd+xml",
|
|
721
|
-
"mods": "application/mods+xml",
|
|
722
|
-
"m21": "application/mp21",
|
|
723
|
-
"mp21": "application/mp21",
|
|
724
|
-
"mp4s": "application/mp4",
|
|
725
|
-
"m4p": "application/mp4",
|
|
726
|
-
"doc": "application/msword",
|
|
727
|
-
"dot": "application/msword",
|
|
728
|
-
"mxf": "application/mxf",
|
|
729
|
-
"nq": "application/n-quads",
|
|
730
|
-
"nt": "application/n-triples",
|
|
731
|
-
"cjs": "application/node",
|
|
732
|
-
"bin": "application/octet-stream",
|
|
733
|
-
"dms": "application/octet-stream",
|
|
734
|
-
"lrf": "application/octet-stream",
|
|
735
|
-
"mar": "application/octet-stream",
|
|
736
|
-
"so": "application/octet-stream",
|
|
737
|
-
"dist": "application/octet-stream",
|
|
738
|
-
"distz": "application/octet-stream",
|
|
739
|
-
"pkg": "application/octet-stream",
|
|
740
|
-
"bpk": "application/octet-stream",
|
|
741
|
-
"dump": "application/octet-stream",
|
|
742
|
-
"elc": "application/octet-stream",
|
|
743
|
-
"deploy": "application/octet-stream",
|
|
744
|
-
"exe": "application/octet-stream",
|
|
745
|
-
"dll": "application/octet-stream",
|
|
746
|
-
"deb": "application/octet-stream",
|
|
747
|
-
"dmg": "application/octet-stream",
|
|
748
|
-
"iso": "application/octet-stream",
|
|
749
|
-
"img": "application/octet-stream",
|
|
750
|
-
"msi": "application/octet-stream",
|
|
751
|
-
"msp": "application/octet-stream",
|
|
752
|
-
"msm": "application/octet-stream",
|
|
753
|
-
"buffer": "application/octet-stream",
|
|
754
|
-
"oda": "application/oda",
|
|
755
|
-
"opf": "application/oebps-package+xml",
|
|
756
|
-
"ogx": "application/ogg",
|
|
757
|
-
"omdoc": "application/omdoc+xml",
|
|
758
|
-
"onetoc": "application/onenote",
|
|
759
|
-
"onetoc2": "application/onenote",
|
|
760
|
-
"onetmp": "application/onenote",
|
|
761
|
-
"onepkg": "application/onenote",
|
|
762
|
-
"oxps": "application/oxps",
|
|
763
|
-
"relo": "application/p2p-overlay+xml",
|
|
764
|
-
"xer": "application/patch-ops-error+xml",
|
|
765
|
-
"pdf": "application/pdf",
|
|
766
|
-
"pgp": "application/pgp-encrypted",
|
|
767
|
-
"asc": "application/pgp-signature",
|
|
768
|
-
"sig": "application/pgp-signature",
|
|
769
|
-
"prf": "application/pics-rules",
|
|
770
|
-
"p10": "application/pkcs10",
|
|
771
|
-
"p7m": "application/pkcs7-mime",
|
|
772
|
-
"p7c": "application/pkcs7-mime",
|
|
773
|
-
"p7s": "application/pkcs7-signature",
|
|
774
|
-
"p8": "application/pkcs8",
|
|
775
|
-
"ac": "application/pkix-attr-cert",
|
|
776
|
-
"cer": "application/pkix-cert",
|
|
777
|
-
"crl": "application/pkix-crl",
|
|
778
|
-
"pkipath": "application/pkix-pkipath",
|
|
779
|
-
"pki": "application/pkixcmp",
|
|
780
|
-
"pls": "application/pls+xml",
|
|
781
|
-
"ai": "application/postscript",
|
|
782
|
-
"eps": "application/postscript",
|
|
783
|
-
"ps": "application/postscript",
|
|
784
|
-
"provx": "application/provenance+xml",
|
|
785
|
-
"cww": "application/prs.cww",
|
|
786
|
-
"pskcxml": "application/pskc+xml",
|
|
787
|
-
"raml": "application/raml+yaml",
|
|
788
|
-
"rdf": "application/rdf+xml",
|
|
789
|
-
"owl": "application/rdf+xml",
|
|
790
|
-
"rif": "application/reginfo+xml",
|
|
791
|
-
"rnc": "application/relax-ng-compact-syntax",
|
|
792
|
-
"rl": "application/resource-lists+xml",
|
|
793
|
-
"rld": "application/resource-lists-diff+xml",
|
|
794
|
-
"rs": "application/rls-services+xml",
|
|
795
|
-
"rapd": "application/route-apd+xml",
|
|
796
|
-
"sls": "application/route-s-tsid+xml",
|
|
797
|
-
"rusd": "application/route-usd+xml",
|
|
798
|
-
"gbr": "application/rpki-ghostbusters",
|
|
799
|
-
"mft": "application/rpki-manifest",
|
|
800
|
-
"roa": "application/rpki-roa",
|
|
801
|
-
"rsd": "application/rsd+xml",
|
|
802
|
-
"rss": "application/rss+xml",
|
|
803
|
-
"rtf": "application/rtf",
|
|
804
|
-
"sbml": "application/sbml+xml",
|
|
805
|
-
"scq": "application/scvp-cv-request",
|
|
806
|
-
"scs": "application/scvp-cv-response",
|
|
807
|
-
"spq": "application/scvp-vp-request",
|
|
808
|
-
"spp": "application/scvp-vp-response",
|
|
809
|
-
"sdp": "application/sdp",
|
|
810
|
-
"senmlx": "application/senml+xml",
|
|
811
|
-
"sensmlx": "application/sensml+xml",
|
|
812
|
-
"setpay": "application/set-payment-initiation",
|
|
813
|
-
"setreg": "application/set-registration-initiation",
|
|
814
|
-
"shf": "application/shf+xml",
|
|
815
|
-
"siv": "application/sieve",
|
|
816
|
-
"sieve": "application/sieve",
|
|
817
|
-
"smi": "application/smil+xml",
|
|
818
|
-
"smil": "application/smil+xml",
|
|
819
|
-
"rq": "application/sparql-query",
|
|
820
|
-
"srx": "application/sparql-results+xml",
|
|
821
|
-
"gram": "application/srgs",
|
|
822
|
-
"grxml": "application/srgs+xml",
|
|
823
|
-
"sru": "application/sru+xml",
|
|
824
|
-
"ssdl": "application/ssdl+xml",
|
|
825
|
-
"ssml": "application/ssml+xml",
|
|
826
|
-
"swidtag": "application/swid+xml",
|
|
827
|
-
"tei": "application/tei+xml",
|
|
828
|
-
"teicorpus": "application/tei+xml",
|
|
829
|
-
"tfi": "application/thraud+xml",
|
|
830
|
-
"tsd": "application/timestamped-data",
|
|
831
|
-
"toml": "application/toml",
|
|
832
|
-
"trig": "application/trig",
|
|
833
|
-
"ttml": "application/ttml+xml",
|
|
834
|
-
"ubj": "application/ubjson",
|
|
835
|
-
"rsheet": "application/urc-ressheet+xml",
|
|
836
|
-
"td": "application/urc-targetdesc+xml",
|
|
837
|
-
"vxml": "application/voicexml+xml",
|
|
838
|
-
"wasm": "application/wasm",
|
|
839
|
-
"wgt": "application/widget",
|
|
840
|
-
"hlp": "application/winhlp",
|
|
841
|
-
"wsdl": "application/wsdl+xml",
|
|
842
|
-
"wspolicy": "application/wspolicy+xml",
|
|
843
|
-
"xaml": "application/xaml+xml",
|
|
844
|
-
"xav": "application/xcap-att+xml",
|
|
845
|
-
"xca": "application/xcap-caps+xml",
|
|
846
|
-
"xdf": "application/xcap-diff+xml",
|
|
847
|
-
"xel": "application/xcap-el+xml",
|
|
848
|
-
"xns": "application/xcap-ns+xml",
|
|
849
|
-
"xenc": "application/xenc+xml",
|
|
850
|
-
"xhtml": "application/xhtml+xml",
|
|
851
|
-
"xht": "application/xhtml+xml",
|
|
852
|
-
"xlf": "application/xliff+xml",
|
|
853
|
-
"xml": "application/xml",
|
|
854
|
-
"xsl": "application/xml",
|
|
855
|
-
"xsd": "application/xml",
|
|
856
|
-
"rng": "application/xml",
|
|
857
|
-
"dtd": "application/xml-dtd",
|
|
858
|
-
"xop": "application/xop+xml",
|
|
859
|
-
"xpl": "application/xproc+xml",
|
|
860
|
-
"xslt": "application/xml",
|
|
861
|
-
"xspf": "application/xspf+xml",
|
|
862
|
-
"mxml": "application/xv+xml",
|
|
863
|
-
"xhvml": "application/xv+xml",
|
|
864
|
-
"xvml": "application/xv+xml",
|
|
865
|
-
"xvm": "application/xv+xml",
|
|
866
|
-
"yang": "application/yang",
|
|
867
|
-
"yin": "application/yin+xml",
|
|
868
|
-
"zip": "application/zip",
|
|
869
|
-
"3gpp": "video/3gpp",
|
|
870
|
-
"adp": "audio/adpcm",
|
|
871
|
-
"amr": "audio/amr",
|
|
872
|
-
"au": "audio/basic",
|
|
873
|
-
"snd": "audio/basic",
|
|
874
|
-
"mid": "audio/midi",
|
|
875
|
-
"midi": "audio/midi",
|
|
876
|
-
"kar": "audio/midi",
|
|
877
|
-
"rmi": "audio/midi",
|
|
878
|
-
"mxmf": "audio/mobile-xmf",
|
|
879
|
-
"mp3": "audio/mpeg",
|
|
880
|
-
"m4a": "audio/mp4",
|
|
881
|
-
"mp4a": "audio/mp4",
|
|
882
|
-
"mpga": "audio/mpeg",
|
|
883
|
-
"mp2": "audio/mpeg",
|
|
884
|
-
"mp2a": "audio/mpeg",
|
|
885
|
-
"m2a": "audio/mpeg",
|
|
886
|
-
"m3a": "audio/mpeg",
|
|
887
|
-
"oga": "audio/ogg",
|
|
888
|
-
"ogg": "audio/ogg",
|
|
889
|
-
"spx": "audio/ogg",
|
|
890
|
-
"opus": "audio/ogg",
|
|
891
|
-
"s3m": "audio/s3m",
|
|
892
|
-
"sil": "audio/silk",
|
|
893
|
-
"wav": "audio/wav",
|
|
894
|
-
"weba": "audio/webm",
|
|
895
|
-
"xm": "audio/xm",
|
|
896
|
-
"ttc": "font/collection",
|
|
897
|
-
"otf": "font/otf",
|
|
898
|
-
"ttf": "font/ttf",
|
|
899
|
-
"woff": "font/woff",
|
|
900
|
-
"woff2": "font/woff2",
|
|
901
|
-
"exr": "image/aces",
|
|
902
|
-
"apng": "image/apng",
|
|
903
|
-
"avif": "image/avif",
|
|
904
|
-
"bmp": "image/bmp",
|
|
905
|
-
"cgm": "image/cgm",
|
|
906
|
-
"drle": "image/dicom-rle",
|
|
907
|
-
"emf": "image/emf",
|
|
908
|
-
"fits": "image/fits",
|
|
909
|
-
"g3": "image/g3fax",
|
|
910
|
-
"gif": "image/gif",
|
|
911
|
-
"heic": "image/heic",
|
|
912
|
-
"heics": "image/heic-sequence",
|
|
913
|
-
"heif": "image/heif",
|
|
914
|
-
"heifs": "image/heif-sequence",
|
|
915
|
-
"hej2": "image/hej2k",
|
|
916
|
-
"hsj2": "image/hsj2",
|
|
917
|
-
"ief": "image/ief",
|
|
918
|
-
"jls": "image/jls",
|
|
919
|
-
"jp2": "image/jp2",
|
|
920
|
-
"jpg2": "image/jp2",
|
|
921
|
-
"jpeg": "image/jpeg",
|
|
922
|
-
"jpg": "image/jpeg",
|
|
923
|
-
"jpe": "image/jpeg",
|
|
924
|
-
"jph": "image/jph",
|
|
925
|
-
"jhc": "image/jphc",
|
|
926
|
-
"jpm": "image/jpm",
|
|
927
|
-
"jpx": "image/jpx",
|
|
928
|
-
"jpf": "image/jpx",
|
|
929
|
-
"jxr": "image/jxr",
|
|
930
|
-
"jxra": "image/jxra",
|
|
931
|
-
"jxrs": "image/jxrs",
|
|
932
|
-
"jxs": "image/jxs",
|
|
933
|
-
"jxsc": "image/jxsc",
|
|
934
|
-
"jxsi": "image/jxsi",
|
|
935
|
-
"jxss": "image/jxss",
|
|
936
|
-
"ktx": "image/ktx",
|
|
937
|
-
"ktx2": "image/ktx2",
|
|
938
|
-
"png": "image/png",
|
|
939
|
-
"btif": "image/prs.btif",
|
|
940
|
-
"pti": "image/prs.pti",
|
|
941
|
-
"sgi": "image/sgi",
|
|
942
|
-
"svg": "image/svg+xml",
|
|
943
|
-
"svgz": "image/svg+xml",
|
|
944
|
-
"t38": "image/t38",
|
|
945
|
-
"tif": "image/tiff",
|
|
946
|
-
"tiff": "image/tiff",
|
|
947
|
-
"tfx": "image/tiff-fx",
|
|
948
|
-
"webp": "image/webp",
|
|
949
|
-
"wmf": "image/wmf",
|
|
950
|
-
"disposition-notification": "message/disposition-notification",
|
|
951
|
-
"u8msg": "message/global",
|
|
952
|
-
"u8dsn": "message/global-delivery-status",
|
|
953
|
-
"u8mdn": "message/global-disposition-notification",
|
|
954
|
-
"u8hdr": "message/global-headers",
|
|
955
|
-
"eml": "message/rfc822",
|
|
956
|
-
"mime": "message/rfc822",
|
|
957
|
-
"3mf": "model/3mf",
|
|
958
|
-
"gltf": "model/gltf+json",
|
|
959
|
-
"glb": "model/gltf-binary",
|
|
960
|
-
"igs": "model/iges",
|
|
961
|
-
"iges": "model/iges",
|
|
962
|
-
"msh": "model/mesh",
|
|
963
|
-
"mesh": "model/mesh",
|
|
964
|
-
"silo": "model/mesh",
|
|
965
|
-
"mtl": "model/mtl",
|
|
966
|
-
"obj": "model/obj",
|
|
967
|
-
"stpz": "model/step+zip",
|
|
968
|
-
"stpxz": "model/step-xml+zip",
|
|
969
|
-
"stl": "model/stl",
|
|
970
|
-
"wrl": "model/vrml",
|
|
971
|
-
"vrml": "model/vrml",
|
|
972
|
-
"x3db": "model/x3d+fastinfoset",
|
|
973
|
-
"x3dbz": "model/x3d+binary",
|
|
974
|
-
"x3dv": "model/x3d-vrml",
|
|
975
|
-
"x3dvz": "model/x3d+vrml",
|
|
976
|
-
"x3d": "model/x3d+xml",
|
|
977
|
-
"x3dz": "model/x3d+xml",
|
|
978
|
-
"appcache": "text/cache-manifest",
|
|
979
|
-
"manifest": "text/cache-manifest",
|
|
980
|
-
"ics": "text/calendar",
|
|
981
|
-
"ifb": "text/calendar",
|
|
982
|
-
"coffee": "text/coffeescript",
|
|
983
|
-
"litcoffee": "text/coffeescript",
|
|
984
|
-
"css": "text/css",
|
|
985
|
-
"csv": "text/csv",
|
|
986
|
-
"html": "text/html",
|
|
987
|
-
"htm": "text/html",
|
|
988
|
-
"shtml": "text/html",
|
|
989
|
-
"jade": "text/jade",
|
|
990
|
-
"jsx": "text/jsx",
|
|
991
|
-
"less": "text/less",
|
|
992
|
-
"markdown": "text/markdown",
|
|
993
|
-
"md": "text/markdown",
|
|
994
|
-
"mml": "text/mathml",
|
|
995
|
-
"mdx": "text/mdx",
|
|
996
|
-
"n3": "text/n3",
|
|
997
|
-
"txt": "text/plain",
|
|
998
|
-
"text": "text/plain",
|
|
999
|
-
"conf": "text/plain",
|
|
1000
|
-
"def": "text/plain",
|
|
1001
|
-
"list": "text/plain",
|
|
1002
|
-
"log": "text/plain",
|
|
1003
|
-
"in": "text/plain",
|
|
1004
|
-
"ini": "text/plain",
|
|
1005
|
-
"dsc": "text/prs.lines.tag",
|
|
1006
|
-
"rtx": "text/richtext",
|
|
1007
|
-
"sgml": "text/sgml",
|
|
1008
|
-
"sgm": "text/sgml",
|
|
1009
|
-
"shex": "text/shex",
|
|
1010
|
-
"slim": "text/slim",
|
|
1011
|
-
"slm": "text/slim",
|
|
1012
|
-
"spdx": "text/spdx",
|
|
1013
|
-
"stylus": "text/stylus",
|
|
1014
|
-
"styl": "text/stylus",
|
|
1015
|
-
"tsv": "text/tab-separated-values",
|
|
1016
|
-
"t": "text/troff",
|
|
1017
|
-
"tr": "text/troff",
|
|
1018
|
-
"roff": "text/troff",
|
|
1019
|
-
"man": "text/troff",
|
|
1020
|
-
"me": "text/troff",
|
|
1021
|
-
"ms": "text/troff",
|
|
1022
|
-
"ttl": "text/turtle",
|
|
1023
|
-
"uri": "text/uri-list",
|
|
1024
|
-
"uris": "text/uri-list",
|
|
1025
|
-
"urls": "text/uri-list",
|
|
1026
|
-
"vcard": "text/vcard",
|
|
1027
|
-
"vtt": "text/vtt",
|
|
1028
|
-
"yaml": "text/yaml",
|
|
1029
|
-
"yml": "text/yaml",
|
|
1030
|
-
"3gp": "video/3gpp",
|
|
1031
|
-
"3g2": "video/3gpp2",
|
|
1032
|
-
"h261": "video/h261",
|
|
1033
|
-
"h263": "video/h263",
|
|
1034
|
-
"h264": "video/h264",
|
|
1035
|
-
"m4s": "video/iso.segment",
|
|
1036
|
-
"jpgv": "video/jpeg",
|
|
1037
|
-
"jpgm": "image/jpm",
|
|
1038
|
-
"mj2": "video/mj2",
|
|
1039
|
-
"mjp2": "video/mj2",
|
|
1040
|
-
"ts": "video/mp2t",
|
|
1041
|
-
"mp4": "video/mp4",
|
|
1042
|
-
"mp4v": "video/mp4",
|
|
1043
|
-
"mpg4": "video/mp4",
|
|
1044
|
-
"mpeg": "video/mpeg",
|
|
1045
|
-
"mpg": "video/mpeg",
|
|
1046
|
-
"mpe": "video/mpeg",
|
|
1047
|
-
"m1v": "video/mpeg",
|
|
1048
|
-
"m2v": "video/mpeg",
|
|
1049
|
-
"ogv": "video/ogg",
|
|
1050
|
-
"qt": "video/quicktime",
|
|
1051
|
-
"mov": "video/quicktime",
|
|
1052
|
-
"webm": "video/webm"
|
|
1053
|
-
};
|
|
1054
|
-
|
|
1055
|
-
function lookup(extn) {
|
|
1056
|
-
let tmp = ('' + extn).trim().toLowerCase();
|
|
1057
|
-
let idx = tmp.lastIndexOf('.');
|
|
1058
|
-
return mimes[!~idx ? tmp : tmp.substring(++idx)];
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
const noop = () => {};
|
|
1062
|
-
|
|
1063
|
-
function isMatch(uri, arr) {
|
|
1064
|
-
for (let i=0; i < arr.length; i++) {
|
|
1065
|
-
if (arr[i].test(uri)) return true;
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
function toAssume(uri, extns) {
|
|
1070
|
-
let i=0, x, len=uri.length - 1;
|
|
1071
|
-
if (uri.charCodeAt(len) === 47) {
|
|
1072
|
-
uri = uri.substring(0, len);
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
let arr=[], tmp=`${uri}/index`;
|
|
1076
|
-
for (; i < extns.length; i++) {
|
|
1077
|
-
x = extns[i] ? `.${extns[i]}` : '';
|
|
1078
|
-
if (uri) arr.push(uri + x);
|
|
1079
|
-
arr.push(tmp + x);
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
return arr;
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
function viaCache(cache, uri, extns) {
|
|
1086
|
-
let i=0, data, arr=toAssume(uri, extns);
|
|
1087
|
-
for (; i < arr.length; i++) {
|
|
1088
|
-
if (data = cache[arr[i]]) return data;
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
function viaLocal(dir, isEtag, uri, extns) {
|
|
1093
|
-
let i=0, arr=toAssume(uri, extns);
|
|
1094
|
-
let abs, stats, name, headers;
|
|
1095
|
-
for (; i < arr.length; i++) {
|
|
1096
|
-
abs = normalize(join(dir, name=arr[i]));
|
|
1097
|
-
if (abs.startsWith(dir) && fs.existsSync(abs)) {
|
|
1098
|
-
stats = fs.statSync(abs);
|
|
1099
|
-
if (stats.isDirectory()) continue;
|
|
1100
|
-
headers = toHeaders(name, stats, isEtag);
|
|
1101
|
-
headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
|
|
1102
|
-
return { abs, stats, headers };
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
function is404(req, res) {
|
|
1108
|
-
return (res.statusCode=404,res.end());
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
function send(req, res, file, stats, headers) {
|
|
1112
|
-
let code=200, tmp, opts={};
|
|
1113
|
-
headers = { ...headers };
|
|
1114
|
-
|
|
1115
|
-
for (let key in headers) {
|
|
1116
|
-
tmp = res.getHeader(key);
|
|
1117
|
-
if (tmp) headers[key] = tmp;
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
if (tmp = res.getHeader('content-type')) {
|
|
1121
|
-
headers['Content-Type'] = tmp;
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
if (req.headers.range) {
|
|
1125
|
-
code = 206;
|
|
1126
|
-
let [x, y] = req.headers.range.replace('bytes=', '').split('-');
|
|
1127
|
-
let end = opts.end = parseInt(y, 10) || stats.size - 1;
|
|
1128
|
-
let start = opts.start = parseInt(x, 10) || 0;
|
|
1129
|
-
|
|
1130
|
-
if (start >= stats.size || end >= stats.size) {
|
|
1131
|
-
res.setHeader('Content-Range', `bytes */${stats.size}`);
|
|
1132
|
-
res.statusCode = 416;
|
|
1133
|
-
return res.end();
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
|
|
1137
|
-
headers['Content-Length'] = (end - start + 1);
|
|
1138
|
-
headers['Accept-Ranges'] = 'bytes';
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
res.writeHead(code, headers);
|
|
1142
|
-
fs.createReadStream(file, opts).pipe(res);
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
const ENCODING = {
|
|
1146
|
-
'.br': 'br',
|
|
1147
|
-
'.gz': 'gzip',
|
|
1148
|
-
};
|
|
1149
|
-
|
|
1150
|
-
function toHeaders(name, stats, isEtag) {
|
|
1151
|
-
let enc = ENCODING[name.slice(-3)];
|
|
1152
|
-
|
|
1153
|
-
let ctype = lookup(name.slice(0, enc && -3)) || '';
|
|
1154
|
-
if (ctype === 'text/html') ctype += ';charset=utf-8';
|
|
1155
|
-
|
|
1156
|
-
let headers = {
|
|
1157
|
-
'Content-Length': stats.size,
|
|
1158
|
-
'Content-Type': ctype,
|
|
1159
|
-
'Last-Modified': stats.mtime.toUTCString(),
|
|
1160
|
-
};
|
|
1161
|
-
|
|
1162
|
-
if (enc) headers['Content-Encoding'] = enc;
|
|
1163
|
-
if (isEtag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
|
|
1164
|
-
|
|
1165
|
-
return headers;
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
|
-
function sirv (dir, opts={}) {
|
|
1169
|
-
dir = resolve(dir || '.');
|
|
1170
|
-
|
|
1171
|
-
let isNotFound = opts.onNoMatch || is404;
|
|
1172
|
-
let setHeaders = opts.setHeaders || noop;
|
|
1173
|
-
|
|
1174
|
-
let extensions = opts.extensions || ['html', 'htm'];
|
|
1175
|
-
let gzips = opts.gzip && extensions.map(x => `${x}.gz`).concat('gz');
|
|
1176
|
-
let brots = opts.brotli && extensions.map(x => `${x}.br`).concat('br');
|
|
1177
|
-
|
|
1178
|
-
const FILES = {};
|
|
1179
|
-
|
|
1180
|
-
let fallback = '/';
|
|
1181
|
-
let isEtag = !!opts.etag;
|
|
1182
|
-
let isSPA = !!opts.single;
|
|
1183
|
-
if (typeof opts.single === 'string') {
|
|
1184
|
-
let idx = opts.single.lastIndexOf('.');
|
|
1185
|
-
fallback += !!~idx ? opts.single.substring(0, idx) : opts.single;
|
|
1186
|
-
}
|
|
1187
|
-
|
|
1188
|
-
let ignores = [];
|
|
1189
|
-
if (opts.ignores !== false) {
|
|
1190
|
-
ignores.push(/[/]([A-Za-z\s\d~$._-]+\.\w+){1,}$/); // any extn
|
|
1191
|
-
if (opts.dotfiles) ignores.push(/\/\.\w/);
|
|
1192
|
-
else ignores.push(/\/\.well-known/);
|
|
1193
|
-
[].concat(opts.ignores || []).forEach(x => {
|
|
1194
|
-
ignores.push(new RegExp(x, 'i'));
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
|
|
1199
|
-
if (cc && opts.immutable) cc += ',immutable';
|
|
1200
|
-
else if (cc && opts.maxAge === 0) cc += ',must-revalidate';
|
|
1201
|
-
|
|
1202
|
-
if (!opts.dev) {
|
|
1203
|
-
totalist(dir, (name, abs, stats) => {
|
|
1204
|
-
if (/\.well-known[\\+\/]/.test(name)) ; // keep
|
|
1205
|
-
else if (!opts.dotfiles && /(^\.|[\\+|\/+]\.)/.test(name)) return;
|
|
1206
|
-
|
|
1207
|
-
let headers = toHeaders(name, stats, isEtag);
|
|
1208
|
-
if (cc) headers['Cache-Control'] = cc;
|
|
1209
|
-
|
|
1210
|
-
FILES['/' + name.normalize().replace(/\\+/g, '/')] = { abs, stats, headers };
|
|
1211
|
-
});
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);
|
|
1215
|
-
|
|
1216
|
-
return function (req, res, next) {
|
|
1217
|
-
let extns = [''];
|
|
1218
|
-
let pathname = parse(req).pathname;
|
|
1219
|
-
let val = req.headers['accept-encoding'] || '';
|
|
1220
|
-
if (gzips && val.includes('gzip')) extns.unshift(...gzips);
|
|
1221
|
-
if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots);
|
|
1222
|
-
extns.push(...extensions); // [...br, ...gz, orig, ...exts]
|
|
1223
|
-
|
|
1224
|
-
if (pathname.indexOf('%') !== -1) {
|
|
1225
|
-
try { pathname = decodeURIComponent(pathname); }
|
|
1226
|
-
catch (err) { /* malform uri */ }
|
|
1227
|
-
}
|
|
1228
|
-
|
|
1229
|
-
let data = lookup(pathname, extns) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns);
|
|
1230
|
-
if (!data) return next ? next() : isNotFound(req, res);
|
|
1231
|
-
|
|
1232
|
-
if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
|
|
1233
|
-
res.writeHead(304);
|
|
1234
|
-
return res.end();
|
|
1235
|
-
}
|
|
1236
|
-
|
|
1237
|
-
if (gzips || brots) {
|
|
1238
|
-
res.setHeader('Vary', 'Accept-Encoding');
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
setHeaders(res, pathname, data.stats);
|
|
1242
|
-
send(req, res, data.abs, data.stats, data.headers);
|
|
1243
|
-
};
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
// in `vite dev` and `vite preview`, we use a fake asset path so that we can
|
|
1247
|
-
// serve local assets while verifying that requests are correctly prefixed
|
|
1248
|
-
const SVELTE_KIT_ASSETS = '/_svelte_kit_assets';
|
|
1249
|
-
|
|
1250
|
-
// Vite doesn't expose this so we just copy the list for now
|
|
1251
|
-
// https://github.com/vitejs/vite/blob/3edd1af56e980aef56641a5a51cf2932bb580d41/packages/vite/src/node/plugins/css.ts#L96
|
|
1252
|
-
const style_pattern = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/;
|
|
1253
|
-
|
|
1254
|
-
const cwd$1 = process.cwd();
|
|
1255
|
-
|
|
1256
|
-
/**
|
|
1257
|
-
* @param {import('vite').ViteDevServer} vite
|
|
1258
|
-
* @param {import('vite').ResolvedConfig} vite_config
|
|
1259
|
-
* @param {import('types').ValidatedConfig} svelte_config
|
|
1260
|
-
* @param {Set<string>} illegal_imports
|
|
1261
|
-
* @return {Promise<Promise<() => void>>}
|
|
1262
|
-
*/
|
|
1263
|
-
async function dev(vite, vite_config, svelte_config, illegal_imports) {
|
|
1264
|
-
installPolyfills();
|
|
1265
|
-
|
|
1266
|
-
init(svelte_config, vite_config.mode);
|
|
1267
|
-
|
|
1268
|
-
const runtime = get_runtime_prefix(svelte_config.kit);
|
|
1269
|
-
|
|
1270
|
-
/** @type {import('types').Respond} */
|
|
1271
|
-
const respond = (await import(`${runtime}/server/index.js`)).respond;
|
|
1272
|
-
|
|
1273
|
-
/** @type {import('types').SSRManifest} */
|
|
1274
|
-
let manifest;
|
|
1275
|
-
|
|
1276
|
-
function update_manifest() {
|
|
1277
|
-
const { manifest_data } = update(svelte_config);
|
|
1278
|
-
|
|
1279
|
-
manifest = {
|
|
1280
|
-
appDir: svelte_config.kit.appDir,
|
|
1281
|
-
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
|
|
1282
|
-
mimeTypes: get_mime_lookup(manifest_data),
|
|
1283
|
-
_: {
|
|
1284
|
-
entry: {
|
|
1285
|
-
file: `/@fs${runtime}/client/start.js`,
|
|
1286
|
-
imports: [],
|
|
1287
|
-
stylesheets: []
|
|
1288
|
-
},
|
|
1289
|
-
nodes: manifest_data.components.map((id, index) => {
|
|
1290
|
-
return async () => {
|
|
1291
|
-
const url = id.startsWith('..') ? `/@fs${path__default.posix.resolve(id)}` : `/${id}`;
|
|
1292
|
-
|
|
1293
|
-
const module = /** @type {import('types').SSRComponent} */ (
|
|
1294
|
-
await vite.ssrLoadModule(url)
|
|
1295
|
-
);
|
|
1296
|
-
|
|
1297
|
-
const node = await vite.moduleGraph.getModuleByUrl(url);
|
|
1298
|
-
if (!node) throw new Error(`Could not find node for ${url}`);
|
|
1299
|
-
|
|
1300
|
-
prevent_illegal_vite_imports(
|
|
1301
|
-
node,
|
|
1302
|
-
illegal_imports,
|
|
1303
|
-
[...svelte_config.extensions, ...svelte_config.kit.moduleExtensions],
|
|
1304
|
-
svelte_config.kit.outDir
|
|
1305
|
-
);
|
|
1306
|
-
|
|
1307
|
-
return {
|
|
1308
|
-
module,
|
|
1309
|
-
index,
|
|
1310
|
-
file: url.endsWith('.svelte') ? url : url + '?import',
|
|
1311
|
-
imports: [],
|
|
1312
|
-
stylesheets: [],
|
|
1313
|
-
// in dev we inline all styles to avoid FOUC
|
|
1314
|
-
inline_styles: async () => {
|
|
1315
|
-
const deps = new Set();
|
|
1316
|
-
await find_deps(vite, node, deps);
|
|
1317
|
-
|
|
1318
|
-
/** @type {Record<string, string>} */
|
|
1319
|
-
const styles = {};
|
|
1320
|
-
|
|
1321
|
-
for (const dep of deps) {
|
|
1322
|
-
const parsed = new URL$1(dep.url, 'http://localhost/');
|
|
1323
|
-
const query = parsed.searchParams;
|
|
1324
|
-
|
|
1325
|
-
if (
|
|
1326
|
-
style_pattern.test(dep.file) ||
|
|
1327
|
-
(query.has('svelte') && query.get('type') === 'style')
|
|
1328
|
-
) {
|
|
1329
|
-
try {
|
|
1330
|
-
const mod = await vite.ssrLoadModule(dep.url);
|
|
1331
|
-
styles[dep.url] = mod.default;
|
|
1332
|
-
} catch {
|
|
1333
|
-
// this can happen with dynamically imported modules, I think
|
|
1334
|
-
// because the Vite module graph doesn't distinguish between
|
|
1335
|
-
// static and dynamic imports? TODO investigate, submit fix
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
return styles;
|
|
1341
|
-
}
|
|
1342
|
-
};
|
|
1343
|
-
};
|
|
1344
|
-
}),
|
|
1345
|
-
routes: manifest_data.routes.map((route) => {
|
|
1346
|
-
const { pattern, names, types } = parse_route_id(route.id);
|
|
1347
|
-
|
|
1348
|
-
if (route.type === 'page') {
|
|
1349
|
-
return {
|
|
1350
|
-
type: 'page',
|
|
1351
|
-
id: route.id,
|
|
1352
|
-
pattern,
|
|
1353
|
-
names,
|
|
1354
|
-
types,
|
|
1355
|
-
shadow: route.shadow
|
|
1356
|
-
? async () => {
|
|
1357
|
-
const url = path__default.resolve(cwd$1, /** @type {string} */ (route.shadow));
|
|
1358
|
-
return await vite.ssrLoadModule(url);
|
|
1359
|
-
}
|
|
1360
|
-
: null,
|
|
1361
|
-
a: route.a.map((id) => (id ? manifest_data.components.indexOf(id) : undefined)),
|
|
1362
|
-
b: route.b.map((id) => (id ? manifest_data.components.indexOf(id) : undefined))
|
|
1363
|
-
};
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
return {
|
|
1367
|
-
type: 'endpoint',
|
|
1368
|
-
id: route.id,
|
|
1369
|
-
pattern,
|
|
1370
|
-
names,
|
|
1371
|
-
types,
|
|
1372
|
-
load: async () => {
|
|
1373
|
-
const url = path__default.resolve(cwd$1, route.file);
|
|
1374
|
-
return await vite.ssrLoadModule(url);
|
|
1375
|
-
}
|
|
1376
|
-
};
|
|
1377
|
-
}),
|
|
1378
|
-
matchers: async () => {
|
|
1379
|
-
/** @type {Record<string, import('types').ParamMatcher>} */
|
|
1380
|
-
const matchers = {};
|
|
1381
|
-
|
|
1382
|
-
for (const key in manifest_data.matchers) {
|
|
1383
|
-
const file = manifest_data.matchers[key];
|
|
1384
|
-
const url = path__default.resolve(cwd$1, file);
|
|
1385
|
-
const module = await vite.ssrLoadModule(url);
|
|
1386
|
-
|
|
1387
|
-
if (module.match) {
|
|
1388
|
-
matchers[key] = module.match;
|
|
1389
|
-
} else {
|
|
1390
|
-
throw new Error(`${file} does not export a \`match\` function`);
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
return matchers;
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
};
|
|
1398
|
-
}
|
|
1399
|
-
|
|
1400
|
-
/** @param {Error} error */
|
|
1401
|
-
function fix_stack_trace(error) {
|
|
1402
|
-
return error.stack ? vite.ssrRewriteStacktrace(error.stack) : error.stack;
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
update_manifest();
|
|
1406
|
-
|
|
1407
|
-
for (const event of ['add', 'unlink']) {
|
|
1408
|
-
vite.watcher.on(event, (file) => {
|
|
1409
|
-
if (file.startsWith(svelte_config.kit.files.routes + path__default.sep)) {
|
|
1410
|
-
update_manifest();
|
|
1411
|
-
}
|
|
1412
|
-
});
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base;
|
|
1416
|
-
const asset_server = sirv(svelte_config.kit.files.assets, {
|
|
1417
|
-
dev: true,
|
|
1418
|
-
etag: true,
|
|
1419
|
-
maxAge: 0,
|
|
1420
|
-
extensions: []
|
|
1421
|
-
});
|
|
1422
|
-
|
|
1423
|
-
vite.middlewares.use(async (req, res, next) => {
|
|
1424
|
-
try {
|
|
1425
|
-
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
1426
|
-
req.headers[':authority'] || req.headers.host
|
|
1427
|
-
}`;
|
|
1428
|
-
|
|
1429
|
-
const decoded = decodeURI(new URL$1(base + req.url).pathname);
|
|
1430
|
-
|
|
1431
|
-
if (decoded.startsWith(assets)) {
|
|
1432
|
-
const pathname = decoded.slice(assets.length);
|
|
1433
|
-
const file = svelte_config.kit.files.assets + pathname;
|
|
1434
|
-
|
|
1435
|
-
if (fs__default.existsSync(file) && !fs__default.statSync(file).isDirectory()) {
|
|
1436
|
-
if (has_correct_case(file, svelte_config.kit.files.assets)) {
|
|
1437
|
-
req.url = encodeURI(pathname); // don't need query/hash
|
|
1438
|
-
asset_server(req, res);
|
|
1439
|
-
return;
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
next();
|
|
1445
|
-
} catch (e) {
|
|
1446
|
-
const error = coalesce_to_error(e);
|
|
1447
|
-
res.statusCode = 500;
|
|
1448
|
-
res.end(fix_stack_trace(error));
|
|
1449
|
-
}
|
|
1450
|
-
});
|
|
1451
|
-
|
|
1452
|
-
return () => {
|
|
1453
|
-
const serve_static_middleware = vite.middlewares.stack.find(
|
|
1454
|
-
(middleware) =>
|
|
1455
|
-
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
|
|
1456
|
-
);
|
|
1457
|
-
|
|
1458
|
-
remove_static_middlewares(vite.middlewares);
|
|
1459
|
-
|
|
1460
|
-
vite.middlewares.use(async (req, res) => {
|
|
1461
|
-
try {
|
|
1462
|
-
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
1463
|
-
req.headers[':authority'] || req.headers.host
|
|
1464
|
-
}`;
|
|
1465
|
-
|
|
1466
|
-
const decoded = decodeURI(new URL$1(base + req.url).pathname);
|
|
1467
|
-
const file = posixify(path__default.resolve(decoded.slice(1)));
|
|
1468
|
-
const is_file = fs__default.existsSync(file) && !fs__default.statSync(file).isDirectory();
|
|
1469
|
-
const allowed =
|
|
1470
|
-
!vite_config.server.fs.strict ||
|
|
1471
|
-
vite_config.server.fs.allow.some((dir) => file.startsWith(dir));
|
|
1472
|
-
|
|
1473
|
-
if (is_file && allowed) {
|
|
1474
|
-
// @ts-expect-error
|
|
1475
|
-
serve_static_middleware.handle(req, res);
|
|
1476
|
-
return;
|
|
1477
|
-
}
|
|
1478
|
-
|
|
1479
|
-
if (!decoded.startsWith(svelte_config.kit.paths.base)) {
|
|
1480
|
-
return not_found(
|
|
1481
|
-
res,
|
|
1482
|
-
`Not found (did you mean ${svelte_config.kit.paths.base + req.url}?)`
|
|
1483
|
-
);
|
|
1484
|
-
}
|
|
1485
|
-
|
|
1486
|
-
const runtime_base = true
|
|
1487
|
-
? `/${posixify(path__default.relative(cwd$1, `${svelte_config.kit.outDir}/runtime`))}`
|
|
1488
|
-
: `/@fs${runtime}`;
|
|
1489
|
-
|
|
1490
|
-
const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
|
|
1491
|
-
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);
|
|
1492
|
-
|
|
1493
|
-
const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
|
|
1494
|
-
set_private_env(env.private);
|
|
1495
|
-
set_public_env(env.public);
|
|
1496
|
-
|
|
1497
|
-
/** @type {Partial<import('types').Hooks>} */
|
|
1498
|
-
const user_hooks = resolve_entry(svelte_config.kit.files.hooks)
|
|
1499
|
-
? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`)
|
|
1500
|
-
: {};
|
|
1501
|
-
|
|
1502
|
-
const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
|
|
1503
|
-
|
|
1504
|
-
/** @type {import('types').Hooks} */
|
|
1505
|
-
const hooks = {
|
|
1506
|
-
getSession: user_hooks.getSession || (() => ({})),
|
|
1507
|
-
handle,
|
|
1508
|
-
handleError:
|
|
1509
|
-
user_hooks.handleError ||
|
|
1510
|
-
(({ /** @type {Error & { frame?: string }} */ error }) => {
|
|
1511
|
-
console.error($.bold().red(error.message));
|
|
1512
|
-
if (error.frame) {
|
|
1513
|
-
console.error($.gray(error.frame));
|
|
1514
|
-
}
|
|
1515
|
-
if (error.stack) {
|
|
1516
|
-
console.error($.gray(error.stack));
|
|
1517
|
-
}
|
|
1518
|
-
}),
|
|
1519
|
-
externalFetch: user_hooks.externalFetch || fetch
|
|
1520
|
-
};
|
|
1521
|
-
|
|
1522
|
-
if (/** @type {any} */ (hooks).getContext) {
|
|
1523
|
-
// TODO remove this for 1.0
|
|
1524
|
-
throw new Error(
|
|
1525
|
-
'The getContext hook has been removed. See https://kit.svelte.dev/docs/hooks'
|
|
1526
|
-
);
|
|
1527
|
-
}
|
|
1528
|
-
|
|
1529
|
-
if (/** @type {any} */ (hooks).serverFetch) {
|
|
1530
|
-
// TODO remove this for 1.0
|
|
1531
|
-
throw new Error('The serverFetch hook has been renamed to externalFetch.');
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
// TODO the / prefix will probably fail if outDir is outside the cwd (which
|
|
1535
|
-
// could be the case in a monorepo setup), but without it these modules
|
|
1536
|
-
// can get loaded twice via different URLs, which causes failures. Might
|
|
1537
|
-
// require changes to Vite to fix
|
|
1538
|
-
const { default: root } = await vite.ssrLoadModule(
|
|
1539
|
-
`/${posixify(path__default.relative(cwd$1, `${svelte_config.kit.outDir}/generated/root.svelte`))}`
|
|
1540
|
-
);
|
|
1541
|
-
|
|
1542
|
-
const paths = await vite.ssrLoadModule(`${runtime_base}/paths.js`);
|
|
1543
|
-
|
|
1544
|
-
paths.set_paths({
|
|
1545
|
-
base: svelte_config.kit.paths.base,
|
|
1546
|
-
assets
|
|
1547
|
-
});
|
|
1548
|
-
|
|
1549
|
-
let request;
|
|
1550
|
-
|
|
1551
|
-
try {
|
|
1552
|
-
request = await getRequest(base, req);
|
|
1553
|
-
} catch (/** @type {any} */ err) {
|
|
1554
|
-
res.statusCode = err.status || 400;
|
|
1555
|
-
return res.end(err.reason || 'Invalid request body');
|
|
1556
|
-
}
|
|
1557
|
-
|
|
1558
|
-
const template = load_template(cwd$1, svelte_config);
|
|
1559
|
-
|
|
1560
|
-
const rendered = await respond(
|
|
1561
|
-
request,
|
|
1562
|
-
{
|
|
1563
|
-
csp: svelte_config.kit.csp,
|
|
1564
|
-
dev: true,
|
|
1565
|
-
get_stack: (error) => fix_stack_trace(error),
|
|
1566
|
-
handle_error: (error, event) => {
|
|
1567
|
-
hooks.handleError({
|
|
1568
|
-
error: new Proxy(error, {
|
|
1569
|
-
get: (target, property) => {
|
|
1570
|
-
if (property === 'stack') {
|
|
1571
|
-
return fix_stack_trace(error);
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
return Reflect.get(target, property, target);
|
|
1575
|
-
}
|
|
1576
|
-
}),
|
|
1577
|
-
event,
|
|
1578
|
-
|
|
1579
|
-
// TODO remove for 1.0
|
|
1580
|
-
// @ts-expect-error
|
|
1581
|
-
get request() {
|
|
1582
|
-
throw new Error(
|
|
1583
|
-
'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
|
|
1584
|
-
);
|
|
1585
|
-
}
|
|
1586
|
-
});
|
|
1587
|
-
},
|
|
1588
|
-
hooks,
|
|
1589
|
-
hydrate: svelte_config.kit.browser.hydrate,
|
|
1590
|
-
manifest,
|
|
1591
|
-
method_override: svelte_config.kit.methodOverride,
|
|
1592
|
-
paths: {
|
|
1593
|
-
base: svelte_config.kit.paths.base,
|
|
1594
|
-
assets
|
|
1595
|
-
},
|
|
1596
|
-
prefix: '',
|
|
1597
|
-
prerender: {
|
|
1598
|
-
default: svelte_config.kit.prerender.default,
|
|
1599
|
-
enabled: svelte_config.kit.prerender.enabled
|
|
1600
|
-
},
|
|
1601
|
-
public_env: env.public,
|
|
1602
|
-
read: (file) => fs__default.readFileSync(path__default.join(svelte_config.kit.files.assets, file)),
|
|
1603
|
-
root,
|
|
1604
|
-
router: svelte_config.kit.browser.router,
|
|
1605
|
-
template: ({ head, body, assets, nonce }) => {
|
|
1606
|
-
return (
|
|
1607
|
-
template
|
|
1608
|
-
.replace(/%sveltekit\.assets%/g, assets)
|
|
1609
|
-
.replace(/%sveltekit\.nonce%/g, nonce)
|
|
1610
|
-
// head and body must be replaced last, in case someone tries to sneak in %sveltekit.assets% etc
|
|
1611
|
-
.replace('%sveltekit.head%', () => head)
|
|
1612
|
-
.replace('%sveltekit.body%', () => body)
|
|
1613
|
-
);
|
|
1614
|
-
},
|
|
1615
|
-
template_contains_nonce: template.includes('%sveltekit.nonce%'),
|
|
1616
|
-
trailing_slash: svelte_config.kit.trailingSlash
|
|
1617
|
-
},
|
|
1618
|
-
{
|
|
1619
|
-
getClientAddress: () => {
|
|
1620
|
-
const { remoteAddress } = req.socket;
|
|
1621
|
-
if (remoteAddress) return remoteAddress;
|
|
1622
|
-
throw new Error('Could not determine clientAddress');
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
);
|
|
1626
|
-
|
|
1627
|
-
if (rendered.status === 404) {
|
|
1628
|
-
// @ts-expect-error
|
|
1629
|
-
serve_static_middleware.handle(req, res, () => {
|
|
1630
|
-
setResponse(res, rendered);
|
|
1631
|
-
});
|
|
1632
|
-
} else {
|
|
1633
|
-
setResponse(res, rendered);
|
|
1634
|
-
}
|
|
1635
|
-
} catch (e) {
|
|
1636
|
-
const error = coalesce_to_error(e);
|
|
1637
|
-
res.statusCode = 500;
|
|
1638
|
-
res.end(fix_stack_trace(error));
|
|
1639
|
-
}
|
|
1640
|
-
});
|
|
1641
|
-
};
|
|
1642
|
-
}
|
|
1643
|
-
|
|
1644
|
-
/** @param {import('http').ServerResponse} res */
|
|
1645
|
-
function not_found(res, message = 'Not found') {
|
|
1646
|
-
res.statusCode = 404;
|
|
1647
|
-
res.end(message);
|
|
1648
|
-
}
|
|
1649
|
-
|
|
1650
|
-
/**
|
|
1651
|
-
* @param {import('connect').Server} server
|
|
1652
|
-
*/
|
|
1653
|
-
function remove_static_middlewares(server) {
|
|
1654
|
-
// We don't use viteServePublicMiddleware because of the following issues:
|
|
1655
|
-
// https://github.com/vitejs/vite/issues/9260
|
|
1656
|
-
// https://github.com/vitejs/vite/issues/9236
|
|
1657
|
-
// https://github.com/vitejs/vite/issues/9234
|
|
1658
|
-
const static_middlewares = ['viteServePublicMiddleware', 'viteServeStaticMiddleware'];
|
|
1659
|
-
for (let i = server.stack.length - 1; i > 0; i--) {
|
|
1660
|
-
// @ts-expect-error using internals
|
|
1661
|
-
if (static_middlewares.includes(server.stack[i].handle.name)) {
|
|
1662
|
-
server.stack.splice(i, 1);
|
|
1663
|
-
}
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1666
|
-
|
|
1667
|
-
/**
|
|
1668
|
-
* @param {import('vite').ViteDevServer} vite
|
|
1669
|
-
* @param {import('vite').ModuleNode} node
|
|
1670
|
-
* @param {Set<import('vite').ModuleNode>} deps
|
|
1671
|
-
*/
|
|
1672
|
-
async function find_deps(vite, node, deps) {
|
|
1673
|
-
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
|
|
1674
|
-
// instead of using `await`, we resolve all branches in parallel.
|
|
1675
|
-
/** @type {Promise<void>[]} */
|
|
1676
|
-
const branches = [];
|
|
1677
|
-
|
|
1678
|
-
/** @param {import('vite').ModuleNode} node */
|
|
1679
|
-
async function add(node) {
|
|
1680
|
-
if (!deps.has(node)) {
|
|
1681
|
-
deps.add(node);
|
|
1682
|
-
await find_deps(vite, node, deps);
|
|
1683
|
-
}
|
|
1684
|
-
}
|
|
1685
|
-
|
|
1686
|
-
/** @param {string} url */
|
|
1687
|
-
async function add_by_url(url) {
|
|
1688
|
-
const node = await vite.moduleGraph.getModuleByUrl(url);
|
|
1689
|
-
|
|
1690
|
-
if (node) {
|
|
1691
|
-
await add(node);
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
|
|
1695
|
-
if (node.ssrTransformResult) {
|
|
1696
|
-
if (node.ssrTransformResult.deps) {
|
|
1697
|
-
node.ssrTransformResult.deps.forEach((url) => branches.push(add_by_url(url)));
|
|
1698
|
-
}
|
|
1699
|
-
|
|
1700
|
-
if (node.ssrTransformResult.dynamicDeps) {
|
|
1701
|
-
node.ssrTransformResult.dynamicDeps.forEach((url) => branches.push(add_by_url(url)));
|
|
1702
|
-
}
|
|
1703
|
-
} else {
|
|
1704
|
-
node.importedModules.forEach((node) => branches.push(add(node)));
|
|
1705
|
-
}
|
|
1706
|
-
|
|
1707
|
-
await Promise.all(branches);
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
/**
|
|
1711
|
-
* Determine if a file is being requested with the correct case,
|
|
1712
|
-
* to ensure consistent behaviour between dev and prod and across
|
|
1713
|
-
* operating systems. Note that we can't use realpath here,
|
|
1714
|
-
* because we don't want to follow symlinks
|
|
1715
|
-
* @param {string} file
|
|
1716
|
-
* @param {string} assets
|
|
1717
|
-
* @returns {boolean}
|
|
1718
|
-
*/
|
|
1719
|
-
function has_correct_case(file, assets) {
|
|
1720
|
-
if (file === assets) return true;
|
|
1721
|
-
|
|
1722
|
-
const parent = path__default.dirname(file);
|
|
1723
|
-
|
|
1724
|
-
if (fs__default.readdirSync(parent).includes(path__default.basename(file))) {
|
|
1725
|
-
return has_correct_case(parent, assets);
|
|
1726
|
-
}
|
|
1727
|
-
|
|
1728
|
-
return false;
|
|
1729
|
-
}
|
|
1730
|
-
|
|
1731
|
-
/**
|
|
1732
|
-
* Generates the data used to write the server-side manifest.js file. This data is used in the Vite
|
|
1733
|
-
* build process, to power routing, etc.
|
|
1734
|
-
* @param {{
|
|
1735
|
-
* build_data: import('types').BuildData;
|
|
1736
|
-
* relative_path: string;
|
|
1737
|
-
* routes: import('types').RouteData[];
|
|
1738
|
-
* format?: 'esm' | 'cjs'
|
|
1739
|
-
* }} opts
|
|
1740
|
-
*/
|
|
1741
|
-
function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
|
|
1742
|
-
/** @typedef {{ index: number, path: string }} LookupEntry */
|
|
1743
|
-
/** @type {Map<string, LookupEntry>} */
|
|
1744
|
-
const bundled_nodes = new Map();
|
|
1745
|
-
|
|
1746
|
-
// 0 and 1 are special, they correspond to the root layout and root error nodes
|
|
1747
|
-
bundled_nodes.set(build_data.manifest_data.components[0], {
|
|
1748
|
-
path: `${relative_path}/nodes/0.js`,
|
|
1749
|
-
index: 0
|
|
1750
|
-
});
|
|
1751
|
-
|
|
1752
|
-
bundled_nodes.set(build_data.manifest_data.components[1], {
|
|
1753
|
-
path: `${relative_path}/nodes/1.js`,
|
|
1754
|
-
index: 1
|
|
1755
|
-
});
|
|
1756
|
-
|
|
1757
|
-
routes.forEach((route) => {
|
|
1758
|
-
if (route.type === 'page') {
|
|
1759
|
-
[...route.a, ...route.b].forEach((component) => {
|
|
1760
|
-
if (component && !bundled_nodes.has(component)) {
|
|
1761
|
-
const i = build_data.manifest_data.components.indexOf(component);
|
|
1762
|
-
|
|
1763
|
-
bundled_nodes.set(component, {
|
|
1764
|
-
path: `${relative_path}/nodes/${i}.js`,
|
|
1765
|
-
index: bundled_nodes.size
|
|
1766
|
-
});
|
|
1767
|
-
}
|
|
1768
|
-
});
|
|
1769
|
-
}
|
|
1770
|
-
});
|
|
1771
|
-
|
|
1772
|
-
/** @type {(path: string) => string} */
|
|
1773
|
-
const load =
|
|
1774
|
-
format === 'esm'
|
|
1775
|
-
? (path) => `import('${path}')`
|
|
1776
|
-
: (path) => `Promise.resolve().then(() => require('${path}'))`;
|
|
1777
|
-
|
|
1778
|
-
/** @type {(path: string) => string} */
|
|
1779
|
-
const loader = (path) => `() => ${load(path)}`;
|
|
1780
|
-
|
|
1781
|
-
const assets = build_data.manifest_data.assets.map((asset) => asset.file);
|
|
1782
|
-
if (build_data.service_worker) {
|
|
1783
|
-
assets.push(build_data.service_worker);
|
|
1784
|
-
}
|
|
1785
|
-
|
|
1786
|
-
/** @param {string | undefined} id */
|
|
1787
|
-
const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
|
|
1788
|
-
|
|
1789
|
-
const matchers = new Set();
|
|
1790
|
-
|
|
1791
|
-
// prettier-ignore
|
|
1792
|
-
return `{
|
|
1793
|
-
appDir: ${s(build_data.app_dir)},
|
|
1794
|
-
assets: new Set(${s(assets)}),
|
|
1795
|
-
mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
|
|
1796
|
-
_: {
|
|
1797
|
-
entry: ${s(build_data.client.entry)},
|
|
1798
|
-
nodes: [
|
|
1799
|
-
${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
|
|
1800
|
-
],
|
|
1801
|
-
routes: [
|
|
1802
|
-
${routes.map(route => {
|
|
1803
|
-
const { pattern, names, types } = parse_route_id(route.id);
|
|
1804
|
-
|
|
1805
|
-
types.forEach(type => {
|
|
1806
|
-
if (type) matchers.add(type);
|
|
1807
|
-
});
|
|
1808
|
-
|
|
1809
|
-
if (route.type === 'page') {
|
|
1810
|
-
return `{
|
|
1811
|
-
type: 'page',
|
|
1812
|
-
id: ${s(route.id)},
|
|
1813
|
-
pattern: ${pattern},
|
|
1814
|
-
names: ${s(names)},
|
|
1815
|
-
types: ${s(types)},
|
|
1816
|
-
path: ${route.path ? s(route.path) : null},
|
|
1817
|
-
shadow: ${route.shadow ? loader(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null},
|
|
1818
|
-
a: ${s(route.a.map(get_index))},
|
|
1819
|
-
b: ${s(route.b.map(get_index))}
|
|
1820
|
-
}`.replace(/^\t\t/gm, '');
|
|
1821
|
-
} else {
|
|
1822
|
-
if (!build_data.server.vite_manifest[route.file]) {
|
|
1823
|
-
// this is necessary in cases where a .css file snuck in —
|
|
1824
|
-
// perhaps it would be better to disallow these (and others?)
|
|
1825
|
-
return null;
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
return `{
|
|
1829
|
-
type: 'endpoint',
|
|
1830
|
-
id: ${s(route.id)},
|
|
1831
|
-
pattern: ${pattern},
|
|
1832
|
-
names: ${s(names)},
|
|
1833
|
-
types: ${s(types)},
|
|
1834
|
-
load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
|
|
1835
|
-
}`.replace(/^\t\t/gm, '');
|
|
1836
|
-
}
|
|
1837
|
-
}).filter(Boolean).join(',\n\t\t\t\t')}
|
|
1838
|
-
],
|
|
1839
|
-
matchers: async () => {
|
|
1840
|
-
${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
|
|
1841
|
-
return { ${Array.from(matchers).join(', ')} };
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
}`.replace(/^\t/gm, '');
|
|
1845
|
-
}
|
|
1846
|
-
|
|
1847
|
-
/** @typedef {import('http').IncomingMessage} Req */
|
|
1848
|
-
/** @typedef {import('http').ServerResponse} Res */
|
|
1849
|
-
/** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
|
|
1850
|
-
|
|
1851
|
-
/**
|
|
1852
|
-
* @param {{
|
|
1853
|
-
* middlewares: import('connect').Server;
|
|
1854
|
-
* httpServer: import('http').Server;
|
|
1855
|
-
* }} vite
|
|
1856
|
-
* @param {import('vite').ResolvedConfig} vite_config
|
|
1857
|
-
* @param {import('types').ValidatedConfig} svelte_config
|
|
1858
|
-
*/
|
|
1859
|
-
async function preview(vite, vite_config, svelte_config) {
|
|
1860
|
-
installPolyfills();
|
|
1861
|
-
|
|
1862
|
-
const { paths } = svelte_config.kit;
|
|
1863
|
-
const base = paths.base;
|
|
1864
|
-
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
|
|
1865
|
-
|
|
1866
|
-
const protocol = vite_config.preview.https ? 'https' : 'http';
|
|
1867
|
-
|
|
1868
|
-
const etag = `"${Date.now()}"`;
|
|
1869
|
-
|
|
1870
|
-
const index_file = join(svelte_config.kit.outDir, 'output/server/index.js');
|
|
1871
|
-
const manifest_file = join(svelte_config.kit.outDir, 'output/server/manifest.js');
|
|
1872
|
-
|
|
1873
|
-
/** @type {import('types').ServerModule} */
|
|
1874
|
-
const { Server, override } = await import(pathToFileURL(index_file).href);
|
|
1875
|
-
const { manifest } = await import(pathToFileURL(manifest_file).href);
|
|
1876
|
-
|
|
1877
|
-
override({
|
|
1878
|
-
paths: { base, assets },
|
|
1879
|
-
prerendering: false,
|
|
1880
|
-
protocol,
|
|
1881
|
-
read: (file) => fs__default.readFileSync(join(svelte_config.kit.files.assets, file))
|
|
1882
|
-
});
|
|
1883
|
-
|
|
1884
|
-
const server = new Server(manifest);
|
|
1885
|
-
server.init({
|
|
1886
|
-
env: loadEnv(vite_config.mode, process.cwd(), '')
|
|
1887
|
-
});
|
|
1888
|
-
|
|
1889
|
-
return () => {
|
|
1890
|
-
// generated client assets and the contents of `static`
|
|
1891
|
-
vite.middlewares.use(
|
|
1892
|
-
scoped(
|
|
1893
|
-
assets,
|
|
1894
|
-
sirv(join(svelte_config.kit.outDir, 'output/client'), {
|
|
1895
|
-
setHeaders: (res, pathname) => {
|
|
1896
|
-
// only apply to immutable directory, not e.g. version.json
|
|
1897
|
-
if (pathname.startsWith(`/${svelte_config.kit.appDir}/immutable`)) {
|
|
1898
|
-
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
})
|
|
1902
|
-
)
|
|
1903
|
-
);
|
|
1904
|
-
|
|
1905
|
-
vite.middlewares.use((req, res, next) => {
|
|
1906
|
-
const original_url = /** @type {string} */ (req.url);
|
|
1907
|
-
const { pathname } = new URL(original_url, 'http://dummy');
|
|
1908
|
-
|
|
1909
|
-
if (pathname.startsWith(base)) {
|
|
1910
|
-
next();
|
|
1911
|
-
} else {
|
|
1912
|
-
res.statusCode = 404;
|
|
1913
|
-
res.end(`Not found (did you mean ${base + pathname}?)`);
|
|
1914
|
-
}
|
|
1915
|
-
});
|
|
1916
|
-
|
|
1917
|
-
// prerendered dependencies
|
|
1918
|
-
vite.middlewares.use(
|
|
1919
|
-
scoped(base, mutable(join(svelte_config.kit.outDir, 'output/prerendered/dependencies')))
|
|
1920
|
-
);
|
|
1921
|
-
|
|
1922
|
-
// prerendered pages (we can't just use sirv because we need to
|
|
1923
|
-
// preserve the correct trailingSlash behaviour)
|
|
1924
|
-
vite.middlewares.use(
|
|
1925
|
-
scoped(base, (req, res, next) => {
|
|
1926
|
-
let if_none_match_value = req.headers['if-none-match'];
|
|
1927
|
-
|
|
1928
|
-
if (if_none_match_value?.startsWith('W/"')) {
|
|
1929
|
-
if_none_match_value = if_none_match_value.substring(2);
|
|
1930
|
-
}
|
|
1931
|
-
|
|
1932
|
-
if (if_none_match_value === etag) {
|
|
1933
|
-
res.statusCode = 304;
|
|
1934
|
-
res.end();
|
|
1935
|
-
return;
|
|
1936
|
-
}
|
|
1937
|
-
|
|
1938
|
-
const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
|
|
1939
|
-
|
|
1940
|
-
// only treat this as a page if it doesn't include an extension
|
|
1941
|
-
if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
|
|
1942
|
-
const file = join(
|
|
1943
|
-
svelte_config.kit.outDir,
|
|
1944
|
-
'output/prerendered/pages' +
|
|
1945
|
-
pathname +
|
|
1946
|
-
(pathname.endsWith('/') ? 'index.html' : '.html')
|
|
1947
|
-
);
|
|
1948
|
-
|
|
1949
|
-
if (fs__default.existsSync(file)) {
|
|
1950
|
-
res.writeHead(200, {
|
|
1951
|
-
'content-type': 'text/html',
|
|
1952
|
-
etag
|
|
1953
|
-
});
|
|
1954
|
-
|
|
1955
|
-
fs__default.createReadStream(file).pipe(res);
|
|
1956
|
-
return;
|
|
1957
|
-
}
|
|
1958
|
-
}
|
|
1959
|
-
|
|
1960
|
-
next();
|
|
1961
|
-
})
|
|
1962
|
-
);
|
|
1963
|
-
|
|
1964
|
-
// SSR
|
|
1965
|
-
vite.middlewares.use(async (req, res) => {
|
|
1966
|
-
const host = req.headers['host'];
|
|
1967
|
-
|
|
1968
|
-
let request;
|
|
1969
|
-
|
|
1970
|
-
try {
|
|
1971
|
-
request = await getRequest(`${protocol}://${host}`, req);
|
|
1972
|
-
} catch (/** @type {any} */ err) {
|
|
1973
|
-
res.statusCode = err.status || 400;
|
|
1974
|
-
return res.end(err.reason || 'Invalid request body');
|
|
1975
|
-
}
|
|
1976
|
-
|
|
1977
|
-
setResponse(
|
|
1978
|
-
res,
|
|
1979
|
-
await server.respond(request, {
|
|
1980
|
-
getClientAddress: () => {
|
|
1981
|
-
const { remoteAddress } = req.socket;
|
|
1982
|
-
if (remoteAddress) return remoteAddress;
|
|
1983
|
-
throw new Error('Could not determine clientAddress');
|
|
1984
|
-
}
|
|
1985
|
-
})
|
|
1986
|
-
);
|
|
1987
|
-
});
|
|
1988
|
-
};
|
|
1989
|
-
}
|
|
1990
|
-
|
|
1991
|
-
/**
|
|
1992
|
-
* @param {string} dir
|
|
1993
|
-
* @returns {Handler}
|
|
1994
|
-
*/
|
|
1995
|
-
const mutable = (dir) =>
|
|
1996
|
-
fs__default.existsSync(dir)
|
|
1997
|
-
? sirv(dir, {
|
|
1998
|
-
etag: true,
|
|
1999
|
-
maxAge: 0
|
|
2000
|
-
})
|
|
2001
|
-
: (_req, _res, next) => next();
|
|
2002
|
-
|
|
2003
|
-
/**
|
|
2004
|
-
* @param {string} scope
|
|
2005
|
-
* @param {Handler} handler
|
|
2006
|
-
* @returns {Handler}
|
|
2007
|
-
*/
|
|
2008
|
-
function scoped(scope, handler) {
|
|
2009
|
-
if (scope === '') return handler;
|
|
2010
|
-
|
|
2011
|
-
return (req, res, next) => {
|
|
2012
|
-
if (req.url?.startsWith(scope)) {
|
|
2013
|
-
const original_url = req.url;
|
|
2014
|
-
req.url = req.url.slice(scope.length);
|
|
2015
|
-
handler(req, res, () => {
|
|
2016
|
-
req.url = original_url;
|
|
2017
|
-
next();
|
|
2018
|
-
});
|
|
2019
|
-
} else {
|
|
2020
|
-
next();
|
|
2021
|
-
}
|
|
2022
|
-
};
|
|
2023
|
-
}
|
|
2024
|
-
|
|
2025
|
-
const cwd = process.cwd();
|
|
2026
|
-
|
|
2027
|
-
/** @type {import('./types').EnforcedConfig} */
|
|
2028
|
-
const enforced_config = {
|
|
2029
|
-
appType: true,
|
|
2030
|
-
base: true,
|
|
2031
|
-
build: {
|
|
2032
|
-
cssCodeSplit: true,
|
|
2033
|
-
emptyOutDir: true,
|
|
2034
|
-
lib: {
|
|
2035
|
-
entry: true,
|
|
2036
|
-
name: true,
|
|
2037
|
-
formats: true
|
|
2038
|
-
},
|
|
2039
|
-
manifest: true,
|
|
2040
|
-
outDir: true,
|
|
2041
|
-
polyfillModulePreload: true,
|
|
2042
|
-
rollupOptions: {
|
|
2043
|
-
input: true,
|
|
2044
|
-
output: {
|
|
2045
|
-
format: true,
|
|
2046
|
-
entryFileNames: true,
|
|
2047
|
-
chunkFileNames: true,
|
|
2048
|
-
assetFileNames: true
|
|
2049
|
-
},
|
|
2050
|
-
preserveEntrySignatures: true
|
|
2051
|
-
},
|
|
2052
|
-
ssr: true
|
|
2053
|
-
},
|
|
2054
|
-
publicDir: true,
|
|
2055
|
-
resolve: {
|
|
2056
|
-
alias: {
|
|
2057
|
-
$app: true,
|
|
2058
|
-
$lib: true,
|
|
2059
|
-
'$service-worker': true
|
|
2060
|
-
}
|
|
2061
|
-
},
|
|
2062
|
-
root: true
|
|
2063
|
-
};
|
|
2064
|
-
|
|
2065
|
-
/**
|
|
2066
|
-
* @return {import('vite').Plugin[]}
|
|
2067
|
-
*/
|
|
2068
|
-
function sveltekit() {
|
|
2069
|
-
return [...svelte(), kit()];
|
|
2070
|
-
}
|
|
2071
|
-
|
|
2072
|
-
/**
|
|
2073
|
-
* Returns the SvelteKit Vite plugin. Vite executes Rollup hooks as well as some of its own.
|
|
2074
|
-
* Background reading is available at:
|
|
2075
|
-
* - https://vitejs.dev/guide/api-plugin.html
|
|
2076
|
-
* - https://rollupjs.org/guide/en/#plugin-development
|
|
2077
|
-
*
|
|
2078
|
-
* You can get an idea of the lifecycle by looking at the flow charts here:
|
|
2079
|
-
* - https://rollupjs.org/guide/en/#build-hooks
|
|
2080
|
-
* - https://rollupjs.org/guide/en/#output-generation-hooks
|
|
2081
|
-
*
|
|
2082
|
-
* @return {import('vite').Plugin}
|
|
2083
|
-
*/
|
|
2084
|
-
function kit() {
|
|
2085
|
-
/** @type {import('types').ValidatedConfig} */
|
|
2086
|
-
let svelte_config;
|
|
2087
|
-
|
|
2088
|
-
/** @type {import('vite').ResolvedConfig} */
|
|
2089
|
-
let vite_config;
|
|
2090
|
-
|
|
2091
|
-
/** @type {import('vite').ConfigEnv} */
|
|
2092
|
-
let vite_config_env;
|
|
2093
|
-
|
|
2094
|
-
/** @type {import('types').ManifestData} */
|
|
2095
|
-
let manifest_data;
|
|
2096
|
-
|
|
2097
|
-
/** @type {boolean} */
|
|
2098
|
-
let is_build;
|
|
2099
|
-
|
|
2100
|
-
/** @type {import('types').Logger} */
|
|
2101
|
-
let log;
|
|
2102
|
-
|
|
2103
|
-
/** @type {import('types').Prerendered} */
|
|
2104
|
-
let prerendered;
|
|
2105
|
-
|
|
2106
|
-
/** @type {import('types').BuildData} */
|
|
2107
|
-
let build_data;
|
|
2108
|
-
|
|
2109
|
-
/** @type {Set<string>} */
|
|
2110
|
-
let illegal_imports;
|
|
2111
|
-
|
|
2112
|
-
/** @type {string | undefined} */
|
|
2113
|
-
let deferred_warning;
|
|
2114
|
-
|
|
2115
|
-
/**
|
|
2116
|
-
* @type {{
|
|
2117
|
-
* build_dir: string;
|
|
2118
|
-
* output_dir: string;
|
|
2119
|
-
* client_out_dir: string;
|
|
2120
|
-
* }}
|
|
2121
|
-
*/
|
|
2122
|
-
let paths;
|
|
2123
|
-
|
|
2124
|
-
let completed_build = false;
|
|
2125
|
-
|
|
2126
|
-
function vite_client_build_config() {
|
|
2127
|
-
/** @type {Record<string, string>} */
|
|
2128
|
-
const input = {
|
|
2129
|
-
// Put unchanging assets in immutable directory. We don't set that in the
|
|
2130
|
-
// outDir so that other plugins can add mutable assets to the bundle
|
|
2131
|
-
start: `${get_runtime_directory(svelte_config.kit)}/client/start.js`
|
|
2132
|
-
};
|
|
2133
|
-
|
|
2134
|
-
// This step is optional — Vite/Rollup will create the necessary chunks
|
|
2135
|
-
// for everything regardless — but it means that entry chunks reflect
|
|
2136
|
-
// their location in the source code, which is helpful for debugging
|
|
2137
|
-
manifest_data.components.forEach((file) => {
|
|
2138
|
-
const resolved = path.resolve(cwd, file);
|
|
2139
|
-
const relative = decodeURIComponent(path.relative(svelte_config.kit.files.routes, resolved));
|
|
2140
|
-
|
|
2141
|
-
const name = relative.startsWith('..')
|
|
2142
|
-
? path.basename(file)
|
|
2143
|
-
: posixify(path.join('pages', relative));
|
|
2144
|
-
input[name] = resolved;
|
|
2145
|
-
});
|
|
2146
|
-
|
|
2147
|
-
return get_default_config({
|
|
2148
|
-
config: svelte_config,
|
|
2149
|
-
input,
|
|
2150
|
-
ssr: false,
|
|
2151
|
-
outDir: `${paths.client_out_dir}`
|
|
2152
|
-
});
|
|
2153
|
-
}
|
|
2154
|
-
|
|
2155
|
-
/**
|
|
2156
|
-
* @param {import('rollup').OutputAsset[]} assets
|
|
2157
|
-
* @param {import('rollup').OutputChunk[]} chunks
|
|
2158
|
-
*/
|
|
2159
|
-
function client_build_info(assets, chunks) {
|
|
2160
|
-
/** @type {import('vite').Manifest} */
|
|
2161
|
-
const vite_manifest = JSON.parse(
|
|
2162
|
-
fs$1.readFileSync(`${paths.client_out_dir}/manifest.json`, 'utf-8')
|
|
2163
|
-
);
|
|
2164
|
-
|
|
2165
|
-
const entry_id = posixify(
|
|
2166
|
-
path.relative(cwd, `${get_runtime_directory(svelte_config.kit)}/client/start.js`)
|
|
2167
|
-
);
|
|
2168
|
-
|
|
2169
|
-
return {
|
|
2170
|
-
assets,
|
|
2171
|
-
chunks,
|
|
2172
|
-
entry: find_deps$1(vite_manifest, entry_id, false),
|
|
2173
|
-
vite_manifest
|
|
2174
|
-
};
|
|
2175
|
-
}
|
|
2176
|
-
|
|
2177
|
-
// TODO remove this for 1.0
|
|
2178
|
-
check_vite_version();
|
|
2179
|
-
|
|
2180
|
-
return {
|
|
2181
|
-
name: 'vite-plugin-svelte-kit',
|
|
2182
|
-
|
|
2183
|
-
/**
|
|
2184
|
-
* Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
|
|
2185
|
-
* @see https://vitejs.dev/guide/api-plugin.html#config
|
|
2186
|
-
*/
|
|
2187
|
-
async config(config, config_env) {
|
|
2188
|
-
vite_config_env = config_env;
|
|
2189
|
-
svelte_config = await load_config();
|
|
2190
|
-
is_build = config_env.command === 'build';
|
|
2191
|
-
|
|
2192
|
-
paths = {
|
|
2193
|
-
build_dir: `${svelte_config.kit.outDir}/build`,
|
|
2194
|
-
output_dir: `${svelte_config.kit.outDir}/output`,
|
|
2195
|
-
client_out_dir: `${svelte_config.kit.outDir}/output/client/`
|
|
2196
|
-
};
|
|
2197
|
-
|
|
2198
|
-
illegal_imports = new Set([
|
|
2199
|
-
vite.normalizePath(`${svelte_config.kit.outDir}/runtime/env/dynamic/private.js`),
|
|
2200
|
-
vite.normalizePath(`${svelte_config.kit.outDir}/runtime/env/static/private.js`)
|
|
2201
|
-
]);
|
|
2202
|
-
|
|
2203
|
-
if (is_build) {
|
|
2204
|
-
manifest_data = all(svelte_config, config_env.mode).manifest_data;
|
|
2205
|
-
|
|
2206
|
-
const new_config = vite_client_build_config();
|
|
2207
|
-
|
|
2208
|
-
const warning = warn_overridden_config(config, new_config);
|
|
2209
|
-
if (warning) console.error(warning + '\n');
|
|
2210
|
-
|
|
2211
|
-
return new_config;
|
|
2212
|
-
}
|
|
2213
|
-
|
|
2214
|
-
// dev and preview config can be shared
|
|
2215
|
-
/** @type {import('vite').UserConfig} */
|
|
2216
|
-
const result = {
|
|
2217
|
-
appType: 'custom',
|
|
2218
|
-
base: '/',
|
|
2219
|
-
build: {
|
|
2220
|
-
rollupOptions: {
|
|
2221
|
-
// Vite dependency crawler needs an explicit JS entry point
|
|
2222
|
-
// eventhough server otherwise works without it
|
|
2223
|
-
input: `${get_runtime_directory(svelte_config.kit)}/client/start.js`
|
|
2224
|
-
}
|
|
2225
|
-
},
|
|
2226
|
-
define: {
|
|
2227
|
-
__SVELTEKIT_DEV__: 'true',
|
|
2228
|
-
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0'
|
|
2229
|
-
},
|
|
2230
|
-
publicDir: svelte_config.kit.files.assets,
|
|
2231
|
-
resolve: {
|
|
2232
|
-
alias: get_aliases(svelte_config.kit)
|
|
2233
|
-
},
|
|
2234
|
-
root: cwd,
|
|
2235
|
-
server: {
|
|
2236
|
-
fs: {
|
|
2237
|
-
allow: [
|
|
2238
|
-
...new Set([
|
|
2239
|
-
svelte_config.kit.files.lib,
|
|
2240
|
-
svelte_config.kit.files.routes,
|
|
2241
|
-
svelte_config.kit.outDir,
|
|
2242
|
-
path.resolve(cwd, 'src'),
|
|
2243
|
-
path.resolve(cwd, 'node_modules'),
|
|
2244
|
-
path.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
|
|
2245
|
-
])
|
|
2246
|
-
]
|
|
2247
|
-
},
|
|
2248
|
-
watch: {
|
|
2249
|
-
ignored: [
|
|
2250
|
-
// Ignore all siblings of config.kit.outDir/generated
|
|
2251
|
-
`${posixify(svelte_config.kit.outDir)}/!(generated)`
|
|
2252
|
-
]
|
|
2253
|
-
}
|
|
2254
|
-
}
|
|
2255
|
-
};
|
|
2256
|
-
|
|
2257
|
-
deferred_warning = warn_overridden_config(config, result);
|
|
2258
|
-
return result;
|
|
2259
|
-
},
|
|
2260
|
-
|
|
2261
|
-
/**
|
|
2262
|
-
* Stores the final config.
|
|
2263
|
-
*/
|
|
2264
|
-
configResolved(config) {
|
|
2265
|
-
vite_config = config;
|
|
2266
|
-
},
|
|
2267
|
-
|
|
2268
|
-
/**
|
|
2269
|
-
* Clears the output directories.
|
|
2270
|
-
*/
|
|
2271
|
-
buildStart() {
|
|
2272
|
-
// Reset for new build. Goes here because `build --watch` calls buildStart but not config
|
|
2273
|
-
completed_build = false;
|
|
2274
|
-
|
|
2275
|
-
if (is_build) {
|
|
2276
|
-
rimraf(paths.build_dir);
|
|
2277
|
-
mkdirp(paths.build_dir);
|
|
2278
|
-
|
|
2279
|
-
rimraf(paths.output_dir);
|
|
2280
|
-
mkdirp(paths.output_dir);
|
|
2281
|
-
}
|
|
2282
|
-
},
|
|
2283
|
-
|
|
2284
|
-
/**
|
|
2285
|
-
* Vite builds a single bundle. We need three bundles: client, server, and service worker.
|
|
2286
|
-
* The user's package.json scripts will invoke the Vite CLI to execute the client build. We
|
|
2287
|
-
* then use this hook to kick off builds for the server and service worker.
|
|
2288
|
-
*/
|
|
2289
|
-
async writeBundle(_options, bundle) {
|
|
2290
|
-
for (const file of manifest_data.components) {
|
|
2291
|
-
const id = vite.normalizePath(path.resolve(file));
|
|
2292
|
-
const node = this.getModuleInfo(id);
|
|
2293
|
-
|
|
2294
|
-
if (node) {
|
|
2295
|
-
prevent_illegal_rollup_imports(
|
|
2296
|
-
this.getModuleInfo.bind(this),
|
|
2297
|
-
node,
|
|
2298
|
-
illegal_imports,
|
|
2299
|
-
svelte_config.kit.outDir
|
|
2300
|
-
);
|
|
2301
|
-
}
|
|
2302
|
-
}
|
|
2303
|
-
|
|
2304
|
-
const verbose = vite_config.logLevel === 'info';
|
|
2305
|
-
log = logger({
|
|
2306
|
-
verbose
|
|
2307
|
-
});
|
|
2308
|
-
|
|
2309
|
-
fs$1.writeFileSync(
|
|
2310
|
-
`${paths.client_out_dir}/${svelte_config.kit.appDir}/version.json`,
|
|
2311
|
-
JSON.stringify({ version: svelte_config.kit.version.name })
|
|
2312
|
-
);
|
|
2313
|
-
|
|
2314
|
-
const { assets, chunks } = collect_output(bundle);
|
|
2315
|
-
log.info(`Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`);
|
|
2316
|
-
|
|
2317
|
-
log.info('Building server');
|
|
2318
|
-
const options = {
|
|
2319
|
-
cwd,
|
|
2320
|
-
config: svelte_config,
|
|
2321
|
-
vite_config,
|
|
2322
|
-
vite_config_env,
|
|
2323
|
-
build_dir: paths.build_dir, // TODO just pass `paths`
|
|
2324
|
-
manifest_data,
|
|
2325
|
-
output_dir: paths.output_dir,
|
|
2326
|
-
service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
|
|
2327
|
-
};
|
|
2328
|
-
const client = client_build_info(assets, chunks);
|
|
2329
|
-
const server = await build_server(options, client);
|
|
2330
|
-
|
|
2331
|
-
/** @type {import('types').BuildData} */
|
|
2332
|
-
build_data = {
|
|
2333
|
-
app_dir: svelte_config.kit.appDir,
|
|
2334
|
-
manifest_data,
|
|
2335
|
-
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
|
|
2336
|
-
client,
|
|
2337
|
-
server
|
|
2338
|
-
};
|
|
2339
|
-
|
|
2340
|
-
const manifest_path = `${paths.output_dir}/server/manifest.js`;
|
|
2341
|
-
fs$1.writeFileSync(
|
|
2342
|
-
manifest_path,
|
|
2343
|
-
`export const manifest = ${generate_manifest({
|
|
2344
|
-
build_data,
|
|
2345
|
-
relative_path: '.',
|
|
2346
|
-
routes: manifest_data.routes
|
|
2347
|
-
})};\n`
|
|
2348
|
-
);
|
|
2349
|
-
|
|
2350
|
-
log.info('Prerendering');
|
|
2351
|
-
await new Promise((fulfil, reject) => {
|
|
2352
|
-
const results_path = `${svelte_config.kit.outDir}/generated/prerendered.json`;
|
|
2353
|
-
|
|
2354
|
-
// do prerendering in a subprocess so any dangling stuff gets killed upon completion
|
|
2355
|
-
const script = fileURLToPath(
|
|
2356
|
-
new URL(
|
|
2357
|
-
'./prerender.js' ,
|
|
2358
|
-
import.meta.url
|
|
2359
|
-
)
|
|
2360
|
-
);
|
|
2361
|
-
|
|
2362
|
-
const child = fork(
|
|
2363
|
-
script,
|
|
2364
|
-
[vite_config.build.outDir, results_path, manifest_path, '' + verbose],
|
|
2365
|
-
{
|
|
2366
|
-
stdio: 'inherit'
|
|
2367
|
-
}
|
|
2368
|
-
);
|
|
2369
|
-
|
|
2370
|
-
child.on('exit', (code) => {
|
|
2371
|
-
if (code) {
|
|
2372
|
-
reject(new Error(`Prerendering failed with code ${code}`));
|
|
2373
|
-
} else {
|
|
2374
|
-
prerendered = JSON.parse(fs$1.readFileSync(results_path, 'utf8'), (key, value) => {
|
|
2375
|
-
if (key === 'pages' || key === 'assets' || key === 'redirects') {
|
|
2376
|
-
return new Map(value);
|
|
2377
|
-
}
|
|
2378
|
-
return value;
|
|
2379
|
-
});
|
|
2380
|
-
fulfil(undefined);
|
|
2381
|
-
}
|
|
2382
|
-
});
|
|
2383
|
-
});
|
|
2384
|
-
|
|
2385
|
-
if (options.service_worker_entry_file) {
|
|
2386
|
-
if (svelte_config.kit.paths.assets) {
|
|
2387
|
-
throw new Error('Cannot use service worker alongside config.kit.paths.assets');
|
|
2388
|
-
}
|
|
2389
|
-
|
|
2390
|
-
log.info('Building service worker');
|
|
2391
|
-
|
|
2392
|
-
await build_service_worker(options, prerendered, client.vite_manifest);
|
|
2393
|
-
}
|
|
2394
|
-
|
|
2395
|
-
console.log(
|
|
2396
|
-
`\nRun ${$.bold().cyan('npm run preview')} to preview your production build locally.`
|
|
2397
|
-
);
|
|
2398
|
-
|
|
2399
|
-
completed_build = true;
|
|
2400
|
-
},
|
|
2401
|
-
|
|
2402
|
-
/**
|
|
2403
|
-
* Runs the adapter.
|
|
2404
|
-
*/
|
|
2405
|
-
async closeBundle() {
|
|
2406
|
-
if (!completed_build) {
|
|
2407
|
-
// vite calls closeBundle when dev-server restarts, ignore that,
|
|
2408
|
-
// and only adapt when build successfully completes.
|
|
2409
|
-
return;
|
|
2410
|
-
}
|
|
2411
|
-
|
|
2412
|
-
if (svelte_config.kit.adapter) {
|
|
2413
|
-
const { adapt } = await import('./chunks/index3.js');
|
|
2414
|
-
await adapt(svelte_config, build_data, prerendered, { log });
|
|
2415
|
-
} else {
|
|
2416
|
-
console.log($.bold().yellow('\nNo adapter specified'));
|
|
2417
|
-
// prettier-ignore
|
|
2418
|
-
console.log(
|
|
2419
|
-
`See ${$.bold().cyan('https://kit.svelte.dev/docs/adapters')} to learn how to configure your app to run on the platform of your choosing`
|
|
2420
|
-
);
|
|
2421
|
-
}
|
|
2422
|
-
},
|
|
2423
|
-
|
|
2424
|
-
/**
|
|
2425
|
-
* Adds the SvelteKit middleware to do SSR in dev mode.
|
|
2426
|
-
* @see https://vitejs.dev/guide/api-plugin.html#configureserver
|
|
2427
|
-
*/
|
|
2428
|
-
async configureServer(vite) {
|
|
2429
|
-
// This method is called by Vite after clearing the screen.
|
|
2430
|
-
// This patch ensures we can log any important messages afterwards for the user to see.
|
|
2431
|
-
const print_urls = vite.printUrls;
|
|
2432
|
-
vite.printUrls = function () {
|
|
2433
|
-
print_urls.apply(this);
|
|
2434
|
-
if (deferred_warning) console.error('\n' + deferred_warning);
|
|
2435
|
-
};
|
|
2436
|
-
|
|
2437
|
-
return await dev(vite, vite_config, svelte_config, illegal_imports);
|
|
2438
|
-
},
|
|
2439
|
-
|
|
2440
|
-
/**
|
|
2441
|
-
* Adds the SvelteKit middleware to do SSR in preview mode.
|
|
2442
|
-
* @see https://vitejs.dev/guide/api-plugin.html#configurepreviewserver
|
|
2443
|
-
*/
|
|
2444
|
-
configurePreviewServer(vite) {
|
|
2445
|
-
return preview(vite, vite_config, svelte_config);
|
|
2446
|
-
}
|
|
2447
|
-
};
|
|
2448
|
-
}
|
|
2449
|
-
|
|
2450
|
-
function check_vite_version() {
|
|
2451
|
-
// TODO parse from kit peer deps and maybe do a full semver compare if we ever require feature releases a min
|
|
2452
|
-
const min_required_vite_major = 3;
|
|
2453
|
-
const vite_version = vite.version ?? '2.x'; // vite started exporting it's version in 3.0
|
|
2454
|
-
const current_vite_major = parseInt(vite_version.split('.')[0], 10);
|
|
2455
|
-
|
|
2456
|
-
if (current_vite_major < min_required_vite_major) {
|
|
2457
|
-
throw new Error(
|
|
2458
|
-
`Vite version ${current_vite_major} is no longer supported. Please upgrade to version ${min_required_vite_major}`
|
|
2459
|
-
);
|
|
2460
|
-
}
|
|
2461
|
-
}
|
|
2462
|
-
|
|
2463
|
-
/** @param {import('rollup').OutputBundle} bundle */
|
|
2464
|
-
function collect_output(bundle) {
|
|
2465
|
-
/** @type {import('rollup').OutputChunk[]} */
|
|
2466
|
-
const chunks = [];
|
|
2467
|
-
/** @type {import('rollup').OutputAsset[]} */
|
|
2468
|
-
const assets = [];
|
|
2469
|
-
for (const value of Object.values(bundle)) {
|
|
2470
|
-
// collect asset and output chunks
|
|
2471
|
-
if (value.type === 'asset') {
|
|
2472
|
-
assets.push(value);
|
|
2473
|
-
} else {
|
|
2474
|
-
chunks.push(value);
|
|
2475
|
-
}
|
|
2476
|
-
}
|
|
2477
|
-
return { assets, chunks };
|
|
2478
|
-
}
|
|
2479
|
-
|
|
2480
|
-
/**
|
|
2481
|
-
* @param {Record<string, any>} config
|
|
2482
|
-
* @param {Record<string, any>} resolved_config
|
|
2483
|
-
*/
|
|
2484
|
-
function warn_overridden_config(config, resolved_config) {
|
|
2485
|
-
const overridden = find_overridden_config(config, resolved_config, enforced_config, '', []);
|
|
2486
|
-
|
|
2487
|
-
if (overridden.length > 0) {
|
|
2488
|
-
return (
|
|
2489
|
-
$.bold().red('The following Vite config options will be overridden by SvelteKit:') +
|
|
2490
|
-
overridden.map((key) => `\n - ${key}`).join('')
|
|
2491
|
-
);
|
|
2492
|
-
}
|
|
2493
|
-
}
|
|
2494
|
-
|
|
2495
|
-
/**
|
|
2496
|
-
* @param {Record<string, any>} config
|
|
2497
|
-
* @param {Record<string, any>} resolved_config
|
|
2498
|
-
* @param {import('./types').EnforcedConfig} enforced_config
|
|
2499
|
-
* @param {string} path
|
|
2500
|
-
* @param {string[]} out used locally to compute the return value
|
|
2501
|
-
*/
|
|
2502
|
-
function find_overridden_config(config, resolved_config, enforced_config, path, out) {
|
|
2503
|
-
for (const key in enforced_config) {
|
|
2504
|
-
if (typeof config === 'object' && config !== null && key in config) {
|
|
2505
|
-
const enforced = enforced_config[key];
|
|
2506
|
-
|
|
2507
|
-
if (enforced === true) {
|
|
2508
|
-
if (config[key] !== resolved_config[key]) {
|
|
2509
|
-
out.push(path + key);
|
|
2510
|
-
}
|
|
2511
|
-
} else {
|
|
2512
|
-
find_overridden_config(config[key], resolved_config[key], enforced, path + key + '.', out);
|
|
2513
|
-
}
|
|
2514
|
-
}
|
|
2515
|
-
}
|
|
2516
|
-
|
|
2517
|
-
return out;
|
|
2518
|
-
}
|
|
2519
|
-
|
|
2520
|
-
export { generate_manifest as g, sveltekit };
|