@sveltejs/kit 1.0.0-next.41 → 1.0.0-next.410
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/package.json +97 -63
- package/src/cli.js +119 -0
- package/src/core/adapt/builder.js +207 -0
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/src/core/config/options.js +488 -0
- 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/src/core/sync/write_tsconfig.js +189 -0
- package/src/core/sync/write_types.js +723 -0
- package/src/core/utils.js +58 -0
- package/src/hooks.js +26 -0
- 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/src/runtime/app/env.js +11 -0
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +94 -0
- package/src/runtime/client/ambient.d.ts +17 -0
- package/src/runtime/client/client.js +1269 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/src/runtime/client/singletons.js +11 -0
- 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/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +7 -0
- package/src/runtime/env-public.js +7 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/src/runtime/paths.js +11 -0
- 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/src/utils/filesystem.js +108 -0
- 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 -1
- package/types/ambient.d.ts +368 -0
- package/types/index.d.ts +345 -0
- package/types/internal.d.ts +313 -0
- package/types/private.d.ts +236 -0
- package/CHANGELOG.md +0 -419
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -41
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -19
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -770
- package/assets/runtime/paths.js +0 -12
- package/dist/api.js +0 -28
- package/dist/api.js.map +0 -1
- package/dist/chunks/index.js +0 -3519
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -524
- package/dist/chunks/index5.js +0 -761
- package/dist/chunks/index6.js +0 -322
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -546
- package/dist/cli.js.map +0 -1
- package/dist/create_app.js +0 -592
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -392
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -3519
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -320
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -323
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -247
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -761
- package/dist/index6.js.map +0 -1
- package/dist/renderer.js +0 -2499
- package/dist/renderer.js.map +0 -1
- package/dist/ssr.js +0 -2581
- package/dist/standard.js +0 -100
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -84
- package/dist/utils.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import colors from 'kleur';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { posixify } from '../utils/filesystem.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolved path of the `runtime` directory
|
|
8
|
+
*
|
|
9
|
+
* TODO Windows issue:
|
|
10
|
+
* Vite or sth else somehow sets the driver letter inconsistently to lower or upper case depending on the run environment.
|
|
11
|
+
* In playwright debug mode run through VS Code this a root-to-lowercase conversion is needed in order for the tests to run.
|
|
12
|
+
* If we do this conversion in other cases it has the opposite effect though and fails.
|
|
13
|
+
*/
|
|
14
|
+
export const runtime_directory = posixify(fileURLToPath(new URL('../runtime', import.meta.url)));
|
|
15
|
+
|
|
16
|
+
/** Prefix for the `runtime` directory, for use with import declarations */
|
|
17
|
+
export const runtime_prefix = posixify_path(runtime_directory);
|
|
18
|
+
|
|
19
|
+
/** @param {string} str */
|
|
20
|
+
function posixify_path(str) {
|
|
21
|
+
const parsed = path.parse(str);
|
|
22
|
+
return `/${parsed.dir.slice(parsed.root.length).split(path.sep).join('/')}/${parsed.base}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function noop() {}
|
|
26
|
+
|
|
27
|
+
/** @param {{ verbose: boolean }} opts */
|
|
28
|
+
export function logger({ verbose }) {
|
|
29
|
+
/** @type {import('types').Logger} */
|
|
30
|
+
const log = (msg) => console.log(msg.replace(/^/gm, ' '));
|
|
31
|
+
|
|
32
|
+
/** @param {string} msg */
|
|
33
|
+
const err = (msg) => console.error(msg.replace(/^/gm, ' '));
|
|
34
|
+
|
|
35
|
+
log.success = (msg) => log(colors.green(`✔ ${msg}`));
|
|
36
|
+
log.error = (msg) => err(colors.bold().red(msg));
|
|
37
|
+
log.warn = (msg) => log(colors.bold().yellow(msg));
|
|
38
|
+
|
|
39
|
+
log.minor = verbose ? (msg) => log(colors.grey(msg)) : noop;
|
|
40
|
+
log.info = verbose ? log : noop;
|
|
41
|
+
|
|
42
|
+
return log;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {import('types').ManifestData} manifest_data */
|
|
46
|
+
export function get_mime_lookup(manifest_data) {
|
|
47
|
+
/** @type {Record<string, string>} */
|
|
48
|
+
const mime = {};
|
|
49
|
+
|
|
50
|
+
manifest_data.assets.forEach((asset) => {
|
|
51
|
+
if (asset.type) {
|
|
52
|
+
const ext = path.extname(asset.file);
|
|
53
|
+
mime[ext] = asset.type;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return mime;
|
|
58
|
+
}
|
package/src/hooks.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {...import('types').Handle} handlers
|
|
3
|
+
* @returns {import('types').Handle}
|
|
4
|
+
*/
|
|
5
|
+
export function sequence(...handlers) {
|
|
6
|
+
const length = handlers.length;
|
|
7
|
+
if (!length) return ({ event, resolve }) => resolve(event);
|
|
8
|
+
|
|
9
|
+
return ({ event, resolve }) => {
|
|
10
|
+
return apply_handle(0, event);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {number} i
|
|
14
|
+
* @param {import('types').RequestEvent} event
|
|
15
|
+
* @returns {import('types').MaybePromise<Response>}
|
|
16
|
+
*/
|
|
17
|
+
function apply_handle(i, event) {
|
|
18
|
+
const handle = handlers[i];
|
|
19
|
+
|
|
20
|
+
return handle({
|
|
21
|
+
event,
|
|
22
|
+
resolve: i < length - 1 ? (event) => apply_handle(i + 1, event) : resolve
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { HttpError, Redirect } from './private.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an `HttpError` object with an HTTP status code and an optional message.
|
|
5
|
+
* This object, if thrown during request handling, will cause SvelteKit to
|
|
6
|
+
* return an error response without invoking `handleError`
|
|
7
|
+
* @param {number} status
|
|
8
|
+
* @param {string | undefined} [message]
|
|
9
|
+
*/
|
|
10
|
+
export function error(status, message) {
|
|
11
|
+
return new HttpError(status, message);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a `Redirect` object. If thrown during request handling, SvelteKit will
|
|
16
|
+
* return a redirect response.
|
|
17
|
+
* @param {number} status
|
|
18
|
+
* @param {string} location
|
|
19
|
+
*/
|
|
20
|
+
export function redirect(status, location) {
|
|
21
|
+
if (isNaN(status) || status < 300 || status > 399) {
|
|
22
|
+
throw new Error('Invalid status code');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return new Redirect(status, location);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Generates a JSON `Response` object from the supplied data.
|
|
30
|
+
* @param {any} data
|
|
31
|
+
* @param {ResponseInit} [init]
|
|
32
|
+
*/
|
|
33
|
+
export function json(data, init) {
|
|
34
|
+
// TODO deprecate this in favour of `Response.json` when it's
|
|
35
|
+
// more widely supported
|
|
36
|
+
const headers = new Headers(init?.headers);
|
|
37
|
+
if (!headers.has('content-type')) {
|
|
38
|
+
headers.set('content-type', 'application/json');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return new Response(JSON.stringify(data), {
|
|
42
|
+
...init,
|
|
43
|
+
headers
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class HttpError {
|
|
2
|
+
// without these, things like `$page.error.stack` will error. we don't want to
|
|
3
|
+
// include a stack for these sorts of errors, but we also don't want red
|
|
4
|
+
// squigglies everywhere, so this feels like a not-terribile compromise
|
|
5
|
+
name = 'HttpError';
|
|
6
|
+
|
|
7
|
+
/** @type {void} */
|
|
8
|
+
stack = undefined;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {number} status
|
|
12
|
+
* @param {string | undefined} message
|
|
13
|
+
*/
|
|
14
|
+
constructor(status, message) {
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.message = message ?? `Error: ${status}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
toString() {
|
|
20
|
+
return this.message;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Redirect {
|
|
25
|
+
/**
|
|
26
|
+
* @param {number} status
|
|
27
|
+
* @param {string} location
|
|
28
|
+
*/
|
|
29
|
+
constructor(status, location) {
|
|
30
|
+
this.status = status;
|
|
31
|
+
this.location = location;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as set_cookie_parser from 'set-cookie-parser';
|
|
2
|
+
|
|
3
|
+
/** @param {import('http').IncomingMessage} req */
|
|
4
|
+
function get_raw_body(req) {
|
|
5
|
+
const h = req.headers;
|
|
6
|
+
|
|
7
|
+
if (!h['content-type']) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const length = Number(h['content-length']);
|
|
12
|
+
|
|
13
|
+
// check if no request body
|
|
14
|
+
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
|
|
15
|
+
if (isNaN(length) && h['transfer-encoding'] == null) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (req.destroyed) {
|
|
20
|
+
const readable = new ReadableStream();
|
|
21
|
+
readable.cancel();
|
|
22
|
+
return readable;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let size = 0;
|
|
26
|
+
let cancelled = false;
|
|
27
|
+
|
|
28
|
+
return new ReadableStream({
|
|
29
|
+
start(controller) {
|
|
30
|
+
req.on('error', (error) => {
|
|
31
|
+
controller.error(error);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
req.on('end', () => {
|
|
35
|
+
if (cancelled) return;
|
|
36
|
+
controller.close();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
req.on('data', (chunk) => {
|
|
40
|
+
if (cancelled) return;
|
|
41
|
+
|
|
42
|
+
size += chunk.length;
|
|
43
|
+
if (size > length) {
|
|
44
|
+
controller.error(new Error('content-length exceeded'));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
controller.enqueue(chunk);
|
|
49
|
+
|
|
50
|
+
if (controller.desiredSize === null || controller.desiredSize <= 0) {
|
|
51
|
+
req.pause();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
pull() {
|
|
57
|
+
req.resume();
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
cancel(reason) {
|
|
61
|
+
cancelled = true;
|
|
62
|
+
req.destroy(reason);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @type {import('@sveltejs/kit/node').getRequest} */
|
|
68
|
+
export async function getRequest(base, req) {
|
|
69
|
+
let headers = /** @type {Record<string, string>} */ (req.headers);
|
|
70
|
+
if (req.httpVersionMajor === 2) {
|
|
71
|
+
// we need to strip out the HTTP/2 pseudo-headers because node-fetch's
|
|
72
|
+
// Request implementation doesn't like them
|
|
73
|
+
// TODO is this still true with Node 18
|
|
74
|
+
headers = Object.assign({}, headers);
|
|
75
|
+
delete headers[':method'];
|
|
76
|
+
delete headers[':path'];
|
|
77
|
+
delete headers[':authority'];
|
|
78
|
+
delete headers[':scheme'];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return new Request(base + req.url, {
|
|
82
|
+
method: req.method,
|
|
83
|
+
headers,
|
|
84
|
+
body: get_raw_body(req)
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @type {import('@sveltejs/kit/node').setResponse} */
|
|
89
|
+
export async function setResponse(res, response) {
|
|
90
|
+
const headers = Object.fromEntries(response.headers);
|
|
91
|
+
|
|
92
|
+
if (response.headers.has('set-cookie')) {
|
|
93
|
+
const header = /** @type {string} */ (response.headers.get('set-cookie'));
|
|
94
|
+
const split = set_cookie_parser.splitCookiesString(header);
|
|
95
|
+
|
|
96
|
+
// @ts-expect-error
|
|
97
|
+
headers['set-cookie'] = split;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
res.writeHead(response.status, headers);
|
|
101
|
+
|
|
102
|
+
if (!response.body) {
|
|
103
|
+
res.end();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const reader = response.body.getReader();
|
|
108
|
+
|
|
109
|
+
if (res.destroyed) {
|
|
110
|
+
reader.cancel();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const cancel = (/** @type {Error|undefined} */ error) => {
|
|
115
|
+
res.off('close', cancel);
|
|
116
|
+
res.off('error', cancel);
|
|
117
|
+
|
|
118
|
+
// If the reader has already been interrupted with an error earlier,
|
|
119
|
+
// then it will appear here, it is useless, but it needs to be catch.
|
|
120
|
+
reader.cancel(error).catch(() => {});
|
|
121
|
+
if (error) res.destroy(error);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
res.on('close', cancel);
|
|
125
|
+
res.on('error', cancel);
|
|
126
|
+
|
|
127
|
+
next();
|
|
128
|
+
async function next() {
|
|
129
|
+
try {
|
|
130
|
+
for (;;) {
|
|
131
|
+
const { done, value } = await reader.read();
|
|
132
|
+
|
|
133
|
+
if (done) break;
|
|
134
|
+
|
|
135
|
+
if (!res.write(value)) {
|
|
136
|
+
res.once('drain', next);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
res.end();
|
|
141
|
+
} catch (error) {
|
|
142
|
+
cancel(error instanceof Error ? error : new Error(String(error)));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { fetch, Response, Request, Headers } from 'undici';
|
|
2
|
+
import { ReadableStream, TransformStream, WritableStream } from 'stream/web';
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
import { Request as NodeFetchRequest } from 'node-fetch';
|
|
5
|
+
import { webcrypto as crypto } from 'crypto';
|
|
6
|
+
|
|
7
|
+
/** @type {Record<string, any>} */
|
|
8
|
+
const globals = {
|
|
9
|
+
crypto,
|
|
10
|
+
fetch,
|
|
11
|
+
Response,
|
|
12
|
+
// TODO remove the superclass as soon as Undici supports formData
|
|
13
|
+
// https://github.com/nodejs/undici/issues/974
|
|
14
|
+
Request: class extends Request {
|
|
15
|
+
// @ts-expect-error
|
|
16
|
+
formData() {
|
|
17
|
+
return new NodeFetchRequest(this.url, {
|
|
18
|
+
method: this.method,
|
|
19
|
+
headers: this.headers,
|
|
20
|
+
body: this.body && Readable.from(this.body)
|
|
21
|
+
}).formData();
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
Headers,
|
|
25
|
+
ReadableStream,
|
|
26
|
+
TransformStream,
|
|
27
|
+
WritableStream
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// exported for dev/preview and node environments
|
|
31
|
+
export function installPolyfills() {
|
|
32
|
+
for (const name in globals) {
|
|
33
|
+
Object.defineProperty(globalThis, name, {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: globals[name]
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { dirname, join, relative } from 'path';
|
|
3
|
+
import colors from 'kleur';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import { preprocess } from 'svelte/compiler';
|
|
6
|
+
import { copy, mkdirp, rimraf } from '../utils/filesystem.js';
|
|
7
|
+
import { analyze, generate_pkg, resolve_lib_alias, scan, strip_lang_tags, write } from './utils.js';
|
|
8
|
+
import { emit_dts, transpile_ts } from './typescript.js';
|
|
9
|
+
import { write_tsconfig } from '../core/sync/write_tsconfig.js';
|
|
10
|
+
|
|
11
|
+
const essential_files = ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmignore'];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {import('types').ValidatedConfig} config
|
|
15
|
+
* @param {string} cwd
|
|
16
|
+
*/
|
|
17
|
+
export async function build(config, cwd = process.cwd()) {
|
|
18
|
+
const { lib } = config.kit.files;
|
|
19
|
+
const { dir } = config.kit.package;
|
|
20
|
+
|
|
21
|
+
if (!fs.existsSync(lib)) {
|
|
22
|
+
throw new Error(`${lib} does not exist`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
rimraf(dir);
|
|
26
|
+
mkdirp(dir);
|
|
27
|
+
|
|
28
|
+
// Make sure generated tsconfig is up-to-date
|
|
29
|
+
write_tsconfig(config.kit, cwd);
|
|
30
|
+
|
|
31
|
+
const files = scan(config);
|
|
32
|
+
|
|
33
|
+
if (config.kit.package.emitTypes) {
|
|
34
|
+
await emit_dts(config, cwd, files);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const pkg = generate_pkg(cwd, files);
|
|
38
|
+
|
|
39
|
+
if (!pkg.svelte && files.some((file) => file.is_svelte)) {
|
|
40
|
+
// Several heuristics in Kit/vite-plugin-svelte to tell Vite to mark Svelte packages
|
|
41
|
+
// rely on the "svelte" property. Vite/Rollup/Webpack plugin can all deal with it.
|
|
42
|
+
// See https://github.com/sveltejs/kit/issues/1959 for more info and related threads.
|
|
43
|
+
if (pkg.exports['.']) {
|
|
44
|
+
const svelte_export =
|
|
45
|
+
typeof pkg.exports['.'] === 'string'
|
|
46
|
+
? pkg.exports['.']
|
|
47
|
+
: pkg.exports['.'].import || pkg.exports['.'].default;
|
|
48
|
+
if (svelte_export) {
|
|
49
|
+
pkg.svelte = svelte_export;
|
|
50
|
+
} else {
|
|
51
|
+
console.warn(
|
|
52
|
+
'Cannot generate a "svelte" entry point because the "." entry in "exports" is not a string. If you set it by hand, please also set one of the options as a "svelte" entry point\n'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
console.warn(
|
|
57
|
+
'Cannot generate a "svelte" entry point because the "." entry in "exports" is missing. Please specify one or set a "svelte" entry point yourself\n'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
|
|
63
|
+
|
|
64
|
+
for (const file of files) {
|
|
65
|
+
await process_file(config, file);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const whitelist = fs.readdirSync(cwd).filter((file) => {
|
|
69
|
+
const lowercased = file.toLowerCase();
|
|
70
|
+
return essential_files.some((name) => lowercased.startsWith(name.toLowerCase()));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
for (const pathname of whitelist) {
|
|
74
|
+
const full_path = join(cwd, pathname);
|
|
75
|
+
if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure
|
|
76
|
+
|
|
77
|
+
const package_path = join(dir, pathname);
|
|
78
|
+
if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const from = relative(cwd, lib);
|
|
82
|
+
const to = relative(cwd, dir);
|
|
83
|
+
console.log(colors.bold().green(`${from} -> ${to}`));
|
|
84
|
+
console.log(`Successfully built '${pkg.name}' package. To publish it to npm:`);
|
|
85
|
+
console.log(colors.bold().cyan(` cd ${to}`));
|
|
86
|
+
console.log(colors.bold().cyan(' npm publish\n'));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {import('types').ValidatedConfig} config
|
|
91
|
+
*/
|
|
92
|
+
export async function watch(config, cwd = process.cwd()) {
|
|
93
|
+
await build(config, cwd);
|
|
94
|
+
|
|
95
|
+
const message = `\nWatching ${relative(cwd, config.kit.files.lib)} for changes...\n`;
|
|
96
|
+
|
|
97
|
+
console.log(message);
|
|
98
|
+
|
|
99
|
+
const { lib } = config.kit.files;
|
|
100
|
+
const { dir } = config.kit.package;
|
|
101
|
+
|
|
102
|
+
/** @type {Array<{ file: import('./types').File, type: string }>} */
|
|
103
|
+
const pending = [];
|
|
104
|
+
|
|
105
|
+
/** @type {Array<(value?: any) => void>} */
|
|
106
|
+
const fulfillers = [];
|
|
107
|
+
|
|
108
|
+
/** @type {NodeJS.Timeout} */
|
|
109
|
+
let timeout;
|
|
110
|
+
|
|
111
|
+
const watcher = chokidar.watch(lib, { ignoreInitial: true });
|
|
112
|
+
const ready = new Promise((resolve) => watcher.on('ready', resolve));
|
|
113
|
+
|
|
114
|
+
watcher.on('all', async (type, path) => {
|
|
115
|
+
const file = analyze(config, relative(lib, path));
|
|
116
|
+
if (!file.is_included) return;
|
|
117
|
+
|
|
118
|
+
pending.push({ file, type });
|
|
119
|
+
|
|
120
|
+
clearTimeout(timeout);
|
|
121
|
+
timeout = setTimeout(async () => {
|
|
122
|
+
const files = scan(config);
|
|
123
|
+
|
|
124
|
+
let should_update_pkg = false;
|
|
125
|
+
|
|
126
|
+
const events = pending.slice();
|
|
127
|
+
pending.length = 0;
|
|
128
|
+
|
|
129
|
+
for (const { file, type } of events) {
|
|
130
|
+
if ((type === 'unlink' || type === 'add') && file.is_exported) {
|
|
131
|
+
should_update_pkg = true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (type === 'unlink') {
|
|
135
|
+
for (const candidate of [
|
|
136
|
+
file.name,
|
|
137
|
+
`${file.base}.d.ts`,
|
|
138
|
+
`${file.base}.d.mts`,
|
|
139
|
+
`${file.base}.d.cts`
|
|
140
|
+
]) {
|
|
141
|
+
const resolved = join(dir, candidate);
|
|
142
|
+
|
|
143
|
+
if (fs.existsSync(resolved)) {
|
|
144
|
+
fs.unlinkSync(resolved);
|
|
145
|
+
|
|
146
|
+
const parent = dirname(resolved);
|
|
147
|
+
if (parent !== dir && fs.readdirSync(parent).length === 0) {
|
|
148
|
+
fs.rmdirSync(parent);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
console.log(`Removed ${file.dest}`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (type === 'add' || type === 'change') {
|
|
156
|
+
await process_file(config, file);
|
|
157
|
+
console.log(`Processing ${file.name}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (should_update_pkg) {
|
|
162
|
+
const pkg = generate_pkg(cwd, files);
|
|
163
|
+
write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
|
|
164
|
+
console.log('Updated package.json');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (config.kit.package.emitTypes) {
|
|
168
|
+
await emit_dts(config, cwd, scan(config));
|
|
169
|
+
console.log('Updated .d.ts files');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
console.log(message);
|
|
173
|
+
|
|
174
|
+
fulfillers.forEach((fn) => fn());
|
|
175
|
+
}, 100);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
watcher,
|
|
180
|
+
ready,
|
|
181
|
+
settled: () =>
|
|
182
|
+
new Promise((fulfil, reject) => {
|
|
183
|
+
fulfillers.push(fulfil);
|
|
184
|
+
setTimeout(() => reject(new Error('Timed out')), 1000);
|
|
185
|
+
})
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {import('types').ValidatedConfig} config
|
|
191
|
+
* @param {import('./types').File} file
|
|
192
|
+
*/
|
|
193
|
+
async function process_file(config, file) {
|
|
194
|
+
if (!file.is_included) return;
|
|
195
|
+
|
|
196
|
+
const filename = join(config.kit.files.lib, file.name);
|
|
197
|
+
const dest = join(config.kit.package.dir, file.dest);
|
|
198
|
+
|
|
199
|
+
if (file.is_svelte || file.name.endsWith('.ts')) {
|
|
200
|
+
let contents = fs.readFileSync(filename, 'utf-8');
|
|
201
|
+
|
|
202
|
+
if (file.is_svelte) {
|
|
203
|
+
if (config.preprocess) {
|
|
204
|
+
const preprocessed = (await preprocess(contents, config.preprocess, { filename })).code;
|
|
205
|
+
contents = strip_lang_tags(preprocessed);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (file.name.endsWith('.ts') && !file.name.endsWith('.d.ts')) {
|
|
210
|
+
contents = await transpile_ts(filename, contents);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
contents = resolve_lib_alias(file.name, contents, config);
|
|
214
|
+
write(dest, contents);
|
|
215
|
+
} else {
|
|
216
|
+
copy(filename, dest);
|
|
217
|
+
}
|
|
218
|
+
}
|