@sveltejs/kit 1.0.0-next.47 → 1.0.0-next.472

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