@sveltejs/kit 1.0.0-next.402 → 1.0.0-next.406

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 (99) hide show
  1. package/package.json +24 -25
  2. package/{dist → src}/cli.js +19 -18
  3. package/{dist/chunks/index3.js → src/core/adapt/builder.js} +52 -63
  4. package/src/core/adapt/index.js +19 -0
  5. package/src/core/config/index.js +86 -0
  6. package/{dist/chunks/index.js → src/core/config/options.js} +7 -194
  7. package/src/core/config/types.d.ts +1 -0
  8. package/src/core/constants.js +3 -0
  9. package/src/core/generate_manifest/index.js +99 -0
  10. package/src/core/prerender/crawl.js +194 -0
  11. package/src/core/prerender/prerender.js +378 -0
  12. package/src/core/prerender/queue.js +80 -0
  13. package/src/core/sync/create_manifest_data/index.js +492 -0
  14. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  15. package/src/core/sync/sync.js +59 -0
  16. package/src/core/sync/utils.js +97 -0
  17. package/src/core/sync/write_ambient.js +87 -0
  18. package/src/core/sync/write_client_manifest.js +82 -0
  19. package/src/core/sync/write_matchers.js +25 -0
  20. package/src/core/sync/write_root.js +88 -0
  21. package/{dist/chunks → src/core/sync}/write_tsconfig.js +24 -108
  22. package/src/core/sync/write_types.js +738 -0
  23. package/src/core/utils.js +58 -0
  24. package/{dist → src}/hooks.js +1 -3
  25. package/src/index/index.js +45 -0
  26. package/src/index/private.js +31 -0
  27. package/src/node/index.js +145 -0
  28. package/src/node/polyfills.js +40 -0
  29. package/src/packaging/index.js +218 -0
  30. package/src/packaging/types.d.ts +8 -0
  31. package/src/packaging/typescript.js +150 -0
  32. package/src/packaging/utils.js +138 -0
  33. package/{assets → src/runtime}/app/env.js +3 -5
  34. package/src/runtime/app/navigation.js +22 -0
  35. package/src/runtime/app/paths.js +1 -0
  36. package/{assets → src/runtime}/app/stores.js +6 -9
  37. package/src/runtime/client/ambient.d.ts +17 -0
  38. package/{assets/client/start.js → src/runtime/client/client.js} +302 -878
  39. package/src/runtime/client/fetcher.js +60 -0
  40. package/src/runtime/client/parse.js +36 -0
  41. package/{assets → src/runtime}/client/singletons.js +2 -4
  42. package/src/runtime/client/start.js +48 -0
  43. package/src/runtime/client/types.d.ts +106 -0
  44. package/src/runtime/client/utils.js +113 -0
  45. package/src/runtime/components/error.svelte +16 -0
  46. package/{assets → src/runtime}/components/layout.svelte +0 -0
  47. package/{assets → src/runtime}/env/dynamic/private.js +0 -0
  48. package/{assets → src/runtime}/env/dynamic/public.js +0 -0
  49. package/{assets → src/runtime}/env-private.js +2 -4
  50. package/{assets → src/runtime}/env-public.js +2 -4
  51. package/src/runtime/env.js +6 -0
  52. package/src/runtime/hash.js +16 -0
  53. package/{assets → src/runtime}/paths.js +3 -5
  54. package/src/runtime/server/endpoint.js +42 -0
  55. package/src/runtime/server/index.js +434 -0
  56. package/src/runtime/server/page/cookie.js +25 -0
  57. package/src/runtime/server/page/crypto.js +239 -0
  58. package/src/runtime/server/page/csp.js +249 -0
  59. package/src/runtime/server/page/fetch.js +265 -0
  60. package/src/runtime/server/page/index.js +423 -0
  61. package/src/runtime/server/page/load_data.js +94 -0
  62. package/src/runtime/server/page/render.js +357 -0
  63. package/src/runtime/server/page/respond_with_error.js +105 -0
  64. package/src/runtime/server/page/types.d.ts +44 -0
  65. package/src/runtime/server/utils.js +116 -0
  66. package/src/utils/error.js +22 -0
  67. package/src/utils/escape.js +104 -0
  68. package/{dist/chunks → src/utils}/filesystem.js +22 -24
  69. package/src/utils/http.js +55 -0
  70. package/src/utils/misc.js +1 -0
  71. package/src/utils/routing.js +107 -0
  72. package/src/utils/url.js +97 -0
  73. package/src/vite/build/build_server.js +333 -0
  74. package/src/vite/build/build_service_worker.js +90 -0
  75. package/src/vite/build/utils.js +152 -0
  76. package/src/vite/dev/index.js +565 -0
  77. package/src/vite/index.js +536 -0
  78. package/src/vite/preview/index.js +186 -0
  79. package/src/vite/types.d.ts +3 -0
  80. package/src/vite/utils.js +335 -0
  81. package/svelte-kit.js +1 -10
  82. package/types/ambient.d.ts +5 -12
  83. package/types/index.d.ts +91 -44
  84. package/types/internal.d.ts +50 -72
  85. package/types/private.d.ts +2 -1
  86. package/assets/app/navigation.js +0 -24
  87. package/assets/app/paths.js +0 -1
  88. package/assets/components/error.svelte +0 -29
  89. package/assets/env.js +0 -8
  90. package/assets/server/index.js +0 -3579
  91. package/dist/chunks/error.js +0 -12
  92. package/dist/chunks/index2.js +0 -15745
  93. package/dist/chunks/multipart-parser.js +0 -458
  94. package/dist/chunks/sync.js +0 -1366
  95. package/dist/chunks/utils.js +0 -66
  96. package/dist/node/polyfills.js +0 -17778
  97. package/dist/node.js +0 -348
  98. package/dist/prerender.js +0 -788
  99. package/dist/vite.js +0 -2520
