@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
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import sirv from 'sirv';
|
|
4
|
+
import { pathToFileURL } from 'url';
|
|
5
|
+
import { getRequest, setResponse } from '../../node/index.js';
|
|
6
|
+
import { installPolyfills } from '../../node/polyfills.js';
|
|
7
|
+
import { SVELTE_KIT_ASSETS } from '../../core/constants.js';
|
|
8
|
+
import { loadEnv } from 'vite';
|
|
9
|
+
|
|
10
|
+
/** @typedef {import('http').IncomingMessage} Req */
|
|
11
|
+
/** @typedef {import('http').ServerResponse} Res */
|
|
12
|
+
/** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {{
|
|
16
|
+
* middlewares: import('connect').Server;
|
|
17
|
+
* httpServer: import('http').Server;
|
|
18
|
+
* }} vite
|
|
19
|
+
* @param {import('vite').ResolvedConfig} vite_config
|
|
20
|
+
* @param {import('types').ValidatedConfig} svelte_config
|
|
21
|
+
*/
|
|
22
|
+
export async function preview(vite, vite_config, svelte_config) {
|
|
23
|
+
installPolyfills();
|
|
24
|
+
|
|
25
|
+
const { paths } = svelte_config.kit;
|
|
26
|
+
const base = paths.base;
|
|
27
|
+
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
|
|
28
|
+
|
|
29
|
+
const protocol = vite_config.preview.https ? 'https' : 'http';
|
|
30
|
+
|
|
31
|
+
const etag = `"${Date.now()}"`;
|
|
32
|
+
|
|
33
|
+
const index_file = join(svelte_config.kit.outDir, 'output/server/index.js');
|
|
34
|
+
const manifest_file = join(svelte_config.kit.outDir, 'output/server/manifest.js');
|
|
35
|
+
|
|
36
|
+
/** @type {import('types').ServerModule} */
|
|
37
|
+
const { Server, override } = await import(pathToFileURL(index_file).href);
|
|
38
|
+
const { manifest } = await import(pathToFileURL(manifest_file).href);
|
|
39
|
+
|
|
40
|
+
override({
|
|
41
|
+
paths: { base, assets },
|
|
42
|
+
prerendering: false,
|
|
43
|
+
protocol,
|
|
44
|
+
read: (file) => fs.readFileSync(join(svelte_config.kit.files.assets, file))
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const server = new Server(manifest);
|
|
48
|
+
server.init({
|
|
49
|
+
env: loadEnv(vite_config.mode, process.cwd(), '')
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return () => {
|
|
53
|
+
// generated client assets and the contents of `static`
|
|
54
|
+
vite.middlewares.use(
|
|
55
|
+
scoped(
|
|
56
|
+
assets,
|
|
57
|
+
sirv(join(svelte_config.kit.outDir, 'output/client'), {
|
|
58
|
+
setHeaders: (res, pathname) => {
|
|
59
|
+
// only apply to immutable directory, not e.g. version.json
|
|
60
|
+
if (pathname.startsWith(`/${svelte_config.kit.appDir}/immutable`)) {
|
|
61
|
+
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
vite.middlewares.use((req, res, next) => {
|
|
69
|
+
const original_url = /** @type {string} */ (req.url);
|
|
70
|
+
const { pathname } = new URL(original_url, 'http://dummy');
|
|
71
|
+
|
|
72
|
+
if (pathname.startsWith(base)) {
|
|
73
|
+
next();
|
|
74
|
+
} else {
|
|
75
|
+
res.statusCode = 404;
|
|
76
|
+
res.end(`Not found (did you mean ${base + pathname}?)`);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// prerendered dependencies
|
|
81
|
+
vite.middlewares.use(
|
|
82
|
+
scoped(base, mutable(join(svelte_config.kit.outDir, 'output/prerendered/dependencies')))
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// prerendered pages (we can't just use sirv because we need to
|
|
86
|
+
// preserve the correct trailingSlash behaviour)
|
|
87
|
+
vite.middlewares.use(
|
|
88
|
+
scoped(base, (req, res, next) => {
|
|
89
|
+
let if_none_match_value = req.headers['if-none-match'];
|
|
90
|
+
|
|
91
|
+
if (if_none_match_value?.startsWith('W/"')) {
|
|
92
|
+
if_none_match_value = if_none_match_value.substring(2);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (if_none_match_value === etag) {
|
|
96
|
+
res.statusCode = 304;
|
|
97
|
+
res.end();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
|
|
102
|
+
|
|
103
|
+
// only treat this as a page if it doesn't include an extension
|
|
104
|
+
if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
|
|
105
|
+
const file = join(
|
|
106
|
+
svelte_config.kit.outDir,
|
|
107
|
+
'output/prerendered/pages' +
|
|
108
|
+
pathname +
|
|
109
|
+
(pathname.endsWith('/') ? 'index.html' : '.html')
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
if (fs.existsSync(file)) {
|
|
113
|
+
res.writeHead(200, {
|
|
114
|
+
'content-type': 'text/html',
|
|
115
|
+
etag
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
fs.createReadStream(file).pipe(res);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
next();
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// SSR
|
|
128
|
+
vite.middlewares.use(async (req, res) => {
|
|
129
|
+
const host = req.headers['host'];
|
|
130
|
+
|
|
131
|
+
let request;
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
request = await getRequest(`${protocol}://${host}`, req);
|
|
135
|
+
} catch (/** @type {any} */ err) {
|
|
136
|
+
res.statusCode = err.status || 400;
|
|
137
|
+
return res.end(err.reason || 'Invalid request body');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
setResponse(
|
|
141
|
+
res,
|
|
142
|
+
await server.respond(request, {
|
|
143
|
+
getClientAddress: () => {
|
|
144
|
+
const { remoteAddress } = req.socket;
|
|
145
|
+
if (remoteAddress) return remoteAddress;
|
|
146
|
+
throw new Error('Could not determine clientAddress');
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {string} dir
|
|
156
|
+
* @returns {Handler}
|
|
157
|
+
*/
|
|
158
|
+
const mutable = (dir) =>
|
|
159
|
+
fs.existsSync(dir)
|
|
160
|
+
? sirv(dir, {
|
|
161
|
+
etag: true,
|
|
162
|
+
maxAge: 0
|
|
163
|
+
})
|
|
164
|
+
: (_req, _res, next) => next();
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @param {string} scope
|
|
168
|
+
* @param {Handler} handler
|
|
169
|
+
* @returns {Handler}
|
|
170
|
+
*/
|
|
171
|
+
function scoped(scope, handler) {
|
|
172
|
+
if (scope === '') return handler;
|
|
173
|
+
|
|
174
|
+
return (req, res, next) => {
|
|
175
|
+
if (req.url?.startsWith(scope)) {
|
|
176
|
+
const original_url = req.url;
|
|
177
|
+
req.url = req.url.slice(scope.length);
|
|
178
|
+
handler(req, res, () => {
|
|
179
|
+
req.url = original_url;
|
|
180
|
+
next();
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
next();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { loadConfigFromFile, loadEnv, normalizePath } from 'vite';
|
|
4
|
+
import { runtime_directory } from '../core/utils.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('vite').ResolvedConfig} config
|
|
8
|
+
* @param {import('vite').ConfigEnv} config_env
|
|
9
|
+
* @return {Promise<import('vite').UserConfig>}
|
|
10
|
+
*/
|
|
11
|
+
export async function get_vite_config(config, config_env) {
|
|
12
|
+
const loaded = await loadConfigFromFile(
|
|
13
|
+
config_env,
|
|
14
|
+
config.configFile,
|
|
15
|
+
undefined,
|
|
16
|
+
config.logLevel
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (!loaded) {
|
|
20
|
+
throw new Error('Could not load Vite config');
|
|
21
|
+
}
|
|
22
|
+
return { ...loaded.config, mode: config_env.mode };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {...import('vite').UserConfig} configs
|
|
27
|
+
* @returns {import('vite').UserConfig}
|
|
28
|
+
*/
|
|
29
|
+
export function merge_vite_configs(...configs) {
|
|
30
|
+
return deep_merge(
|
|
31
|
+
...configs.map((config) => ({
|
|
32
|
+
...config,
|
|
33
|
+
resolve: {
|
|
34
|
+
...config.resolve,
|
|
35
|
+
alias: normalize_alias(config.resolve?.alias || {})
|
|
36
|
+
}
|
|
37
|
+
}))
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Takes zero or more objects and returns a new object that has all the values
|
|
43
|
+
* deeply merged together. None of the original objects will be mutated at any
|
|
44
|
+
* level, and the returned object will have no references to the original
|
|
45
|
+
* objects at any depth. If there's a conflict the last one wins, except for
|
|
46
|
+
* arrays which will be combined.
|
|
47
|
+
* @param {...Object} objects
|
|
48
|
+
* @returns {Record<string, any>} the merged object
|
|
49
|
+
*/
|
|
50
|
+
export function deep_merge(...objects) {
|
|
51
|
+
const result = {};
|
|
52
|
+
/** @type {string[]} */
|
|
53
|
+
objects.forEach((o) => merge_into(result, o));
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* normalize kit.vite.resolve.alias as an array
|
|
59
|
+
* @param {import('vite').AliasOptions} o
|
|
60
|
+
* @returns {import('vite').Alias[]}
|
|
61
|
+
*/
|
|
62
|
+
function normalize_alias(o) {
|
|
63
|
+
if (Array.isArray(o)) return o;
|
|
64
|
+
return Object.entries(o).map(([find, replacement]) => ({ find, replacement }));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Merges b into a, recursively, mutating a.
|
|
69
|
+
* @param {Record<string, any>} a
|
|
70
|
+
* @param {Record<string, any>} b
|
|
71
|
+
*/
|
|
72
|
+
function merge_into(a, b) {
|
|
73
|
+
/**
|
|
74
|
+
* Checks for "plain old Javascript object", typically made as an object
|
|
75
|
+
* literal. Excludes Arrays and built-in types like Buffer.
|
|
76
|
+
* @param {any} x
|
|
77
|
+
*/
|
|
78
|
+
const is_plain_object = (x) => typeof x === 'object' && x.constructor === Object;
|
|
79
|
+
|
|
80
|
+
for (const prop in b) {
|
|
81
|
+
if (is_plain_object(b[prop])) {
|
|
82
|
+
if (!is_plain_object(a[prop])) {
|
|
83
|
+
a[prop] = {};
|
|
84
|
+
}
|
|
85
|
+
merge_into(a[prop], b[prop]);
|
|
86
|
+
} else if (Array.isArray(b[prop])) {
|
|
87
|
+
if (!Array.isArray(a[prop])) {
|
|
88
|
+
a[prop] = [];
|
|
89
|
+
}
|
|
90
|
+
a[prop].push(...b[prop]);
|
|
91
|
+
} else {
|
|
92
|
+
a[prop] = b[prop];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Transforms kit.alias to a valid vite.resolve.alias array.
|
|
99
|
+
* Related to tsconfig path alias creation.
|
|
100
|
+
*
|
|
101
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
102
|
+
* */
|
|
103
|
+
export function get_aliases(config) {
|
|
104
|
+
/** @type {import('vite').Alias[]} */
|
|
105
|
+
const alias = [
|
|
106
|
+
{ find: '__GENERATED__', replacement: path.posix.join(config.outDir, 'generated') },
|
|
107
|
+
{ find: '$app', replacement: `${runtime_directory}/app` },
|
|
108
|
+
// For now, we handle `$lib` specially here rather than make it a default value for
|
|
109
|
+
// `config.kit.alias` since it has special meaning for packaging, etc.
|
|
110
|
+
{ find: '$lib', replacement: config.files.lib }
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
for (let [key, value] of Object.entries(config.alias)) {
|
|
114
|
+
if (value.endsWith('/*')) {
|
|
115
|
+
value = value.slice(0, -2);
|
|
116
|
+
}
|
|
117
|
+
if (key.endsWith('/*')) {
|
|
118
|
+
// Doing just `{ find: key.slice(0, -2) ,..}` would mean `import .. from "key"` would also be matched, which we don't want
|
|
119
|
+
alias.push({
|
|
120
|
+
find: new RegExp(`^${key.slice(0, -2)}\\/(.+)$`),
|
|
121
|
+
replacement: `${path.resolve(value)}/$1`
|
|
122
|
+
});
|
|
123
|
+
} else if (key + '/*' in config.alias) {
|
|
124
|
+
// key and key/* both exist -> the replacement for key needs to happen _only_ on import .. from "key"
|
|
125
|
+
alias.push({ find: new RegExp(`^${key}$`), replacement: path.resolve(value) });
|
|
126
|
+
} else {
|
|
127
|
+
alias.push({ find: key, replacement: path.resolve(value) });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
alias.push(
|
|
132
|
+
{
|
|
133
|
+
find: '$env/static/public',
|
|
134
|
+
replacement: path.posix.join(config.outDir, 'runtime/env/static/public.js')
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
find: '$env/static/private',
|
|
138
|
+
replacement: path.posix.join(config.outDir, 'runtime/env/static/private.js')
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
find: '$env',
|
|
142
|
+
replacement: `${runtime_directory}/env`
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return alias;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
|
|
151
|
+
* @param {string} entry
|
|
152
|
+
* @returns {string|null}
|
|
153
|
+
*/
|
|
154
|
+
export function resolve_entry(entry) {
|
|
155
|
+
if (fs.existsSync(entry)) {
|
|
156
|
+
const stats = fs.statSync(entry);
|
|
157
|
+
if (stats.isDirectory()) {
|
|
158
|
+
return resolve_entry(path.join(entry, 'index'));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return entry;
|
|
162
|
+
} else {
|
|
163
|
+
const dir = path.dirname(entry);
|
|
164
|
+
|
|
165
|
+
if (fs.existsSync(dir)) {
|
|
166
|
+
const base = path.basename(entry);
|
|
167
|
+
const files = fs.readdirSync(dir);
|
|
168
|
+
|
|
169
|
+
const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
|
|
170
|
+
|
|
171
|
+
if (found) return path.join(dir, found);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param {string} str
|
|
180
|
+
* @param {number} times
|
|
181
|
+
*/
|
|
182
|
+
function repeat(str, times) {
|
|
183
|
+
return new Array(times + 1).join(str);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Create a formatted error for an illegal import.
|
|
188
|
+
* @param {Array<{name: string, dynamic: boolean}>} stack
|
|
189
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
190
|
+
*/
|
|
191
|
+
function format_illegal_import_chain(stack, out_dir) {
|
|
192
|
+
const app = path.join(out_dir, 'runtime/env');
|
|
193
|
+
|
|
194
|
+
stack = stack.map((file) => {
|
|
195
|
+
if (file.name.startsWith(app)) return { ...file, name: file.name.replace(app, '$env') };
|
|
196
|
+
return { ...file, name: path.relative(process.cwd(), file.name) };
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const pyramid = stack
|
|
200
|
+
.map(
|
|
201
|
+
(file, i) =>
|
|
202
|
+
`${repeat(' ', i * 2)}- ${file.name} ${
|
|
203
|
+
file.dynamic ? '(imported by parent dynamically)' : ''
|
|
204
|
+
}`
|
|
205
|
+
)
|
|
206
|
+
.join('\n');
|
|
207
|
+
|
|
208
|
+
return `Cannot import ${stack.at(-1)?.name} into client-side code:\n${pyramid}`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Load environment variables from process.env and .env files
|
|
213
|
+
* @param {string} mode
|
|
214
|
+
* @param {string} prefix
|
|
215
|
+
*/
|
|
216
|
+
export function get_env(mode, prefix) {
|
|
217
|
+
const entries = Object.entries(loadEnv(mode, process.cwd(), ''));
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
public: Object.fromEntries(entries.filter(([k]) => k.startsWith(prefix))),
|
|
221
|
+
private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(prefix)))
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
227
|
+
* @param {import('rollup').ModuleInfo} node
|
|
228
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
229
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
230
|
+
*/
|
|
231
|
+
export function prevent_illegal_rollup_imports(node_getter, node, illegal_imports, out_dir) {
|
|
232
|
+
const chain = find_illegal_rollup_imports(node_getter, node, false, illegal_imports);
|
|
233
|
+
if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
238
|
+
* @param {import('rollup').ModuleInfo} node
|
|
239
|
+
* @param {boolean} dynamic
|
|
240
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
241
|
+
* @param {Set<string>} seen
|
|
242
|
+
* @returns {Array<import('types').ImportNode> | null}
|
|
243
|
+
*/
|
|
244
|
+
const find_illegal_rollup_imports = (
|
|
245
|
+
node_getter,
|
|
246
|
+
node,
|
|
247
|
+
dynamic,
|
|
248
|
+
illegal_imports,
|
|
249
|
+
seen = new Set()
|
|
250
|
+
) => {
|
|
251
|
+
const name = normalizePath(node.id);
|
|
252
|
+
if (seen.has(name)) return null;
|
|
253
|
+
seen.add(name);
|
|
254
|
+
|
|
255
|
+
if (illegal_imports.has(name)) {
|
|
256
|
+
return [{ name, dynamic }];
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
for (const id of node.importedIds) {
|
|
260
|
+
const child = node_getter(id);
|
|
261
|
+
const chain =
|
|
262
|
+
child && find_illegal_rollup_imports(node_getter, child, false, illegal_imports, seen);
|
|
263
|
+
if (chain) return [{ name, dynamic }, ...chain];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (const id of node.dynamicallyImportedIds) {
|
|
267
|
+
const child = node_getter(id);
|
|
268
|
+
const chain =
|
|
269
|
+
child && find_illegal_rollup_imports(node_getter, child, true, illegal_imports, seen);
|
|
270
|
+
if (chain) return [{ name, dynamic }, ...chain];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return null;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Vite does some weird things with import trees in dev
|
|
278
|
+
* for example, a Tailwind app.css will appear to import
|
|
279
|
+
* every file in the project. This isn't a problem for
|
|
280
|
+
* Rollup during build.
|
|
281
|
+
* @param {Iterable<string>} config_module_types
|
|
282
|
+
*/
|
|
283
|
+
const get_module_types = (config_module_types) => {
|
|
284
|
+
return new Set([
|
|
285
|
+
'.ts',
|
|
286
|
+
'.js',
|
|
287
|
+
'.svelte',
|
|
288
|
+
'.mts',
|
|
289
|
+
'.mjs',
|
|
290
|
+
'.cts',
|
|
291
|
+
'.cjs',
|
|
292
|
+
'.svelte.md',
|
|
293
|
+
'.svx',
|
|
294
|
+
'.md',
|
|
295
|
+
...config_module_types
|
|
296
|
+
]);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Throw an error if a private module is imported from a client-side node.
|
|
301
|
+
* @param {import('vite').ModuleNode} node
|
|
302
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
303
|
+
* @param {Iterable<string>} module_types File extensions to analyze in addition to the defaults: `.ts`, `.js`, etc.
|
|
304
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
305
|
+
*/
|
|
306
|
+
export function prevent_illegal_vite_imports(node, illegal_imports, module_types, out_dir) {
|
|
307
|
+
const chain = find_illegal_vite_imports(node, illegal_imports, get_module_types(module_types));
|
|
308
|
+
if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @param {import('vite').ModuleNode} node
|
|
313
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
314
|
+
* @param {Set<string>} module_types File extensions to analyze: `.ts`, `.js`, etc.
|
|
315
|
+
* @param {Set<string>} seen
|
|
316
|
+
* @returns {Array<import('types').ImportNode> | null}
|
|
317
|
+
*/
|
|
318
|
+
function find_illegal_vite_imports(node, illegal_imports, module_types, seen = new Set()) {
|
|
319
|
+
if (!node.id) return null; // TODO when does this happen?
|
|
320
|
+
const name = normalizePath(node.id);
|
|
321
|
+
|
|
322
|
+
if (seen.has(name) || !module_types.has(path.extname(name))) return null;
|
|
323
|
+
seen.add(name);
|
|
324
|
+
|
|
325
|
+
if (name && illegal_imports.has(name)) {
|
|
326
|
+
return [{ name, dynamic: false }];
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
for (const child of node.importedModules) {
|
|
330
|
+
const chain = child && find_illegal_vite_imports(child, illegal_imports, module_types, seen);
|
|
331
|
+
if (chain) return [{ name, dynamic: false }, ...chain];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return null;
|
|
335
|
+
}
|
package/svelte-kit.js
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
|
|
5
|
-
// in our own CI, and when deploying directly from this monorepo,
|
|
6
|
-
// the `dist` directory will not exist yet
|
|
7
|
-
if (fs.existsSync(fileURLToPath(new URL('./dist', import.meta.url)))) {
|
|
8
|
-
import('./dist/cli.js');
|
|
9
|
-
} else {
|
|
10
|
-
console.error('Run "pnpm build" and try running this command again');
|
|
11
|
-
}
|
|
2
|
+
import './src/cli.js';
|
package/types/ambient.d.ts
CHANGED
|
@@ -14,8 +14,6 @@
|
|
|
14
14
|
* interface PublicEnv {}
|
|
15
15
|
*
|
|
16
16
|
* interface Session {}
|
|
17
|
-
*
|
|
18
|
-
* interface Stuff {}
|
|
19
17
|
* }
|
|
20
18
|
* ```
|
|
21
19
|
*
|
|
@@ -57,24 +55,19 @@ declare namespace App {
|
|
|
57
55
|
export interface Platform {}
|
|
58
56
|
|
|
59
57
|
/**
|
|
60
|
-
* The interface that defines the dynamic environment variables exported from
|
|
58
|
+
* The interface that defines the dynamic environment variables exported from `$env/dynamic/private`.
|
|
61
59
|
*/
|
|
62
60
|
export interface PrivateEnv extends Record<string, string> {}
|
|
63
61
|
|
|
64
62
|
/**
|
|
65
|
-
* The interface that defines the dynamic environment variables exported from
|
|
63
|
+
* The interface that defines the dynamic environment variables exported from `$env/dynamic/public`.
|
|
66
64
|
*/
|
|
67
65
|
export interface PublicEnv extends Record<string, string> {}
|
|
68
66
|
|
|
69
67
|
/**
|
|
70
|
-
* The interface that defines `session`, both as an argument to [`load`](https://kit.svelte.dev/docs/
|
|
68
|
+
* The interface that defines `session`, both as an argument to [`load`](https://kit.svelte.dev/docs/load) functions and the value of the [session store](https://kit.svelte.dev/docs/modules#$app-stores).
|
|
71
69
|
*/
|
|
72
70
|
export interface Session {}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The interface that defines `stuff`, as input or output to [`load`](https://kit.svelte.dev/docs/loading) or as the value of the `stuff` property of the [page store](https://kit.svelte.dev/docs/modules#$app-stores).
|
|
76
|
-
*/
|
|
77
|
-
export interface Stuff {}
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
/**
|
|
@@ -165,10 +158,10 @@ declare module '$app/navigation' {
|
|
|
165
158
|
opts?: { replaceState?: boolean; noscroll?: boolean; keepfocus?: boolean; state?: any }
|
|
166
159
|
): Promise<void>;
|
|
167
160
|
/**
|
|
168
|
-
* Causes any `load` functions belonging to the currently active page to re-run if they `fetch` the resource in question, or re-fetches data from a page endpoint if the invalidated resource is the page itself. Returns a `Promise` that resolves when the page is subsequently updated.
|
|
161
|
+
* Causes any `load` functions belonging to the currently active page to re-run if they `fetch` the resource in question, or re-fetches data from a page endpoint if the invalidated resource is the page itself. If no argument is given, all resources will be invalidated. Returns a `Promise` that resolves when the page is subsequently updated.
|
|
169
162
|
* @param dependency The invalidated resource
|
|
170
163
|
*/
|
|
171
|
-
export function invalidate(dependency
|
|
164
|
+
export function invalidate(dependency?: string | ((href: string) => boolean)): Promise<void>;
|
|
172
165
|
/**
|
|
173
166
|
* Programmatically prefetches the given page, which means
|
|
174
167
|
* 1. ensuring that the code for the page is loaded, and
|