@sveltejs/kit 1.0.0-next.21 → 1.0.0-next.210
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/README.md +12 -9
- package/assets/components/error.svelte +19 -3
- package/assets/kit.js +1912 -0
- package/assets/runtime/app/env.js +20 -0
- package/assets/runtime/app/navigation.js +45 -13
- package/assets/runtime/app/paths.js +1 -2
- package/assets/runtime/app/stores.js +15 -9
- package/assets/runtime/chunks/utils.js +13 -0
- package/assets/runtime/env.js +8 -0
- package/assets/runtime/internal/singletons.js +5 -11
- package/assets/runtime/internal/start.js +979 -355
- package/assets/runtime/paths.js +13 -0
- package/dist/chunks/cert.js +29255 -0
- package/dist/chunks/constants.js +8 -0
- package/dist/chunks/error.js +21 -0
- package/dist/chunks/index.js +4732 -0
- package/dist/chunks/index2.js +812 -0
- package/dist/chunks/index3.js +624 -0
- package/dist/chunks/index4.js +109 -0
- package/dist/chunks/index5.js +628 -0
- package/dist/chunks/index6.js +827 -0
- package/dist/chunks/index7.js +15575 -0
- package/dist/chunks/misc.js +3 -0
- package/dist/chunks/multipart-parser.js +449 -0
- package/dist/chunks/url.js +62 -0
- package/dist/cli.js +1012 -67
- package/dist/hooks.js +28 -0
- package/dist/install-fetch.js +6514 -0
- package/dist/node.js +51 -0
- package/dist/ssr.js +1842 -0
- package/package.json +90 -54
- package/svelte-kit.js +2 -0
- package/types/ambient-modules.d.ts +181 -0
- package/types/app.d.ts +45 -0
- package/types/config.d.ts +163 -0
- package/types/endpoint.d.ts +18 -0
- package/types/helper.d.ts +41 -0
- package/types/hooks.d.ts +48 -0
- package/types/index.d.ts +17 -0
- package/types/internal.d.ts +231 -0
- package/types/page.d.ts +71 -0
- package/CHANGELOG.md +0 -282
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/paths.js.map +0 -1
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js.map +0 -1
- package/assets/runtime/internal/start.js.map +0 -1
- package/assets/runtime/utils-85ebcc60.js +0 -18
- package/assets/runtime/utils-85ebcc60.js.map +0 -1
- package/dist/api.js +0 -44
- package/dist/api.js.map +0 -1
- package/dist/build.js +0 -246
- package/dist/build.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/colors.js +0 -37
- package/dist/colors.js.map +0 -1
- package/dist/create_app.js +0 -578
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -12042
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -544
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -71
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -466
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -729
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -713
- package/dist/index6.js.map +0 -1
- package/dist/logging.js +0 -43
- package/dist/logging.js.map +0 -1
- package/dist/package.js +0 -432
- package/dist/package.js.map +0 -1
- package/dist/renderer.js +0 -2391
- package/dist/renderer.js.map +0 -1
- package/dist/standard.js +0 -101
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -54
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import fs__default, { writeFileSync } from 'fs';
|
|
2
|
+
import path__default from 'path';
|
|
3
|
+
import { p as print_config_conflicts, c as copy_assets, b as posixify, a as resolve_entry, r as rimraf, m as mkdirp } from '../cli.js';
|
|
4
|
+
import { d as deep_merge, a as create_app, c as create_manifest_data } from './index2.js';
|
|
5
|
+
import { S as SVELTE_KIT } from './constants.js';
|
|
6
|
+
import { g as generate_manifest } from './index4.js';
|
|
7
|
+
import { s } from './misc.js';
|
|
8
|
+
import vite from 'vite';
|
|
9
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
10
|
+
import 'sade';
|
|
11
|
+
import 'child_process';
|
|
12
|
+
import 'net';
|
|
13
|
+
import 'url';
|
|
14
|
+
import 'os';
|
|
15
|
+
import './error.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {{
|
|
19
|
+
* cwd: string;
|
|
20
|
+
* assets_base: string;
|
|
21
|
+
* config: import('types/config').ValidatedConfig
|
|
22
|
+
* manifest_data: import('types/internal').ManifestData
|
|
23
|
+
* build_dir: string;
|
|
24
|
+
* output_dir: string;
|
|
25
|
+
* client_entry_file: string;
|
|
26
|
+
* service_worker_entry_file: string | null;
|
|
27
|
+
* }} options
|
|
28
|
+
* @param {import('vite').Manifest} client_manifest
|
|
29
|
+
*/
|
|
30
|
+
async function build_service_worker(
|
|
31
|
+
{ cwd, assets_base, config, manifest_data, build_dir, output_dir, service_worker_entry_file },
|
|
32
|
+
client_manifest
|
|
33
|
+
) {
|
|
34
|
+
// TODO add any assets referenced in template .html file, e.g. favicon?
|
|
35
|
+
const app_files = new Set();
|
|
36
|
+
for (const key in client_manifest) {
|
|
37
|
+
const { file, css } = client_manifest[key];
|
|
38
|
+
app_files.add(file);
|
|
39
|
+
if (css) {
|
|
40
|
+
css.forEach((file) => {
|
|
41
|
+
app_files.add(file);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fs__default.writeFileSync(
|
|
47
|
+
`${build_dir}/runtime/service-worker.js`,
|
|
48
|
+
`
|
|
49
|
+
export const timestamp = ${Date.now()};
|
|
50
|
+
|
|
51
|
+
export const build = [
|
|
52
|
+
${Array.from(app_files)
|
|
53
|
+
.map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
|
|
54
|
+
.join(',\n\t\t\t\t')}
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
export const files = [
|
|
58
|
+
${manifest_data.assets
|
|
59
|
+
.map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
|
|
60
|
+
.join(',\n\t\t\t\t')}
|
|
61
|
+
];
|
|
62
|
+
`
|
|
63
|
+
.replace(/^\t{3}/gm, '')
|
|
64
|
+
.trim()
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
/** @type {[any, string[]]} */
|
|
68
|
+
const [merged_config, conflicts] = deep_merge(config.kit.vite(), {
|
|
69
|
+
configFile: false,
|
|
70
|
+
root: cwd,
|
|
71
|
+
base: assets_base,
|
|
72
|
+
build: {
|
|
73
|
+
lib: {
|
|
74
|
+
entry: service_worker_entry_file,
|
|
75
|
+
name: 'app',
|
|
76
|
+
formats: ['es']
|
|
77
|
+
},
|
|
78
|
+
rollupOptions: {
|
|
79
|
+
output: {
|
|
80
|
+
entryFileNames: 'service-worker.js'
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
outDir: `${output_dir}/client`,
|
|
84
|
+
emptyOutDir: false
|
|
85
|
+
},
|
|
86
|
+
resolve: {
|
|
87
|
+
alias: {
|
|
88
|
+
'$service-worker': path__default.resolve(`${build_dir}/runtime/service-worker`),
|
|
89
|
+
$lib: config.kit.files.lib
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
print_config_conflicts(conflicts, 'kit.vite.', 'build_service_worker');
|
|
95
|
+
|
|
96
|
+
await vite.build(merged_config);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @param {import('vite').UserConfig} config */
|
|
100
|
+
async function create_build(config) {
|
|
101
|
+
const { output } = /** @type {import('rollup').RollupOutput} */ (await vite.build(config));
|
|
102
|
+
|
|
103
|
+
const chunks = /** @type {import('rollup').OutputChunk[]} */ (
|
|
104
|
+
output.filter((output) => output.type === 'chunk')
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const assets = /** @type {import('rollup').OutputAsset[]} */ (
|
|
108
|
+
output.filter((output) => output.type === 'asset')
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
return { chunks, assets };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {string} file
|
|
116
|
+
* @param {import('vite').Manifest} manifest
|
|
117
|
+
* @param {Set<string>} css
|
|
118
|
+
* @param {Set<string>} js
|
|
119
|
+
* @returns
|
|
120
|
+
*/
|
|
121
|
+
function find_deps(file, manifest, js, css) {
|
|
122
|
+
const chunk = manifest[file];
|
|
123
|
+
|
|
124
|
+
if (js.has(chunk.file)) return;
|
|
125
|
+
js.add(chunk.file);
|
|
126
|
+
|
|
127
|
+
if (chunk.css) {
|
|
128
|
+
chunk.css.forEach((file) => css.add(file));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (chunk.imports) {
|
|
132
|
+
chunk.imports.forEach((file) => find_deps(file, manifest, js, css));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @param {{
|
|
138
|
+
* cwd: string;
|
|
139
|
+
* assets_base: string;
|
|
140
|
+
* config: import('types/config').ValidatedConfig
|
|
141
|
+
* manifest_data: import('types/internal').ManifestData
|
|
142
|
+
* build_dir: string;
|
|
143
|
+
* output_dir: string;
|
|
144
|
+
* client_entry_file: string;
|
|
145
|
+
* service_worker_entry_file: string | null;
|
|
146
|
+
* service_worker_register: boolean;
|
|
147
|
+
* }} options
|
|
148
|
+
*/
|
|
149
|
+
async function build_client({
|
|
150
|
+
cwd,
|
|
151
|
+
assets_base,
|
|
152
|
+
config,
|
|
153
|
+
manifest_data,
|
|
154
|
+
build_dir,
|
|
155
|
+
output_dir,
|
|
156
|
+
client_entry_file
|
|
157
|
+
}) {
|
|
158
|
+
create_app({
|
|
159
|
+
manifest_data,
|
|
160
|
+
output: build_dir,
|
|
161
|
+
cwd
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
copy_assets(build_dir);
|
|
165
|
+
|
|
166
|
+
process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : '';
|
|
167
|
+
|
|
168
|
+
const client_out_dir = `${output_dir}/client/${config.kit.appDir}`;
|
|
169
|
+
|
|
170
|
+
/** @type {Record<string, string>} */
|
|
171
|
+
const input = {
|
|
172
|
+
start: path__default.resolve(cwd, client_entry_file)
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// This step is optional — Vite/Rollup will create the necessary chunks
|
|
176
|
+
// for everything regardless — but it means that entry chunks reflect
|
|
177
|
+
// their location in the source code, which is helpful for debugging
|
|
178
|
+
manifest_data.components.forEach((file) => {
|
|
179
|
+
const resolved = path__default.resolve(cwd, file);
|
|
180
|
+
const relative = path__default.relative(config.kit.files.routes, resolved);
|
|
181
|
+
|
|
182
|
+
const name = relative.startsWith('..')
|
|
183
|
+
? path__default.basename(file)
|
|
184
|
+
: posixify(path__default.join('pages', relative));
|
|
185
|
+
input[name] = resolved;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
/** @type {[any, string[]]} */
|
|
189
|
+
const [merged_config, conflicts] = deep_merge(config.kit.vite(), {
|
|
190
|
+
configFile: false,
|
|
191
|
+
root: cwd,
|
|
192
|
+
base: assets_base,
|
|
193
|
+
build: {
|
|
194
|
+
cssCodeSplit: true,
|
|
195
|
+
manifest: true,
|
|
196
|
+
outDir: client_out_dir,
|
|
197
|
+
polyfillDynamicImport: false,
|
|
198
|
+
rollupOptions: {
|
|
199
|
+
input,
|
|
200
|
+
output: {
|
|
201
|
+
entryFileNames: '[name]-[hash].js',
|
|
202
|
+
chunkFileNames: 'chunks/[name]-[hash].js',
|
|
203
|
+
assetFileNames: 'assets/[name]-[hash][extname]'
|
|
204
|
+
},
|
|
205
|
+
preserveEntrySignatures: 'strict'
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
resolve: {
|
|
209
|
+
alias: {
|
|
210
|
+
$app: path__default.resolve(`${build_dir}/runtime/app`),
|
|
211
|
+
$lib: config.kit.files.lib
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
plugins: [
|
|
215
|
+
svelte({
|
|
216
|
+
extensions: config.extensions,
|
|
217
|
+
emitCss: !config.kit.amp,
|
|
218
|
+
compilerOptions: {
|
|
219
|
+
hydratable: !!config.kit.hydrate
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
]
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
print_config_conflicts(conflicts, 'kit.vite.', 'build_client');
|
|
226
|
+
|
|
227
|
+
const { chunks, assets } = await create_build(merged_config);
|
|
228
|
+
|
|
229
|
+
/** @type {import('vite').Manifest} */
|
|
230
|
+
const vite_manifest = JSON.parse(fs__default.readFileSync(`${client_out_dir}/manifest.json`, 'utf-8'));
|
|
231
|
+
|
|
232
|
+
const entry_js = new Set();
|
|
233
|
+
const entry_css = new Set();
|
|
234
|
+
find_deps(client_entry_file, vite_manifest, entry_js, entry_css);
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
assets,
|
|
238
|
+
chunks,
|
|
239
|
+
entry: {
|
|
240
|
+
file: vite_manifest[client_entry_file].file,
|
|
241
|
+
js: Array.from(entry_js),
|
|
242
|
+
css: Array.from(entry_css)
|
|
243
|
+
},
|
|
244
|
+
vite_manifest
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @param {{
|
|
250
|
+
* runtime: string,
|
|
251
|
+
* hooks: string,
|
|
252
|
+
* config: import('types/config').ValidatedConfig
|
|
253
|
+
* }} opts
|
|
254
|
+
* @returns
|
|
255
|
+
*/
|
|
256
|
+
const template = ({ config, hooks, runtime }) => `
|
|
257
|
+
import { respond } from '${runtime}';
|
|
258
|
+
import root from './generated/root.svelte';
|
|
259
|
+
import { set_paths, assets, base } from './runtime/paths.js';
|
|
260
|
+
import { set_prerendering } from './runtime/env.js';
|
|
261
|
+
import * as user_hooks from ${s(hooks)};
|
|
262
|
+
|
|
263
|
+
const template = ({ head, body }) => ${s(fs__default.readFileSync(config.kit.files.template, 'utf-8'))
|
|
264
|
+
.replace('%svelte.head%', '" + head + "')
|
|
265
|
+
.replace('%svelte.body%', '" + body + "')};
|
|
266
|
+
|
|
267
|
+
let read = null;
|
|
268
|
+
|
|
269
|
+
set_paths(${s(config.kit.paths)});
|
|
270
|
+
|
|
271
|
+
// this looks redundant, but the indirection allows us to access
|
|
272
|
+
// named imports without triggering Rollup's missing import detection
|
|
273
|
+
const get_hooks = hooks => ({
|
|
274
|
+
getSession: hooks.getSession || (() => ({})),
|
|
275
|
+
handle: hooks.handle || (({ request, resolve }) => resolve(request)),
|
|
276
|
+
handleError: hooks.handleError || (({ error }) => console.error(error.stack)),
|
|
277
|
+
externalFetch: hooks.externalFetch || fetch
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
let default_protocol = 'https';
|
|
281
|
+
|
|
282
|
+
// allow paths to be globally overridden
|
|
283
|
+
// in svelte-kit preview and in prerendering
|
|
284
|
+
export function override(settings) {
|
|
285
|
+
default_protocol = settings.protocol || default_protocol;
|
|
286
|
+
set_paths(settings.paths);
|
|
287
|
+
set_prerendering(settings.prerendering);
|
|
288
|
+
read = settings.read;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export class App {
|
|
292
|
+
constructor(manifest) {
|
|
293
|
+
const hooks = get_hooks(user_hooks);
|
|
294
|
+
|
|
295
|
+
this.options = {
|
|
296
|
+
amp: ${config.kit.amp},
|
|
297
|
+
dev: false,
|
|
298
|
+
floc: ${config.kit.floc},
|
|
299
|
+
get_stack: error => String(error), // for security
|
|
300
|
+
handle_error: (error, request) => {
|
|
301
|
+
hooks.handleError({ error, request });
|
|
302
|
+
error.stack = this.options.get_stack(error);
|
|
303
|
+
},
|
|
304
|
+
hooks,
|
|
305
|
+
hydrate: ${s(config.kit.hydrate)},
|
|
306
|
+
manifest,
|
|
307
|
+
paths: { base, assets },
|
|
308
|
+
prefix: assets + '/${config.kit.appDir}/',
|
|
309
|
+
prerender: ${config.kit.prerender.enabled},
|
|
310
|
+
read,
|
|
311
|
+
root,
|
|
312
|
+
service_worker: ${
|
|
313
|
+
config.kit.files.serviceWorker && config.kit.serviceWorker.register
|
|
314
|
+
? "'/service-worker.js'"
|
|
315
|
+
: 'null'
|
|
316
|
+
},
|
|
317
|
+
router: ${s(config.kit.router)},
|
|
318
|
+
ssr: ${s(config.kit.ssr)},
|
|
319
|
+
target: ${s(config.kit.target)},
|
|
320
|
+
template,
|
|
321
|
+
trailing_slash: ${s(config.kit.trailingSlash)}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
render(request, {
|
|
326
|
+
prerender
|
|
327
|
+
} = {}) {
|
|
328
|
+
// TODO remove this for 1.0
|
|
329
|
+
if (Object.keys(request).sort().join() !== 'headers,method,rawBody,url') {
|
|
330
|
+
throw new Error('Adapters should call app.render({ url, method, headers, rawBody })');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const host = ${
|
|
334
|
+
config.kit.host
|
|
335
|
+
? s(config.kit.host)
|
|
336
|
+
: `request.headers[${s(config.kit.headers.host || 'host')}]`
|
|
337
|
+
};
|
|
338
|
+
const protocol = ${
|
|
339
|
+
config.kit.protocol
|
|
340
|
+
? s(config.kit.protocol)
|
|
341
|
+
: config.kit.headers.protocol
|
|
342
|
+
? `request.headers[${s(config.kit.headers.protocol)}] || default_protocol`
|
|
343
|
+
: 'default_protocol'
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return respond({ ...request, url: new URL(request.url, protocol + '://' + host) }, this.options, { prerender });
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
`;
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* @param {{
|
|
353
|
+
* cwd: string;
|
|
354
|
+
* assets_base: string;
|
|
355
|
+
* config: import('types/config').ValidatedConfig
|
|
356
|
+
* manifest_data: import('types/internal').ManifestData
|
|
357
|
+
* build_dir: string;
|
|
358
|
+
* output_dir: string;
|
|
359
|
+
* }} options
|
|
360
|
+
* @param {string} runtime
|
|
361
|
+
*/
|
|
362
|
+
async function build_server(
|
|
363
|
+
{ cwd, assets_base, config, manifest_data, build_dir, output_dir },
|
|
364
|
+
runtime
|
|
365
|
+
) {
|
|
366
|
+
let hooks_file = resolve_entry(config.kit.files.hooks);
|
|
367
|
+
if (!hooks_file || !fs__default.existsSync(hooks_file)) {
|
|
368
|
+
hooks_file = path__default.resolve(cwd, `${SVELTE_KIT}/build/hooks.js`);
|
|
369
|
+
fs__default.writeFileSync(hooks_file, '');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** @type {Record<string, string>} */
|
|
373
|
+
const input = {
|
|
374
|
+
app: `${build_dir}/app.js`
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// add entry points for every endpoint...
|
|
378
|
+
manifest_data.routes.forEach((route) => {
|
|
379
|
+
if (route.type === 'endpoint') {
|
|
380
|
+
const resolved = path__default.resolve(cwd, route.file);
|
|
381
|
+
const relative = path__default.relative(config.kit.files.routes, resolved);
|
|
382
|
+
const name = posixify(path__default.join('entries/endpoints', relative.replace(/\.js$/, '')));
|
|
383
|
+
input[name] = resolved;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// ...and every component used by pages
|
|
388
|
+
manifest_data.components.forEach((file) => {
|
|
389
|
+
const resolved = path__default.resolve(cwd, file);
|
|
390
|
+
const relative = path__default.relative(config.kit.files.routes, resolved);
|
|
391
|
+
|
|
392
|
+
const name = relative.startsWith('..')
|
|
393
|
+
? posixify(path__default.join('entries/pages', path__default.basename(file)))
|
|
394
|
+
: posixify(path__default.join('entries/pages', relative));
|
|
395
|
+
input[name] = resolved;
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
/** @type {(file: string) => string} */
|
|
399
|
+
const app_relative = (file) => {
|
|
400
|
+
const relative_file = path__default.relative(build_dir, path__default.resolve(cwd, file));
|
|
401
|
+
return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
// prettier-ignore
|
|
405
|
+
fs__default.writeFileSync(
|
|
406
|
+
input.app,
|
|
407
|
+
template({
|
|
408
|
+
config,
|
|
409
|
+
hooks: app_relative(hooks_file),
|
|
410
|
+
runtime
|
|
411
|
+
})
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
/** @type {import('vite').UserConfig} */
|
|
415
|
+
const vite_config = config.kit.vite();
|
|
416
|
+
|
|
417
|
+
const default_config = {
|
|
418
|
+
build: {
|
|
419
|
+
target: 'es2020'
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// don't warn on overriding defaults
|
|
424
|
+
const [modified_vite_config] = deep_merge(default_config, vite_config);
|
|
425
|
+
|
|
426
|
+
/** @type {[any, string[]]} */
|
|
427
|
+
const [merged_config, conflicts] = deep_merge(modified_vite_config, {
|
|
428
|
+
configFile: false,
|
|
429
|
+
root: cwd,
|
|
430
|
+
base: assets_base,
|
|
431
|
+
build: {
|
|
432
|
+
ssr: true,
|
|
433
|
+
outDir: `${output_dir}/server`,
|
|
434
|
+
manifest: true,
|
|
435
|
+
polyfillDynamicImport: false,
|
|
436
|
+
rollupOptions: {
|
|
437
|
+
input,
|
|
438
|
+
output: {
|
|
439
|
+
format: 'esm',
|
|
440
|
+
entryFileNames: '[name].js',
|
|
441
|
+
chunkFileNames: 'chunks/[name]-[hash].js',
|
|
442
|
+
assetFileNames: 'assets/[name]-[hash][extname]'
|
|
443
|
+
},
|
|
444
|
+
preserveEntrySignatures: 'strict'
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
plugins: [
|
|
448
|
+
svelte({
|
|
449
|
+
extensions: config.extensions,
|
|
450
|
+
compilerOptions: {
|
|
451
|
+
hydratable: !!config.kit.hydrate
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
],
|
|
455
|
+
resolve: {
|
|
456
|
+
alias: {
|
|
457
|
+
$app: path__default.resolve(`${build_dir}/runtime/app`),
|
|
458
|
+
$lib: config.kit.files.lib
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
print_config_conflicts(conflicts, 'kit.vite.', 'build_server');
|
|
464
|
+
|
|
465
|
+
const { chunks } = await create_build(merged_config);
|
|
466
|
+
|
|
467
|
+
/** @type {Record<string, string[]>} */
|
|
468
|
+
const lookup = {};
|
|
469
|
+
chunks.forEach((chunk) => {
|
|
470
|
+
if (!chunk.facadeModuleId) return;
|
|
471
|
+
const id = chunk.facadeModuleId.slice(cwd.length + 1);
|
|
472
|
+
lookup[id] = chunk.exports;
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
/** @type {Record<string, import('types/internal').HttpMethod[]>} */
|
|
476
|
+
const methods = {};
|
|
477
|
+
manifest_data.routes.forEach((route) => {
|
|
478
|
+
if (route.type === 'endpoint' && lookup[route.file]) {
|
|
479
|
+
methods[route.file] = lookup[route.file]
|
|
480
|
+
.map((x) => /** @type {import('types/internal').HttpMethod} */ (method_names[x]))
|
|
481
|
+
.filter(Boolean);
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
return {
|
|
486
|
+
chunks,
|
|
487
|
+
/** @type {import('vite').Manifest} */
|
|
488
|
+
vite_manifest: JSON.parse(fs__default.readFileSync(`${output_dir}/server/manifest.json`, 'utf-8')),
|
|
489
|
+
methods: get_methods(cwd, chunks, manifest_data)
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** @type {Record<string, string>} */
|
|
494
|
+
const method_names = {
|
|
495
|
+
get: 'get',
|
|
496
|
+
head: 'head',
|
|
497
|
+
post: 'post',
|
|
498
|
+
put: 'put',
|
|
499
|
+
del: 'delete',
|
|
500
|
+
patch: 'patch'
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
*
|
|
505
|
+
* @param {string} cwd
|
|
506
|
+
* @param {import('rollup').OutputChunk[]} output
|
|
507
|
+
* @param {import('types/internal').ManifestData} manifest_data
|
|
508
|
+
*/
|
|
509
|
+
function get_methods(cwd, output, manifest_data) {
|
|
510
|
+
/** @type {Record<string, string[]>} */
|
|
511
|
+
const lookup = {};
|
|
512
|
+
output.forEach((chunk) => {
|
|
513
|
+
if (!chunk.facadeModuleId) return;
|
|
514
|
+
const id = chunk.facadeModuleId.slice(cwd.length + 1);
|
|
515
|
+
lookup[id] = chunk.exports;
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
/** @type {Record<string, import('types/internal').HttpMethod[]>} */
|
|
519
|
+
const methods = {};
|
|
520
|
+
manifest_data.routes.forEach((route) => {
|
|
521
|
+
if (route.type === 'endpoint' && lookup[route.file]) {
|
|
522
|
+
methods[route.file] = lookup[route.file]
|
|
523
|
+
.map((x) => /** @type {import('types/internal').HttpMethod} */ (method_names[x]))
|
|
524
|
+
.filter(Boolean);
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
return methods;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* @param {import('types/config').ValidatedConfig} config
|
|
533
|
+
* @param {{
|
|
534
|
+
* cwd?: string;
|
|
535
|
+
* runtime?: string;
|
|
536
|
+
* }} [opts]
|
|
537
|
+
* @returns {Promise<import('types/internal').BuildData>}
|
|
538
|
+
*/
|
|
539
|
+
async function build(config, { cwd = process.cwd(), runtime = './kit.js' } = {}) {
|
|
540
|
+
const build_dir = path__default.resolve(cwd, `${SVELTE_KIT}/build`);
|
|
541
|
+
|
|
542
|
+
rimraf(build_dir);
|
|
543
|
+
|
|
544
|
+
const output_dir = path__default.resolve(cwd, `${SVELTE_KIT}/output`);
|
|
545
|
+
|
|
546
|
+
const options = {
|
|
547
|
+
cwd,
|
|
548
|
+
config,
|
|
549
|
+
build_dir,
|
|
550
|
+
// TODO this is so that Vite's preloading works. Unfortunately, it fails
|
|
551
|
+
// during `svelte-kit preview`, because we use a local asset path. If Vite
|
|
552
|
+
// used relative paths, I _think_ this could get fixed. Issue here:
|
|
553
|
+
// https://github.com/vitejs/vite/issues/2009
|
|
554
|
+
assets_base: `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/`,
|
|
555
|
+
manifest_data: create_manifest_data({
|
|
556
|
+
config,
|
|
557
|
+
output: build_dir,
|
|
558
|
+
cwd
|
|
559
|
+
}),
|
|
560
|
+
output_dir,
|
|
561
|
+
client_entry_file: `${SVELTE_KIT}/build/runtime/internal/start.js`,
|
|
562
|
+
service_worker_entry_file: resolve_entry(config.kit.files.serviceWorker),
|
|
563
|
+
service_worker_register: config.kit.serviceWorker.register
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
const client = await build_client(options);
|
|
567
|
+
const server = await build_server(options, runtime);
|
|
568
|
+
|
|
569
|
+
const styles_lookup = new Map();
|
|
570
|
+
if (options.config.kit.amp) {
|
|
571
|
+
client.assets.forEach((asset) => {
|
|
572
|
+
if (asset.fileName.endsWith('.css')) {
|
|
573
|
+
styles_lookup.set(asset.fileName, asset.source);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
mkdirp(`${output_dir}/server/nodes`);
|
|
579
|
+
options.manifest_data.components.forEach((component, i) => {
|
|
580
|
+
const file = `${output_dir}/server/nodes/${i}.js`;
|
|
581
|
+
|
|
582
|
+
const js = new Set();
|
|
583
|
+
const css = new Set();
|
|
584
|
+
find_deps(component, client.vite_manifest, js, css);
|
|
585
|
+
|
|
586
|
+
const styles = config.kit.amp && Array.from(css).map((file) => styles_lookup.get(file));
|
|
587
|
+
|
|
588
|
+
const node = `import * as module from '../${server.vite_manifest[component].file}';
|
|
589
|
+
export { module };
|
|
590
|
+
export const entry = '${client.vite_manifest[component].file}';
|
|
591
|
+
export const js = ${JSON.stringify(Array.from(js))};
|
|
592
|
+
export const css = ${JSON.stringify(Array.from(css))};
|
|
593
|
+
${styles ? `export const styles = ${s(styles)}` : ''}
|
|
594
|
+
`.replace(/^\t\t\t/gm, '');
|
|
595
|
+
|
|
596
|
+
writeFileSync(file, node);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
if (options.service_worker_entry_file) {
|
|
600
|
+
if (config.kit.paths.assets) {
|
|
601
|
+
throw new Error('Cannot use service worker alongside config.kit.paths.assets');
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
await build_service_worker(options, client.vite_manifest);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const build_data = {
|
|
608
|
+
app_dir: config.kit.appDir,
|
|
609
|
+
manifest_data: options.manifest_data,
|
|
610
|
+
client,
|
|
611
|
+
server,
|
|
612
|
+
static: options.manifest_data.assets.map((asset) => posixify(asset.file)),
|
|
613
|
+
entries: options.manifest_data.routes
|
|
614
|
+
.map((route) => (route.type === 'page' ? route.path : ''))
|
|
615
|
+
.filter(Boolean)
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
const manifest = `export const manifest = ${generate_manifest(build_data, '.')};\n`;
|
|
619
|
+
fs__default.writeFileSync(`${output_dir}/server/manifest.js`, manifest);
|
|
620
|
+
|
|
621
|
+
return build_data;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export { build };
|