@@ -0,0 +1,150 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { createRequire } from 'module';
4
+ import { posixify, mkdirp, rimraf, walk } from '../utils/filesystem.js';
5
+ import { resolve_lib_alias, write } from './utils.js';
6
+
7
+ /**
8
+ * @param {import('types').ValidatedConfig} config
9
+ * @param {string} cwd
10
+ * @param {import('./types').File[]} files
11
+ */
12
+ export async function emit_dts(config, cwd, files) {
13
+ const tmp = `${config.kit.outDir}/package/types`;
14
+ rimraf(tmp);
15
+ mkdirp(tmp);
16
+
17
+ const require = createRequire(import.meta.url);
18
+ const emit = await try_load_svelte2tsx();
19
+ await emit({
20
+ libRoot: config.kit.files.lib,
21
+ svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
22
+ declarationDir: path.relative(cwd, tmp)
23
+ });
24
+
25
+ const handwritten = new Set();
26
+ const excluded = new Set();
27
+
28
+ // remove excluded files, and files that conflict with hand-written .d.ts
29
+ for (const file of files) {
30
+ if (file.name.endsWith('.d.ts')) {
31
+ handwritten.add(file.name);
32
+ }
33
+
34
+ if (!file.is_included) {
35
+ excluded.add(file.base + '.d.ts');
36
+ excluded.add(file.base + '.d.mts');
37
+ excluded.add(file.base + '.d.cts');
38
+ }
39
+ }
40
+
41
+ // resolve $lib alias (TODO others), copy into package dir
42
+ for (const file of walk(tmp)) {
43
+ const normalized = posixify(file);
44
+
45
+ if (handwritten.has(normalized)) {
46
+ console.warn(`Using $lib/${normalized} instead of generated .d.ts file`);
47
+ }
48
+
49
+ // don't overwrite hand-written .d.ts files
50
+ if (excluded.has(normalized)) continue;
51
+
52
+ const source = fs.readFileSync(path.join(tmp, normalized), 'utf8');
53
+ write(
54
+ path.join(config.kit.package.dir, normalized),
55
+ resolve_lib_alias(normalized, source, config)
56
+ );
57
+ }
58
+ }
59
+
60
+ async function try_load_svelte2tsx() {
61
+ const svelte2tsx = await load();
62
+ const emit_dts = svelte2tsx.emitDts;
63
+ if (!emit_dts) {
64
+ throw new Error(
65
+ 'You need to install svelte2tsx >=0.4.1 if you want to generate type definitions'
66
+ );
67
+ }
68
+ return emit_dts;
69
+
70
+ async function load() {
71
+ try {
72
+ return await import('svelte2tsx');
73
+ } catch (e) {
74
+ throw new Error(
75
+ 'You need svelte2tsx and typescript if you want to generate type definitions. Install it through your package manager, or disable generation which is highly discouraged. See https://kit.svelte.dev/docs/packaging'
76
+ );
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @param {string} filename
83
+ * @param {string} source
84
+ */
85
+ export async function transpile_ts(filename, source) {
86
+ const ts = await try_load_ts();
87
+ return ts.transpileModule(source, {
88
+ compilerOptions: load_tsconfig(filename, ts),
89
+ fileName: filename
90
+ }).outputText;
91
+ }
92
+
93
+ async function try_load_ts() {
94
+ try {
95
+ return (await import('typescript')).default;
96
+ } catch (e) {
97
+ throw new Error(
98
+ 'You need to install TypeScript if you want to transpile TypeScript files and/or generate type definitions'
99
+ );
100
+ }
101
+ }
102
+
103
+ /**
104
+ * @param {string} filename
105
+ * @param {import('typescript')} ts
106
+ */
107
+ function load_tsconfig(filename, ts) {
108
+ let config_filename;
109
+
110
+ // ts.findConfigFile is broken (it will favour a distant tsconfig
111
+ // over a near jsconfig, and then only when you call it twice)
112
+ // so we implement it ourselves
113
+ let dir = filename;
114
+ while (dir !== (dir = path.dirname(dir))) {
115
+ const tsconfig = path.join(dir, 'tsconfig.json');
116
+ const jsconfig = path.join(dir, 'jsconfig.json');
117
+
118
+ if (fs.existsSync(tsconfig)) {
119
+ config_filename = tsconfig;
120
+ break;
121
+ }
122
+
123
+ if (fs.existsSync(jsconfig)) {
124
+ config_filename = jsconfig;
125
+ break;
126
+ }
127
+ }
128
+
129
+ if (!config_filename) {
130
+ throw new Error('Failed to locate tsconfig or jsconfig');
131
+ }
132
+
133
+ const { error, config } = ts.readConfigFile(config_filename, ts.sys.readFile);
134
+
135
+ if (error) {
136
+ throw new Error('Malformed tsconfig\n' + JSON.stringify(error, null, 2));
137
+ }
138
+
139
+ // Do this so TS will not search for initial files which might take a while
140
+ config.include = [];
141
+ config.files = [];
142
+ const { options } = ts.parseJsonConfigFileContent(
143
+ config,
144
+ ts.sys,
145
+ path.dirname(config_filename),
146
+ { sourceMap: false },
147
+ config_filename
148
+ );
149
+ return options;
150
+ }
@@ -0,0 +1,138 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { posixify, mkdirp, walk } from '../utils/filesystem.js';
4
+
5
+ /**
6
+ * Resolves the `$lib` alias.
7
+ *
8
+ * TODO: make this more generic to also handle other aliases the user could have defined via
9
+ * `kit.alias`. Also investigate how to do this in a more robust way (right now regex string
10
+ * replacement is used).
11
+ * For more discussion see https://github.com/sveltejs/kit/pull/2453
12
+ *
13
+ * @param {string} file Relative to the lib root
14
+ * @param {string} content
15
+ * @param {import('types').ValidatedConfig} config
16
+ * @returns {string}
17
+ */
18
+ export function resolve_lib_alias(file, content, config) {
19
+ /**
20
+ * @param {string} match
21
+ * @param {string} _
22
+ * @param {string} import_path
23
+ */
24
+ const replace_import_path = (match, _, import_path) => {
25
+ if (!import_path.startsWith('$lib/')) {
26
+ return match;
27
+ }
28
+
29
+ const full_path = path.join(config.kit.files.lib, file);
30
+ const full_import_path = path.join(config.kit.files.lib, import_path.slice('$lib/'.length));
31
+ let resolved = posixify(path.relative(path.dirname(full_path), full_import_path));
32
+ resolved = resolved.startsWith('.') ? resolved : './' + resolved;
33
+ return match.replace(import_path, resolved);
34
+ };
35
+ content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path);
36
+ content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path);
37
+ return content;
38
+ }
39
+
40
+ /**
41
+ * Strip out lang="X" or type="text/X" tags. Doing it here is only a temporary solution.
42
+ * See https://github.com/sveltejs/kit/issues/2450 for ideas for places where it's handled better.
43
+ *
44
+ * @param {string} content
45
+ */
46
+ export function strip_lang_tags(content) {
47
+ return content
48
+ .replace(/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2')
49
+ .replace(/(<!--[^]*?-->)|(<style[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2');
50
+ }
51
+
52
+ /**
53
+ * @param {string} file
54
+ * @param {Parameters<typeof fs.writeFileSync>[1]} contents
55
+ */
56
+ export function write(file, contents) {
57
+ mkdirp(path.dirname(file));
58
+ fs.writeFileSync(file, contents);
59
+ }
60
+
61
+ /**
62
+ * @param {import('types').ValidatedConfig} config
63
+ * @returns {import('./types').File[]}
64
+ */
65
+ export function scan(config) {
66
+ return walk(config.kit.files.lib).map((file) => analyze(config, file));
67
+ }
68
+
69
+ /**
70
+ * @param {import('types').ValidatedConfig} config
71
+ * @param {string} file
72
+ * @returns {import('./types').File}
73
+ */
74
+ export function analyze(config, file) {
75
+ const name = posixify(file);
76
+
77
+ const svelte_extension = config.extensions.find((ext) => name.endsWith(ext));
78
+
79
+ const base = svelte_extension ? name : name.slice(0, -path.extname(name).length);
80
+
81
+ const dest = svelte_extension
82
+ ? name.slice(0, -svelte_extension.length) + '.svelte'
83
+ : name.endsWith('.d.ts')
84
+ ? name
85
+ : name.endsWith('.ts')
86
+ ? name.slice(0, -3) + '.js'
87
+ : name;
88
+
89
+ return {
90
+ name,
91
+ dest,
92
+ base,
93
+ is_included: config.kit.package.files(name),
94
+ is_exported: config.kit.package.exports(name),
95
+ is_svelte: !!svelte_extension
96
+ };
97
+ }
98
+
99
+ /**
100
+ * @param {string} cwd
101
+ * @param {import('./types').File[]} files
102
+ */
103
+ export function generate_pkg(cwd, files) {
104
+ const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
105
+
106
+ delete pkg.scripts;
107
+ pkg.type = 'module';
108
+
109
+ pkg.exports = {
110
+ './package.json': './package.json',
111
+ ...pkg.exports
112
+ };
113
+
114
+ /** @type {Record<string, string>} */
115
+ const clashes = {};
116
+
117
+ for (const file of files) {
118
+ if (file.is_included && file.is_exported) {
119
+ const original = `$lib/${file.name}`;
120
+ const entry = `./${file.dest}`;
121
+ const key = entry.replace(/\/index\.js$|(\/[^/]+)\.js$/, '$1');
122
+
123
+ if (clashes[key]) {
124
+ throw new Error(
125
+ `Duplicate "${key}" export. Please remove or rename either ${clashes[key]} or ${original}`
126
+ );
127
+ }
128
+
129
+ if (!pkg.exports[key]) {
130
+ pkg.exports[key] = entry;
131
+ }
132
+
133
+ clashes[key] = original;
134
+ }
135
+ }
136
+
137
+ return pkg;
138
+ }
@@ -1,13 +1,11 @@
1
- export { prerendering } from '../env.js';
2
-
3
1
  /**
4
2
  * @type {import('$app/env').browser}
5
3
  */
6
- const browser = !import.meta.env.SSR;
4
+ export const browser = !import.meta.env.SSR;
7
5
 
8
6
  /**
9
7
  * @type {import('$app/env').dev}
10
8
  */
11
- const dev = __SVELTEKIT_DEV__;
9
+ export const dev = __SVELTEKIT_DEV__;
12
10
 
13
- export { browser, dev };
11
+ export { prerendering } from '../env.js';
@@ -0,0 +1,22 @@
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 prefetch = ssr ? guard('prefetch') : client.prefetch;
20
+ export const prefetchRoutes = ssr ? guard('prefetchRoutes') : client.prefetch_routes;
21
+ export const beforeNavigate = ssr ? () => {} : client.before_navigate;
22
+ export const afterNavigate = ssr ? () => {} : client.after_navigate;
@@ -0,0 +1 @@
1
+ export { base, assets } from '../paths.js';
@@ -1,10 +1,9 @@
1
1
  import { getContext } from 'svelte';
2
2
  import { browser } from './env.js';
3
- import '../env.js';
4
3
 
5
4
  // TODO remove this (for 1.0? after 1.0?)
6
5
  let warned = false;
7
- function stores() {
6
+ export function stores() {
8
7
  if (!warned) {
9
8
  console.error('stores() is deprecated; use getStores() instead');
10
9
  warned = true;
@@ -15,7 +14,7 @@ function stores() {
15
14
  /**
16
15
  * @type {import('$app/stores').getStores}
17
16
  */
18
- const getStores = () => {
17
+ export const getStores = () => {
19
18
  const stores = getContext('__svelte__');
20
19
 
21
20
  return {
@@ -39,7 +38,7 @@ const getStores = () => {
39
38
  };
40
39
 
41
40
  /** @type {typeof import('$app/stores').page} */
42
- const page = {
41
+ export const page = {
43
42
  /** @param {(value: any) => void} fn */
44
43
  subscribe(fn) {
45
44
  const store = getStores().page;
@@ -48,7 +47,7 @@ const page = {
48
47
  };
49
48
 
50
49
  /** @type {typeof import('$app/stores').navigating} */
51
- const navigating = {
50
+ export const navigating = {
52
51
  subscribe(fn) {
53
52
  const store = getStores().navigating;
54
53
  return store.subscribe(fn);
@@ -65,7 +64,7 @@ const throw_error = (verb) => {
65
64
  };
66
65
 
67
66
  /** @type {typeof import('$app/stores').session} */
68
- const session = {
67
+ export const session = {
69
68
  subscribe(fn) {
70
69
  const store = getStores().session;
71
70
 
@@ -81,7 +80,7 @@ const session = {
81
80
  };
82
81
 
83
82
  /** @type {typeof import('$app/stores').updated} */
84
- const updated = {
83
+ export const updated = {
85
84
  subscribe(fn) {
86
85
  const store = getStores().updated;
87
86
 
@@ -93,5 +92,3 @@ const updated = {
93
92
  },
94
93
  check: () => throw_error('check')
95
94
  };
96
-
97
- export { getStores, navigating, page, session, stores, updated };
@@ -0,0 +1,17 @@
1
+ declare module '__GENERATED__/client-manifest.js' {
2
+ import { CSRPageNodeLoader, ParamMatcher } from 'types';
3
+
4
+ /**
5
+ * A list of all the error/layout/page nodes used in the app
6
+ */
7
+ export const nodes: CSRPageNodeLoader[];
8
+
9
+ /**
10
+ * A map of `[routeId: string]: [errors, layouts, page]` tuples, which
11
+ * is parsed into an array of routes on startup. The numbers refer to the
12
+ * indices in `nodes`.
13
+ */
14
+ export const dictionary: Record<string, [number[], number[], number]>;
15
+
16
+ export const matchers: Record<string, ParamMatcher>;
17
+ }