@sveltejs/kit 1.0.0-next.392 → 1.0.0-next.395
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/assets/client/start.js +9 -2
- package/assets/env/dynamic/private.js +1 -0
- package/assets/env/dynamic/public.js +1 -0
- package/assets/env-private.js +9 -0
- package/assets/env-public.js +9 -0
- package/assets/server/index.js +4 -1
- package/dist/chunks/error.js +1 -671
- package/dist/chunks/filesystem.js +110 -0
- package/dist/chunks/index.js +574 -15643
- package/dist/chunks/index2.js +15705 -177
- package/dist/chunks/index3.js +218 -0
- package/dist/chunks/sync.js +377 -70
- package/dist/chunks/utils.js +66 -0
- package/dist/chunks/write_tsconfig.js +64 -121
- package/dist/cli.js +8 -6
- package/dist/prerender.js +788 -0
- package/dist/vite.js +194 -971
- package/package.json +1 -1
- package/types/ambient.d.ts +58 -12
- package/types/index.d.ts +8 -0
- package/types/internal.d.ts +8 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { $ } from './index.js';
|
|
2
|
+
import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
|
|
3
|
+
import { g as generate_manifest } from '../vite.js';
|
|
4
|
+
import 'fs';
|
|
5
|
+
import 'path';
|
|
6
|
+
import 'url';
|
|
7
|
+
import 'node:child_process';
|
|
8
|
+
import 'node:fs';
|
|
9
|
+
import 'node:path';
|
|
10
|
+
import '@sveltejs/vite-plugin-svelte';
|
|
11
|
+
import 'vite';
|
|
12
|
+
import './sync.js';
|
|
13
|
+
import './utils.js';
|
|
14
|
+
import './write_tsconfig.js';
|
|
15
|
+
import 'querystring';
|
|
16
|
+
import '../node.js';
|
|
17
|
+
import '../node/polyfills.js';
|
|
18
|
+
import 'assert';
|
|
19
|
+
import 'net';
|
|
20
|
+
import 'http';
|
|
21
|
+
import 'stream';
|
|
22
|
+
import 'buffer';
|
|
23
|
+
import 'util';
|
|
24
|
+
import 'stream/web';
|
|
25
|
+
import 'perf_hooks';
|
|
26
|
+
import 'util/types';
|
|
27
|
+
import 'events';
|
|
28
|
+
import 'tls';
|
|
29
|
+
import 'async_hooks';
|
|
30
|
+
import 'console';
|
|
31
|
+
import 'zlib';
|
|
32
|
+
import 'node:http';
|
|
33
|
+
import 'node:https';
|
|
34
|
+
import 'node:zlib';
|
|
35
|
+
import 'node:stream';
|
|
36
|
+
import 'node:buffer';
|
|
37
|
+
import 'node:util';
|
|
38
|
+
import 'node:url';
|
|
39
|
+
import 'node:net';
|
|
40
|
+
import 'crypto';
|
|
41
|
+
import './error.js';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates the Builder which is passed to adapters for building the application.
|
|
45
|
+
* @param {{
|
|
46
|
+
* config: import('types').ValidatedConfig;
|
|
47
|
+
* build_data: import('types').BuildData;
|
|
48
|
+
* prerendered: import('types').Prerendered;
|
|
49
|
+
* log: import('types').Logger;
|
|
50
|
+
* }} opts
|
|
51
|
+
* @returns {import('types').Builder}
|
|
52
|
+
*/
|
|
53
|
+
function create_builder({ config, build_data, prerendered, log }) {
|
|
54
|
+
/** @type {Set<string>} */
|
|
55
|
+
const prerendered_paths = new Set(prerendered.paths);
|
|
56
|
+
|
|
57
|
+
/** @param {import('types').RouteData} route */
|
|
58
|
+
// TODO routes should come pre-filtered
|
|
59
|
+
function not_prerendered(route) {
|
|
60
|
+
if (route.type === 'page' && route.path) {
|
|
61
|
+
return !prerendered_paths.has(route.path) && !prerendered_paths.has(route.path + '/');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
log,
|
|
69
|
+
rimraf,
|
|
70
|
+
mkdirp,
|
|
71
|
+
copy,
|
|
72
|
+
|
|
73
|
+
config,
|
|
74
|
+
prerendered,
|
|
75
|
+
|
|
76
|
+
async createEntries(fn) {
|
|
77
|
+
const { routes } = build_data.manifest_data;
|
|
78
|
+
|
|
79
|
+
/** @type {import('types').RouteDefinition[]} */
|
|
80
|
+
const facades = routes.map((route) => ({
|
|
81
|
+
id: route.id,
|
|
82
|
+
type: route.type,
|
|
83
|
+
segments: route.id.split('/').map((segment) => ({
|
|
84
|
+
dynamic: segment.includes('['),
|
|
85
|
+
rest: segment.includes('[...'),
|
|
86
|
+
content: segment
|
|
87
|
+
})),
|
|
88
|
+
pattern: route.pattern,
|
|
89
|
+
methods: route.type === 'page' ? ['GET'] : build_data.server.methods[route.file]
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
const seen = new Set();
|
|
93
|
+
|
|
94
|
+
for (let i = 0; i < routes.length; i += 1) {
|
|
95
|
+
const route = routes[i];
|
|
96
|
+
const { id, filter, complete } = fn(facades[i]);
|
|
97
|
+
|
|
98
|
+
if (seen.has(id)) continue;
|
|
99
|
+
seen.add(id);
|
|
100
|
+
|
|
101
|
+
const group = [route];
|
|
102
|
+
|
|
103
|
+
// figure out which lower priority routes should be considered fallbacks
|
|
104
|
+
for (let j = i + 1; j < routes.length; j += 1) {
|
|
105
|
+
if (filter(facades[j])) {
|
|
106
|
+
group.push(routes[j]);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const filtered = new Set(group.filter(not_prerendered));
|
|
111
|
+
|
|
112
|
+
// heuristic: if /foo/[bar] is included, /foo/[bar].json should
|
|
113
|
+
// also be included, since the page likely needs the endpoint
|
|
114
|
+
filtered.forEach((route) => {
|
|
115
|
+
if (route.type === 'page') {
|
|
116
|
+
const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
|
|
117
|
+
|
|
118
|
+
if (endpoint) {
|
|
119
|
+
filtered.add(endpoint);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (filtered.size > 0) {
|
|
125
|
+
await complete({
|
|
126
|
+
generateManifest: ({ relativePath, format }) =>
|
|
127
|
+
generate_manifest({
|
|
128
|
+
build_data,
|
|
129
|
+
relative_path: relativePath,
|
|
130
|
+
routes: Array.from(filtered),
|
|
131
|
+
format
|
|
132
|
+
})
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
generateManifest: ({ relativePath, format }) => {
|
|
139
|
+
return generate_manifest({
|
|
140
|
+
build_data,
|
|
141
|
+
relative_path: relativePath,
|
|
142
|
+
routes: build_data.manifest_data.routes.filter(not_prerendered),
|
|
143
|
+
format
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
getBuildDirectory(name) {
|
|
148
|
+
return `${config.kit.outDir}/${name}`;
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
getClientDirectory() {
|
|
152
|
+
return `${config.kit.outDir}/output/client`;
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
getServerDirectory() {
|
|
156
|
+
return `${config.kit.outDir}/output/server`;
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
getStaticDirectory() {
|
|
160
|
+
return config.kit.files.assets;
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
writeClient(dest) {
|
|
164
|
+
return [...copy(`${config.kit.outDir}/output/client`, dest)];
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
writePrerendered(dest, { fallback } = {}) {
|
|
168
|
+
const source = `${config.kit.outDir}/output/prerendered`;
|
|
169
|
+
const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
|
|
170
|
+
|
|
171
|
+
if (fallback) {
|
|
172
|
+
files.push(fallback);
|
|
173
|
+
copy(`${source}/fallback.html`, `${dest}/${fallback}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return files;
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
writeServer(dest) {
|
|
180
|
+
return copy(`${config.kit.outDir}/output/server`, dest);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
// TODO remove these methods for 1.0
|
|
184
|
+
// @ts-expect-error
|
|
185
|
+
writeStatic() {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`writeStatic has been removed. Please ensure you are using the latest version of ${
|
|
188
|
+
config.kit.adapter.name || 'your adapter'
|
|
189
|
+
}`
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
async prerender() {
|
|
194
|
+
throw new Error(
|
|
195
|
+
'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @param {import('types').ValidatedConfig} config
|
|
203
|
+
* @param {import('types').BuildData} build_data
|
|
204
|
+
* @param {import('types').Prerendered} prerendered
|
|
205
|
+
* @param {{ log: import('types').Logger }} opts
|
|
206
|
+
*/
|
|
207
|
+
async function adapt(config, build_data, prerendered, { log }) {
|
|
208
|
+
const { name, adapt } = config.kit.adapter;
|
|
209
|
+
|
|
210
|
+
console.log($.bold().cyan(`\n> Using ${name}`));
|
|
211
|
+
|
|
212
|
+
const builder = create_builder({ config, build_data, prerendered, log });
|
|
213
|
+
await adapt(builder);
|
|
214
|
+
|
|
215
|
+
log.success('done');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export { adapt };
|
package/dist/chunks/sync.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import path__default from 'path';
|
|
2
2
|
import fs__default from 'fs';
|
|
3
|
-
import {
|
|
3
|
+
import { g as get_runtime_directory } from './utils.js';
|
|
4
|
+
import { p as posixify, c as copy, r as rimraf } from './filesystem.js';
|
|
4
5
|
import { fileURLToPath } from 'url';
|
|
5
|
-
import {
|
|
6
|
+
import { w as write_if_changed, t as trim, a as write, v as valid_identifier, r as reserved, b as write_tsconfig } from './write_tsconfig.js';
|
|
7
|
+
import { $ } from './index.js';
|
|
8
|
+
import { loadEnv, loadConfigFromFile } from 'vite';
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
11
|
* @param typeMap [Object] Map of MIME type -> Array[extensions]
|
|
@@ -107,67 +110,6 @@ var other = {"application/prs.cww":["cww"],"application/vnd.1000minds.decision-m
|
|
|
107
110
|
let Mime = Mime_1;
|
|
108
111
|
var mime = new Mime(standard, other);
|
|
109
112
|
|
|
110
|
-
/**
|
|
111
|
-
* Get the prefix for the `runtime` directory, for use with import declarations
|
|
112
|
-
* @param {import('types').ValidatedKitConfig} config
|
|
113
|
-
*/
|
|
114
|
-
function get_runtime_prefix(config) {
|
|
115
|
-
{
|
|
116
|
-
return posixify_path(path__default.join(config.outDir, 'runtime'));
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Get the resolved path of the `runtime` directory
|
|
122
|
-
* @param {import('types').ValidatedKitConfig} config
|
|
123
|
-
*/
|
|
124
|
-
function get_runtime_directory(config) {
|
|
125
|
-
{
|
|
126
|
-
return path__default.join(config.outDir, 'runtime');
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** @param {string} str */
|
|
131
|
-
function posixify_path(str) {
|
|
132
|
-
const parsed = path__default.parse(str);
|
|
133
|
-
return `/${parsed.dir.slice(parsed.root.length).split(path__default.sep).join('/')}/${parsed.base}`;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function noop() {}
|
|
137
|
-
|
|
138
|
-
/** @param {{ verbose: boolean }} opts */
|
|
139
|
-
function logger({ verbose }) {
|
|
140
|
-
/** @type {import('types').Logger} */
|
|
141
|
-
const log = (msg) => console.log(msg.replace(/^/gm, ' '));
|
|
142
|
-
|
|
143
|
-
/** @param {string} msg */
|
|
144
|
-
const err = (msg) => console.error(msg.replace(/^/gm, ' '));
|
|
145
|
-
|
|
146
|
-
log.success = (msg) => log($.green(`✔ ${msg}`));
|
|
147
|
-
log.error = (msg) => err($.bold().red(msg));
|
|
148
|
-
log.warn = (msg) => log($.bold().yellow(msg));
|
|
149
|
-
|
|
150
|
-
log.minor = verbose ? (msg) => log($.grey(msg)) : noop;
|
|
151
|
-
log.info = verbose ? log : noop;
|
|
152
|
-
|
|
153
|
-
return log;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** @param {import('types').ManifestData} manifest_data */
|
|
157
|
-
function get_mime_lookup(manifest_data) {
|
|
158
|
-
/** @type {Record<string, string>} */
|
|
159
|
-
const mime = {};
|
|
160
|
-
|
|
161
|
-
manifest_data.assets.forEach((asset) => {
|
|
162
|
-
if (asset.type) {
|
|
163
|
-
const ext = path__default.extname(asset.file);
|
|
164
|
-
mime[ext] = asset.type;
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
return mime;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
113
|
const param_pattern = /^(\.\.\.)?(\w+)(?:=(\w+))?$/;
|
|
172
114
|
|
|
173
115
|
/** @param {string} id */
|
|
@@ -970,13 +912,374 @@ function write_types(config, manifest_data) {
|
|
|
970
912
|
});
|
|
971
913
|
}
|
|
972
914
|
|
|
973
|
-
/**
|
|
974
|
-
|
|
915
|
+
/**
|
|
916
|
+
* @param {import('vite').ResolvedConfig} config
|
|
917
|
+
* @param {import('vite').ConfigEnv} config_env
|
|
918
|
+
* @return {Promise<import('vite').UserConfig>}
|
|
919
|
+
*/
|
|
920
|
+
async function get_vite_config(config, config_env) {
|
|
921
|
+
const loaded = await loadConfigFromFile(
|
|
922
|
+
config_env,
|
|
923
|
+
config.configFile,
|
|
924
|
+
undefined,
|
|
925
|
+
config.logLevel
|
|
926
|
+
);
|
|
927
|
+
|
|
928
|
+
if (!loaded) {
|
|
929
|
+
throw new Error('Could not load Vite config');
|
|
930
|
+
}
|
|
931
|
+
return { ...loaded.config, mode: config_env.mode };
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* @param {...import('vite').UserConfig} configs
|
|
936
|
+
* @returns {import('vite').UserConfig}
|
|
937
|
+
*/
|
|
938
|
+
function merge_vite_configs(...configs) {
|
|
939
|
+
return deep_merge(
|
|
940
|
+
...configs.map((config) => ({
|
|
941
|
+
...config,
|
|
942
|
+
resolve: {
|
|
943
|
+
...config.resolve,
|
|
944
|
+
alias: normalize_alias(config.resolve?.alias || {})
|
|
945
|
+
}
|
|
946
|
+
}))
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* Takes zero or more objects and returns a new object that has all the values
|
|
952
|
+
* deeply merged together. None of the original objects will be mutated at any
|
|
953
|
+
* level, and the returned object will have no references to the original
|
|
954
|
+
* objects at any depth. If there's a conflict the last one wins, except for
|
|
955
|
+
* arrays which will be combined.
|
|
956
|
+
* @param {...Object} objects
|
|
957
|
+
* @returns {Record<string, any>} the merged object
|
|
958
|
+
*/
|
|
959
|
+
function deep_merge(...objects) {
|
|
960
|
+
const result = {};
|
|
961
|
+
/** @type {string[]} */
|
|
962
|
+
objects.forEach((o) => merge_into(result, o));
|
|
963
|
+
return result;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* normalize kit.vite.resolve.alias as an array
|
|
968
|
+
* @param {import('vite').AliasOptions} o
|
|
969
|
+
* @returns {import('vite').Alias[]}
|
|
970
|
+
*/
|
|
971
|
+
function normalize_alias(o) {
|
|
972
|
+
if (Array.isArray(o)) return o;
|
|
973
|
+
return Object.entries(o).map(([find, replacement]) => ({ find, replacement }));
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* Merges b into a, recursively, mutating a.
|
|
978
|
+
* @param {Record<string, any>} a
|
|
979
|
+
* @param {Record<string, any>} b
|
|
980
|
+
*/
|
|
981
|
+
function merge_into(a, b) {
|
|
982
|
+
/**
|
|
983
|
+
* Checks for "plain old Javascript object", typically made as an object
|
|
984
|
+
* literal. Excludes Arrays and built-in types like Buffer.
|
|
985
|
+
* @param {any} x
|
|
986
|
+
*/
|
|
987
|
+
const is_plain_object = (x) => typeof x === 'object' && x.constructor === Object;
|
|
988
|
+
|
|
989
|
+
for (const prop in b) {
|
|
990
|
+
if (is_plain_object(b[prop])) {
|
|
991
|
+
if (!is_plain_object(a[prop])) {
|
|
992
|
+
a[prop] = {};
|
|
993
|
+
}
|
|
994
|
+
merge_into(a[prop], b[prop]);
|
|
995
|
+
} else if (Array.isArray(b[prop])) {
|
|
996
|
+
if (!Array.isArray(a[prop])) {
|
|
997
|
+
a[prop] = [];
|
|
998
|
+
}
|
|
999
|
+
a[prop].push(...b[prop]);
|
|
1000
|
+
} else {
|
|
1001
|
+
a[prop] = b[prop];
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/** @param {import('types').ValidatedKitConfig} config */
|
|
1007
|
+
function get_aliases(config) {
|
|
1008
|
+
/** @type {Record<string, string>} */
|
|
1009
|
+
const alias = {
|
|
1010
|
+
__GENERATED__: path__default.posix.join(config.outDir, 'generated'),
|
|
1011
|
+
|
|
1012
|
+
$app: `${get_runtime_directory(config)}/app`,
|
|
1013
|
+
|
|
1014
|
+
// For now, we handle `$lib` specially here rather than make it a default value for
|
|
1015
|
+
// `config.kit.alias` since it has special meaning for packaging, etc.
|
|
1016
|
+
$lib: config.files.lib
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
alias['$env'] = `${get_runtime_directory(config)}/env`;
|
|
1020
|
+
|
|
1021
|
+
for (const [key, value] of Object.entries(config.alias)) {
|
|
1022
|
+
alias[key] = path__default.resolve(value);
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
return alias;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
/**
|
|
1029
|
+
* Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
|
|
1030
|
+
* @param {string} entry
|
|
1031
|
+
* @returns {string|null}
|
|
1032
|
+
*/
|
|
1033
|
+
function resolve_entry(entry) {
|
|
1034
|
+
if (fs__default.existsSync(entry)) {
|
|
1035
|
+
const stats = fs__default.statSync(entry);
|
|
1036
|
+
if (stats.isDirectory()) {
|
|
1037
|
+
return resolve_entry(path__default.join(entry, 'index'));
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
return entry;
|
|
1041
|
+
} else {
|
|
1042
|
+
const dir = path__default.dirname(entry);
|
|
1043
|
+
|
|
1044
|
+
if (fs__default.existsSync(dir)) {
|
|
1045
|
+
const base = path__default.basename(entry);
|
|
1046
|
+
const files = fs__default.readdirSync(dir);
|
|
1047
|
+
|
|
1048
|
+
const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
|
|
1049
|
+
|
|
1050
|
+
if (found) return path__default.join(dir, found);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
return null;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* @param {string} str
|
|
1059
|
+
* @param {number} times
|
|
1060
|
+
*/
|
|
1061
|
+
function repeat(str, times) {
|
|
1062
|
+
return new Array(times + 1).join(str);
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* Create a formatted error for an illegal import.
|
|
1067
|
+
* @param {Array<{name: string, dynamic: boolean}>} stack
|
|
1068
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
1069
|
+
*/
|
|
1070
|
+
function format_illegal_import_chain(stack, out_dir) {
|
|
1071
|
+
const app = path__default.join(out_dir, 'runtime/env');
|
|
1072
|
+
|
|
1073
|
+
stack = stack.map((file) => {
|
|
1074
|
+
if (file.name.startsWith(app)) return { ...file, name: file.name.replace(app, '$env') };
|
|
1075
|
+
return { ...file, name: path__default.relative(process.cwd(), file.name) };
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
const pyramid = stack
|
|
1079
|
+
.map(
|
|
1080
|
+
(file, i) =>
|
|
1081
|
+
`${repeat(' ', i * 2)}- ${file.name} ${
|
|
1082
|
+
file.dynamic ? '(imported by parent dynamically)' : ''
|
|
1083
|
+
}`
|
|
1084
|
+
)
|
|
1085
|
+
.join('\n');
|
|
1086
|
+
|
|
1087
|
+
return `Cannot import ${stack.at(-1)?.name} into client-side code:\n${pyramid}`;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* Load environment variables from process.env and .env files
|
|
1092
|
+
* @param {string} mode
|
|
1093
|
+
* @param {string} prefix
|
|
1094
|
+
*/
|
|
1095
|
+
function get_env(mode, prefix) {
|
|
1096
|
+
const entries = Object.entries(loadEnv(mode, process.cwd(), ''));
|
|
1097
|
+
|
|
1098
|
+
return {
|
|
1099
|
+
public: Object.fromEntries(entries.filter(([k]) => k.startsWith(prefix))),
|
|
1100
|
+
private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(prefix)))
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
1106
|
+
* @param {import('rollup').ModuleInfo} node
|
|
1107
|
+
* @param {Set<string>} illegal_imports
|
|
1108
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
1109
|
+
*/
|
|
1110
|
+
function prevent_illegal_rollup_imports(node_getter, node, illegal_imports, out_dir) {
|
|
1111
|
+
const chain = find_illegal_rollup_imports(node_getter, node, false, illegal_imports);
|
|
1112
|
+
if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
|
|
1117
|
+
* @param {import('rollup').ModuleInfo} node
|
|
1118
|
+
* @param {boolean} dynamic
|
|
1119
|
+
* @param {Set<string>} illegal_imports
|
|
1120
|
+
* @param {Set<string>} seen
|
|
1121
|
+
* @returns {Array<import('types').ImportNode> | undefined}
|
|
1122
|
+
*/
|
|
1123
|
+
const find_illegal_rollup_imports = (
|
|
1124
|
+
node_getter,
|
|
1125
|
+
node,
|
|
1126
|
+
dynamic,
|
|
1127
|
+
illegal_imports,
|
|
1128
|
+
seen = new Set()
|
|
1129
|
+
) => {
|
|
1130
|
+
if (seen.has(node.id)) return;
|
|
1131
|
+
seen.add(node.id);
|
|
1132
|
+
|
|
1133
|
+
if (illegal_imports.has(node.id)) {
|
|
1134
|
+
return [{ name: node.id, dynamic }];
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
for (const id of node.importedIds) {
|
|
1138
|
+
const child = node_getter(id);
|
|
1139
|
+
const chain =
|
|
1140
|
+
child && find_illegal_rollup_imports(node_getter, child, false, illegal_imports, seen);
|
|
1141
|
+
if (chain) return [{ name: node.id, dynamic }, ...chain];
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
for (const id of node.dynamicallyImportedIds) {
|
|
1145
|
+
const child = node_getter(id);
|
|
1146
|
+
const chain =
|
|
1147
|
+
child && find_illegal_rollup_imports(node_getter, child, true, illegal_imports, seen);
|
|
1148
|
+
if (chain) return [{ name: node.id, dynamic }, ...chain];
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* Throw an error if a private module is imported from a client-side node.
|
|
1154
|
+
* @param {import('vite').ModuleNode} node
|
|
1155
|
+
* @param {Set<string>} illegal_imports
|
|
1156
|
+
* @param {string} out_dir The directory specified by config.kit.outDir
|
|
1157
|
+
*/
|
|
1158
|
+
function prevent_illegal_vite_imports(node, illegal_imports, out_dir) {
|
|
1159
|
+
const chain = find_illegal_vite_imports(node, illegal_imports);
|
|
1160
|
+
if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* @param {import('vite').ModuleNode} node
|
|
1165
|
+
* @param {Set<string>} illegal_imports
|
|
1166
|
+
* @param {Set<string>} seen
|
|
1167
|
+
* @returns {Array<import('types').ImportNode> | null}
|
|
1168
|
+
*/
|
|
1169
|
+
function find_illegal_vite_imports(node, illegal_imports, seen = new Set()) {
|
|
1170
|
+
if (!node.id) return null; // TODO when does this happen?
|
|
1171
|
+
|
|
1172
|
+
if (seen.has(node.id)) return null;
|
|
1173
|
+
seen.add(node.id);
|
|
1174
|
+
|
|
1175
|
+
if (node.id && illegal_imports.has(node.id)) {
|
|
1176
|
+
return [{ name: node.id, dynamic: false }];
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
for (const child of node.importedModules) {
|
|
1180
|
+
const chain = child && find_illegal_vite_imports(child, illegal_imports, seen);
|
|
1181
|
+
if (chain) return [{ name: node.id, dynamic: false }, ...chain];
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
return null;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
const autogen_comment = '// this file is generated — do not edit it\n';
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Writes the existing environment variables in process.env to
|
|
1191
|
+
* $env/static/private and $env/static/public
|
|
1192
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
1193
|
+
* @param {string} mode The Vite mode
|
|
1194
|
+
*/
|
|
1195
|
+
function write_env(config, mode) {
|
|
1196
|
+
const env = get_env(mode, config.env.publicPrefix);
|
|
1197
|
+
|
|
1198
|
+
// TODO when testing src, `$app` points at `src/runtime/app`... will
|
|
1199
|
+
// probably need to fiddle with aliases
|
|
1200
|
+
write_if_changed(
|
|
1201
|
+
path__default.join(config.outDir, 'runtime/env/static/public.js'),
|
|
1202
|
+
create_module('$env/static/public', env.public)
|
|
1203
|
+
);
|
|
1204
|
+
|
|
1205
|
+
write_if_changed(
|
|
1206
|
+
path__default.join(config.outDir, 'runtime/env/static/private.js'),
|
|
1207
|
+
create_module('$env/static/private', env.private)
|
|
1208
|
+
);
|
|
1209
|
+
|
|
1210
|
+
write_if_changed(
|
|
1211
|
+
path__default.join(config.outDir, 'ambient.d.ts'),
|
|
1212
|
+
autogen_comment +
|
|
1213
|
+
create_types('$env/static/public', env.public) +
|
|
1214
|
+
'\n\n' +
|
|
1215
|
+
create_types('$env/static/private', env.private)
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* @param {string} id
|
|
1221
|
+
* @param {Record<string, string>} env
|
|
1222
|
+
* @returns {string}
|
|
1223
|
+
*/
|
|
1224
|
+
function create_module(id, env) {
|
|
1225
|
+
/** @type {string[]} */
|
|
1226
|
+
const declarations = [];
|
|
1227
|
+
|
|
1228
|
+
for (const key in env) {
|
|
1229
|
+
const warning = !valid_identifier.test(key)
|
|
1230
|
+
? 'not a valid identifier'
|
|
1231
|
+
: reserved.has(key)
|
|
1232
|
+
? 'a reserved word'
|
|
1233
|
+
: null;
|
|
1234
|
+
|
|
1235
|
+
if (warning) {
|
|
1236
|
+
console.error(
|
|
1237
|
+
$
|
|
1238
|
+
.bold()
|
|
1239
|
+
.yellow(`Omitting environment variable "${key}" from ${id} as it is ${warning}`)
|
|
1240
|
+
);
|
|
1241
|
+
continue;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
const comment = `/** @type {import('${id}'}').${key}} */`;
|
|
1245
|
+
const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;
|
|
1246
|
+
|
|
1247
|
+
declarations.push(`${comment}\n${declaration}`);
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
return autogen_comment + declarations.join('\n\n');
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/**
|
|
1254
|
+
* @param {string} id
|
|
1255
|
+
* @param {Record<string, string>} env
|
|
1256
|
+
* @returns {string}
|
|
1257
|
+
*/
|
|
1258
|
+
function create_types(id, env) {
|
|
1259
|
+
const declarations = Object.keys(env)
|
|
1260
|
+
.filter((k) => valid_identifier.test(k))
|
|
1261
|
+
.map((k) => `\texport const ${k}: string;`)
|
|
1262
|
+
.join('\n');
|
|
1263
|
+
|
|
1264
|
+
return `declare module '${id}' {\n${declarations}\n}`;
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
/**
|
|
1268
|
+
* Initialize SvelteKit's generated files.
|
|
1269
|
+
* @param {import('types').ValidatedConfig} config
|
|
1270
|
+
* @param {string} mode
|
|
1271
|
+
*/
|
|
1272
|
+
function init(config, mode) {
|
|
975
1273
|
copy_assets(path__default.join(config.kit.outDir, 'runtime'));
|
|
1274
|
+
|
|
976
1275
|
write_tsconfig(config.kit);
|
|
1276
|
+
write_env(config.kit, mode);
|
|
977
1277
|
}
|
|
978
1278
|
|
|
979
|
-
/**
|
|
1279
|
+
/**
|
|
1280
|
+
* Update SvelteKit's generated files.
|
|
1281
|
+
* @param {import('types').ValidatedConfig} config
|
|
1282
|
+
*/
|
|
980
1283
|
function update(config) {
|
|
981
1284
|
const manifest_data = create_manifest_data({ config });
|
|
982
1285
|
|
|
@@ -991,9 +1294,13 @@ function update(config) {
|
|
|
991
1294
|
return { manifest_data };
|
|
992
1295
|
}
|
|
993
1296
|
|
|
994
|
-
/**
|
|
995
|
-
|
|
996
|
-
|
|
1297
|
+
/**
|
|
1298
|
+
* Run sync.init and sync.update in series, returning the result from sync.update.
|
|
1299
|
+
* @param {import('types').ValidatedConfig} config
|
|
1300
|
+
* @param {string} mode The Vite mode
|
|
1301
|
+
*/
|
|
1302
|
+
function all(config, mode) {
|
|
1303
|
+
init(config, mode);
|
|
997
1304
|
return update(config);
|
|
998
1305
|
}
|
|
999
1306
|
|
|
@@ -1004,4 +1311,4 @@ var sync = /*#__PURE__*/Object.freeze({
|
|
|
1004
1311
|
all: all
|
|
1005
1312
|
});
|
|
1006
1313
|
|
|
1007
|
-
export {
|
|
1314
|
+
export { get_vite_config as a, get_env as b, parse_route_id as c, all as d, prevent_illegal_rollup_imports as e, sync as f, get_aliases as g, init as i, merge_vite_configs as m, prevent_illegal_vite_imports as p, resolve_entry as r, s, update as u };
|