@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430
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 +95 -63
- package/src/cli.js +112 -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 +5 -0
- package/src/core/env.js +97 -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 +506 -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 +44 -0
- package/src/core/sync/write_ambient.js +27 -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 +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types.js +775 -0
- package/src/core/utils.js +70 -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/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 +102 -0
- package/src/runtime/client/ambient.d.ts +17 -0
- package/src/runtime/client/client.js +1289 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +46 -0
- package/src/runtime/client/types.d.ts +105 -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 +58 -0
- package/src/runtime/server/index.js +448 -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 +266 -0
- package/src/runtime/server/page/index.js +416 -0
- package/src/runtime/server/page/load_data.js +135 -0
- package/src/runtime/server/page/render.js +362 -0
- package/src/runtime/server/page/respond_with_error.js +94 -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 +108 -0
- package/src/utils/url.js +97 -0
- package/src/vite/build/build_server.js +337 -0
- package/src/vite/build/build_service_worker.js +90 -0
- package/src/vite/build/utils.js +160 -0
- package/src/vite/dev/index.js +551 -0
- package/src/vite/index.js +574 -0
- package/src/vite/preview/index.js +186 -0
- package/src/vite/types.d.ts +3 -0
- package/src/vite/utils.js +345 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +357 -0
- package/types/index.d.ts +343 -0
- package/types/internal.d.ts +308 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -431
- 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/.DS_Store +0 -0
- package/dist/chunks/index.js +0 -3521
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -538
- 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/ssr.js +0 -2581
|
@@ -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,345 @@
|
|
|
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
|
+
import { posixify } from '../utils/filesystem.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {import('vite').ResolvedConfig} config
|
|
9
|
+
* @param {import('vite').ConfigEnv} config_env
|
|
10
|
+
* @return {Promise<import('vite').UserConfig>}
|
|
11
|
+
*/
|
|
12
|
+
export async function get_vite_config(config, config_env) {
|
|
13
|
+
const loaded = await loadConfigFromFile(
|
|
14
|
+
config_env,
|
|
15
|
+
config.configFile,
|
|
16
|
+
undefined,
|
|
17
|
+
config.logLevel
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
if (!loaded) {
|
|
21
|
+
throw new Error('Could not load Vite config');
|
|
22
|
+
}
|
|
23
|
+
return { ...loaded.config, mode: config_env.mode };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {...import('vite').UserConfig} configs
|
|
28
|
+
* @returns {import('vite').UserConfig}
|
|
29
|
+
*/
|
|
30
|
+
export function merge_vite_configs(...configs) {
|
|
31
|
+
return deep_merge(
|
|
32
|
+
...configs.map((config) => ({
|
|
33
|
+
...config,
|
|
34
|
+
resolve: {
|
|
35
|
+
...config.resolve,
|
|
36
|
+
alias: normalize_alias(config.resolve?.alias || {})
|
|
37
|
+
}
|
|
38
|
+
}))
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Takes zero or more objects and returns a new object that has all the values
|
|
44
|
+
* deeply merged together. None of the original objects will be mutated at any
|
|
45
|
+
* level, and the returned object will have no references to the original
|
|
46
|
+
* objects at any depth. If there's a conflict the last one wins, except for
|
|
47
|
+
* arrays which will be combined.
|
|
48
|
+
* @param {...Object} objects
|
|
49
|
+
* @returns {Record<string, any>} the merged object
|
|
50
|
+
*/
|
|
51
|
+
export function deep_merge(...objects) {
|
|
52
|
+
const result = {};
|
|
53
|
+
/** @type {string[]} */
|
|
54
|
+
objects.forEach((o) => merge_into(result, o));
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* normalize kit.vite.resolve.alias as an array
|
|
60
|
+
* @param {import('vite').AliasOptions} o
|
|
61
|
+
* @returns {import('vite').Alias[]}
|
|
62
|
+
*/
|
|
63
|
+
function normalize_alias(o) {
|
|
64
|
+
if (Array.isArray(o)) return o;
|
|
65
|
+
return Object.entries(o).map(([find, replacement]) => ({ find, replacement }));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Merges b into a, recursively, mutating a.
|
|
70
|
+
* @param {Record<string, any>} a
|
|
71
|
+
* @param {Record<string, any>} b
|
|
72
|
+
*/
|
|
73
|
+
function merge_into(a, b) {
|
|
74
|
+
/**
|
|
75
|
+
* Checks for "plain old Javascript object", typically made as an object
|
|
76
|
+
* literal. Excludes Arrays and built-in types like Buffer.
|
|
77
|
+
* @param {any} x
|
|
78
|
+
*/
|
|
79
|
+
const is_plain_object = (x) => typeof x === 'object' && x.constructor === Object;
|
|
80
|
+
|
|
81
|
+
for (const prop in b) {
|
|
82
|
+
if (is_plain_object(b[prop])) {
|
|
83
|
+
if (!is_plain_object(a[prop])) {
|
|
84
|
+
a[prop] = {};
|
|
85
|
+
}
|
|
86
|
+
merge_into(a[prop], b[prop]);
|
|
87
|
+
} else if (Array.isArray(b[prop])) {
|
|
88
|
+
if (!Array.isArray(a[prop])) {
|
|
89
|
+
a[prop] = [];
|
|
90
|
+
}
|
|
91
|
+
a[prop].push(...b[prop]);
|
|
92
|
+
} else {
|
|
93
|
+
a[prop] = b[prop];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Transforms kit.alias to a valid vite.resolve.alias array.
|
|
100
|
+
* Related to tsconfig path alias creation.
|
|
101
|
+
*
|
|
102
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
103
|
+
* */
|
|
104
|
+
export function get_aliases(config) {
|
|
105
|
+
/** @type {import('vite').Alias[]} */
|
|
106
|
+
const alias = [
|
|
107
|
+
{ find: '__GENERATED__', replacement: path.posix.join(config.outDir, 'generated') },
|
|
108
|
+
{ find: '$app', replacement: `${runtime_directory}/app` },
|
|
109
|
+
// For now, we handle `$lib` specially here rather than make it a default value for
|
|
110
|
+
// `config.kit.alias` since it has special meaning for packaging, etc.
|
|
111
|
+
{ find: '$lib', replacement: config.files.lib }
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
for (let [key, value] of Object.entries(config.alias)) {
|
|
115
|
+
value = posixify(value);
|
|
116
|
+
if (value.endsWith('/*')) {
|
|
117
|
+
value = value.slice(0, -2);
|
|
118
|
+
}
|
|
119
|
+
if (key.endsWith('/*')) {
|
|
120
|
+
// Doing just `{ find: key.slice(0, -2) ,..}` would mean `import .. from "key"` would also be matched, which we don't want
|
|
121
|
+
alias.push({
|
|
122
|
+
find: new RegExp(`^${escape_for_regexp(key.slice(0, -2))}\\/(.+)$`),
|
|
123
|
+
replacement: `${path.resolve(value)}/$1`
|
|
124
|
+
});
|
|
125
|
+
} else if (key + '/*' in config.alias) {
|
|
126
|
+
// key and key/* both exist -> the replacement for key needs to happen _only_ on import .. from "key"
|
|
127
|
+
alias.push({
|
|
128
|
+
find: new RegExp(`^${escape_for_regexp(key)}$`),
|
|
129
|
+
replacement: path.resolve(value)
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
alias.push({ find: key, replacement: path.resolve(value) });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return alias;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {string} str
|
|
141
|
+
*/
|
|
142
|
+
function escape_for_regexp(str) {
|
|
143
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, (match) => '\\' + match);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
|
|
148
|
+
* @param {string} entry
|
|
149
|
+
* @returns {string|null}
|
|
150
|
+
*/
|
|
151
|
+
export function resolve_entry(entry) {
|
|
152
|
+
if (fs.existsSync(entry)) {
|
|
153
|
+
const stats = fs.statSync(entry);
|
|
154
|
+
if (stats.isDirectory()) {
|
|
155
|
+
return resolve_entry(path.join(entry, 'index'));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return entry;
|
|
159
|
+
} else {
|
|
160
|
+
const dir = path.dirname(entry);
|
|
161
|
+
|
|
162
|
+
if (fs.existsSync(dir)) {
|
|
163
|
+
const base = path.basename(entry);
|
|
164
|
+
const files = fs.readdirSync(dir);
|
|
165
|
+
|
|
166
|
+
const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
|
|
167
|
+
|
|
168
|
+
if (found) return path.join(dir, found);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {string} str
|
|
177
|
+
* @param {number} times
|
|
178
|
+
*/
|
|
179
|
+
function repeat(str, times) {
|
|
180
|
+
return new Array(times + 1).join(str);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Create a formatted error for an illegal import.
|
|
185
|
+
* @param {Array<{name: string, dynamic: boolean}>} stack
|
|
186
|
+
*/
|
|
187
|
+
function format_illegal_import_chain(stack) {
|
|
188
|
+
const dev_virtual_prefix = '/@id/__x00__';
|
|
189
|
+
const prod_virtual_prefix = '\0';
|
|
190
|
+
|
|
191
|
+
stack = stack.map((file) => {
|
|
192
|
+
if (file.name.startsWith(dev_virtual_prefix)) {
|
|
193
|
+
return { ...file, name: file.name.replace(dev_virtual_prefix, '') };
|
|
194
|
+
}
|
|
195
|
+
if (file.name.startsWith(prod_virtual_prefix)) {
|
|
196
|
+
return { ...file, name: file.name.replace(prod_virtual_prefix, '') };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return { ...file, name: path.relative(process.cwd(), file.name) };
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const pyramid = stack
|
|
203
|
+
.map(
|
|
204
|
+
(file, i) =>
|
|
205
|
+
`${repeat(' ', i * 2)}- ${file.name} ${
|
|
206
|
+
file.dynamic ? '(imported by parent dynamically)' : ''
|
|
207
|
+
}`
|
|
208
|
+
)
|
|
209
|
+
.join('\n');
|
|
210
|
+
|
|
211
|
+
return `Cannot import ${stack.at(-1)?.name} into client-side code:\n${pyramid}`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Load environment variables from process.env and .env files
|
|
216
|
+
* @param {import('types').ValidatedKitConfig['env']} env_config
|
|
217
|
+
* @param {string} mode
|
|
218
|
+
*/
|
|
219
|
+
export function get_env(env_config, mode) {
|
|
220
|
+
const entries = Object.entries(loadEnv(mode, env_config.dir, ''));
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
public: Object.fromEntries(entries.filter(([k]) => k.startsWith(env_config.publicPrefix))),
|
|
224
|
+
private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(env_config.publicPrefix)))
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
230
|
+
* @param {import('rollup').ModuleInfo} node
|
|
231
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
232
|
+
*/
|
|
233
|
+
export function prevent_illegal_rollup_imports(node_getter, node, illegal_imports) {
|
|
234
|
+
const chain = find_illegal_rollup_imports(node_getter, node, false, illegal_imports);
|
|
235
|
+
if (chain) throw new Error(format_illegal_import_chain(chain));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const query_pattern = /\?.*$/s;
|
|
239
|
+
|
|
240
|
+
/** @param {string} path */
|
|
241
|
+
function remove_query_from_path(path) {
|
|
242
|
+
return path.replace(query_pattern, '');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
247
|
+
* @param {import('rollup').ModuleInfo} node
|
|
248
|
+
* @param {boolean} dynamic
|
|
249
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
250
|
+
* @param {Set<string>} seen
|
|
251
|
+
* @returns {Array<import('types').ImportNode> | null}
|
|
252
|
+
*/
|
|
253
|
+
const find_illegal_rollup_imports = (
|
|
254
|
+
node_getter,
|
|
255
|
+
node,
|
|
256
|
+
dynamic,
|
|
257
|
+
illegal_imports,
|
|
258
|
+
seen = new Set()
|
|
259
|
+
) => {
|
|
260
|
+
const name = remove_query_from_path(normalizePath(node.id));
|
|
261
|
+
if (seen.has(name)) return null;
|
|
262
|
+
seen.add(name);
|
|
263
|
+
|
|
264
|
+
if (illegal_imports.has(name)) {
|
|
265
|
+
return [{ name, dynamic }];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
for (const id of node.importedIds) {
|
|
269
|
+
const child = node_getter(id);
|
|
270
|
+
const chain =
|
|
271
|
+
child && find_illegal_rollup_imports(node_getter, child, false, illegal_imports, seen);
|
|
272
|
+
if (chain) return [{ name, dynamic }, ...chain];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
for (const id of node.dynamicallyImportedIds) {
|
|
276
|
+
const child = node_getter(id);
|
|
277
|
+
const chain =
|
|
278
|
+
child && find_illegal_rollup_imports(node_getter, child, true, illegal_imports, seen);
|
|
279
|
+
if (chain) return [{ name, dynamic }, ...chain];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return null;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Vite does some weird things with import trees in dev
|
|
287
|
+
* for example, a Tailwind app.css will appear to import
|
|
288
|
+
* every file in the project. This isn't a problem for
|
|
289
|
+
* Rollup during build.
|
|
290
|
+
* @param {Iterable<string>} config_module_types
|
|
291
|
+
*/
|
|
292
|
+
const get_module_types = (config_module_types) => {
|
|
293
|
+
return new Set([
|
|
294
|
+
'.ts',
|
|
295
|
+
'.js',
|
|
296
|
+
'.svelte',
|
|
297
|
+
'.mts',
|
|
298
|
+
'.mjs',
|
|
299
|
+
'.cts',
|
|
300
|
+
'.cjs',
|
|
301
|
+
'.svelte.md',
|
|
302
|
+
'.svx',
|
|
303
|
+
'.md',
|
|
304
|
+
...config_module_types
|
|
305
|
+
]);
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Throw an error if a private module is imported from a client-side node.
|
|
310
|
+
* @param {import('vite').ModuleNode} node
|
|
311
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
312
|
+
* @param {Iterable<string>} module_types File extensions to analyze in addition to the defaults: `.ts`, `.js`, etc.
|
|
313
|
+
*/
|
|
314
|
+
export function prevent_illegal_vite_imports(node, illegal_imports, module_types) {
|
|
315
|
+
const chain = find_illegal_vite_imports(node, illegal_imports, get_module_types(module_types));
|
|
316
|
+
if (chain) throw new Error(format_illegal_import_chain(chain));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* @param {import('vite').ModuleNode} node
|
|
321
|
+
* @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
|
|
322
|
+
* @param {Set<string>} module_types File extensions to analyze: `.ts`, `.js`, etc.
|
|
323
|
+
* @param {Set<string>} seen
|
|
324
|
+
* @returns {Array<import('types').ImportNode> | null}
|
|
325
|
+
*/
|
|
326
|
+
function find_illegal_vite_imports(node, illegal_imports, module_types, seen = new Set()) {
|
|
327
|
+
if (!node.id) return null; // TODO when does this happen?
|
|
328
|
+
const name = remove_query_from_path(normalizePath(node.id));
|
|
329
|
+
|
|
330
|
+
if (path.extname(name) !== '' && (seen.has(name) || !module_types.has(path.extname(name)))) {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
seen.add(name);
|
|
334
|
+
|
|
335
|
+
if (name && illegal_imports.has(name)) {
|
|
336
|
+
return [{ name, dynamic: false }];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
for (const child of node.importedModules) {
|
|
340
|
+
const chain = child && find_illegal_vite_imports(child, illegal_imports, module_types, seen);
|
|
341
|
+
if (chain) return [{ name, dynamic: false }, ...chain];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return null;
|
|
345
|
+
}
|
package/svelte-kit.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import './
|
|
2
|
+
import './src/cli.js';
|