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