@sveltejs/kit 1.0.0-next.44 → 1.0.0-next.442

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 (153) hide show
  1. package/README.md +12 -9
  2. package/package.json +94 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +223 -0
  5. package/src/core/adapt/index.js +19 -0
  6. package/src/core/config/index.js +86 -0
  7. package/src/core/config/options.js +488 -0
  8. package/src/core/config/types.d.ts +1 -0
  9. package/src/core/constants.js +5 -0
  10. package/src/core/env.js +97 -0
  11. package/src/core/generate_manifest/index.js +78 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +380 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +452 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +33 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +92 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +91 -0
  23. package/src/core/sync/write_tsconfig.js +195 -0
  24. package/src/core/sync/write_types/index.js +580 -0
  25. package/src/core/sync/write_types/test/layout/+layout.js +5 -0
  26. package/src/core/sync/write_types/test/layout/+layout.server.js +5 -0
  27. package/src/core/sync/write_types/test/layout/+layout.svelte +0 -0
  28. package/src/core/sync/write_types/test/layout/+page.js +5 -0
  29. package/src/core/sync/write_types/test/layout/+page.server.js +5 -0
  30. package/src/core/sync/write_types/test/layout/+page.svelte +0 -0
  31. package/src/core/sync/write_types/test/layout/_expected/$types.d.ts +67 -0
  32. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.server.js +5 -0
  33. package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.svelte +0 -0
  34. package/src/core/sync/write_types/test/layout-advanced/(main)/+page.js +5 -0
  35. package/src/core/sync/write_types/test/layout-advanced/(main)/+page@.svelte +0 -0
  36. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.js +5 -0
  37. package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.svelte +0 -0
  38. package/src/core/sync/write_types/test/layout-advanced/+layout.js +5 -0
  39. package/src/core/sync/write_types/test/layout-advanced/+layout.svelte +0 -0
  40. package/src/core/sync/write_types/test/layout-advanced/_expected/$types.d.ts +32 -0
  41. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/$types.d.ts +42 -0
  42. package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts +33 -0
  43. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.js +5 -0
  44. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.server.js +5 -0
  45. package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.svelte +0 -0
  46. package/src/core/sync/write_types/test/simple-page-server-and-shared/_expected/$types.d.ts +44 -0
  47. package/src/core/sync/write_types/test/simple-page-server-only/+page.server.js +5 -0
  48. package/src/core/sync/write_types/test/simple-page-server-only/+page.svelte +0 -0
  49. package/src/core/sync/write_types/test/simple-page-server-only/_expected/$types.d.ts +30 -0
  50. package/src/core/sync/write_types/test/simple-page-shared-only/+page.js +5 -0
  51. package/src/core/sync/write_types/test/simple-page-shared-only/+page.svelte +0 -0
  52. package/src/core/sync/write_types/test/simple-page-shared-only/_expected/$types.d.ts +33 -0
  53. package/src/core/sync/write_types/test/slugs/+layout.js +1 -0
  54. package/src/core/sync/write_types/test/slugs/+layout.svelte +1 -0
  55. package/src/core/sync/write_types/test/slugs/[...rest]/+page.js +3 -0
  56. package/src/core/sync/write_types/test/slugs/[...rest]/+page.svelte +0 -0
  57. package/src/core/sync/write_types/test/slugs/[slug]/+page.js +3 -0
  58. package/src/core/sync/write_types/test/slugs/[slug]/+page.svelte +0 -0
  59. package/src/core/sync/write_types/test/slugs/_expected/$types.d.ts +32 -0
  60. package/src/core/sync/write_types/test/slugs/_expected/[...rest]/$types.d.ts +29 -0
  61. package/src/core/sync/write_types/test/slugs/_expected/[slug]/$types.d.ts +29 -0
  62. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.js +1 -0
  63. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.svelte +1 -0
  64. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/$types.d.ts +30 -0
  65. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/$types.d.ts +32 -0
  66. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[...rest]/$types.d.ts +37 -0
  67. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[slug]/$types.d.ts +17 -0
  68. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.js +1 -0
  69. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.svelte +1 -0
  70. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.js +3 -0
  71. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.svelte +0 -0
  72. package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[slug]/+page@.svelte +0 -0
  73. package/src/core/utils.js +70 -0
  74. package/src/hooks.js +26 -0
  75. package/src/index/index.js +45 -0
  76. package/src/index/private.js +33 -0
  77. package/src/node/index.js +145 -0
  78. package/src/node/polyfills.js +40 -0
  79. package/src/runtime/app/env.js +11 -0
  80. package/src/runtime/app/navigation.js +22 -0
  81. package/src/runtime/app/paths.js +1 -0
  82. package/src/runtime/app/stores.js +102 -0
  83. package/src/runtime/client/ambient.d.ts +18 -0
  84. package/src/runtime/client/client.js +1362 -0
  85. package/src/runtime/client/fetcher.js +60 -0
  86. package/src/runtime/client/parse.js +41 -0
  87. package/src/runtime/client/singletons.js +21 -0
  88. package/src/runtime/client/start.js +46 -0
  89. package/src/runtime/client/types.d.ts +86 -0
  90. package/src/runtime/client/utils.js +113 -0
  91. package/src/runtime/components/error.svelte +16 -0
  92. package/{assets → src/runtime}/components/layout.svelte +0 -0
  93. package/src/runtime/env/dynamic/private.js +1 -0
  94. package/src/runtime/env/dynamic/public.js +1 -0
  95. package/src/runtime/env-private.js +7 -0
  96. package/src/runtime/env-public.js +7 -0
  97. package/src/runtime/env.js +6 -0
  98. package/src/runtime/hash.js +16 -0
  99. package/src/runtime/paths.js +11 -0
  100. package/src/runtime/server/endpoint.js +50 -0
  101. package/src/runtime/server/index.js +492 -0
  102. package/src/runtime/server/page/cookie.js +25 -0
  103. package/src/runtime/server/page/crypto.js +239 -0
  104. package/src/runtime/server/page/csp.js +249 -0
  105. package/src/runtime/server/page/fetch.js +266 -0
  106. package/src/runtime/server/page/index.js +408 -0
  107. package/src/runtime/server/page/load_data.js +168 -0
  108. package/src/runtime/server/page/render.js +361 -0
  109. package/src/runtime/server/page/respond_with_error.js +95 -0
  110. package/src/runtime/server/page/types.d.ts +44 -0
  111. package/src/runtime/server/utils.js +116 -0
  112. package/src/utils/array.js +9 -0
  113. package/src/utils/error.js +22 -0
  114. package/src/utils/escape.js +104 -0
  115. package/src/utils/filesystem.js +108 -0
  116. package/src/utils/functions.js +16 -0
  117. package/src/utils/http.js +55 -0
  118. package/src/utils/misc.js +1 -0
  119. package/src/utils/routing.js +146 -0
  120. package/src/utils/url.js +142 -0
  121. package/src/vite/build/build_server.js +348 -0
  122. package/src/vite/build/build_service_worker.js +90 -0
  123. package/src/vite/build/utils.js +160 -0
  124. package/src/vite/dev/index.js +543 -0
  125. package/src/vite/index.js +578 -0
  126. package/src/vite/preview/index.js +186 -0
  127. package/src/vite/types.d.ts +3 -0
  128. package/src/vite/utils.js +345 -0
  129. package/svelte-kit.js +1 -1
  130. package/types/ambient.d.ts +366 -0
  131. package/types/index.d.ts +345 -0
  132. package/types/internal.d.ts +374 -0
  133. package/types/private.d.ts +209 -0
  134. package/CHANGELOG.md +0 -437
  135. package/assets/components/error.svelte +0 -13
  136. package/assets/runtime/app/env.js +0 -5
  137. package/assets/runtime/app/navigation.js +0 -41
  138. package/assets/runtime/app/paths.js +0 -1
  139. package/assets/runtime/app/stores.js +0 -93
  140. package/assets/runtime/chunks/utils.js +0 -19
  141. package/assets/runtime/internal/singletons.js +0 -23
  142. package/assets/runtime/internal/start.js +0 -770
  143. package/assets/runtime/paths.js +0 -12
  144. package/dist/chunks/index.js +0 -3521
  145. package/dist/chunks/index2.js +0 -587
  146. package/dist/chunks/index3.js +0 -246
  147. package/dist/chunks/index4.js +0 -545
  148. package/dist/chunks/index5.js +0 -761
  149. package/dist/chunks/index6.js +0 -322
  150. package/dist/chunks/standard.js +0 -99
  151. package/dist/chunks/utils.js +0 -83
  152. package/dist/cli.js +0 -546
  153. package/dist/ssr.js +0 -2580
package/README.md CHANGED
@@ -1,15 +1,18 @@
1
- # @sveltejs/kit
1
+ # The fastest way to build Svelte apps
2
2
 
3
- Here be dragons, etc etc.
3
+ This is the [SvelteKit](https://kit.svelte.dev) framework and CLI.
4
4
 
5
- This project aims to replicate Sapper's functionality in its entirety, minus building for deployment (which can be handled by 'adapters' that do various opinionated things with the output of `snowpack build`).
5
+ The quickest way to get started is via the [create-svelte](https://github.com/sveltejs/kit/tree/master/packages/create-svelte) package:
6
6
 
7
- It's currently missing a ton of stuff but I figured I'd throw it up on GitHub anyway partly for the sake of 'working in the open' but mostly because I need an issue tracker to organise my thoughts.
7
+ ```bash
8
+ npm create svelte@latest my-app
9
+ cd my-app
10
+ npm install
11
+ npm run dev
12
+ ```
8
13
 
9
- There are no tests yet or anything like that. Some of the code has just been straight copied over from the existing Sapper repo, but a pleasing amount of it can safely be left behind.
14
+ See the [documentation](https://kit.svelte.dev/docs) to learn more.
10
15
 
11
- ## Trying it out
16
+ ## Changelog
12
17
 
13
- Clone this repo, `npm install`, and `npm link`. That will create a global link to the `svelte` bin. You can then either `npm run build` or `npm run dev`, if you intend to make changes and see them immediately reflected.
14
-
15
- Then, clone the corresponding [svelte-app-demo](https://github.com/sveltejs/svelte-app-demo) repo and follow the instructions therein.
18
+ [The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/kit/CHANGELOG.md).
package/package.json CHANGED
@@ -1,64 +1,95 @@
1
1
  {
2
- "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.44",
4
- "type": "module",
5
- "dependencies": {
6
- "@sveltejs/vite-plugin-svelte": "1.0.0-next.2",
7
- "@svitejs/vite-plugin-svelte": "^0.10.0-0",
8
- "cheap-watch": "^1.0.3",
9
- "sade": "^1.7.4"
10
- },
11
- "devDependencies": {
12
- "@rollup/plugin-replace": "^2.4.1",
13
- "@sveltejs/app-utils": "1.0.0-next.2",
14
- "@types/amphtml-validator": "^1.0.1",
15
- "@types/mime": "^2.0.3",
16
- "@types/node": "^14.14.31",
17
- "@types/rimraf": "^3.0.0",
18
- "@types/sade": "^1.7.2",
19
- "amphtml-validator": "^1.0.34",
20
- "devalue": "^2.0.1",
21
- "eslint": "^7.19.0",
22
- "kleur": "^4.1.4",
23
- "node-fetch": "^3.0.0-beta.8",
24
- "port-authority": "^1.1.2",
25
- "rimraf": "^3.0.2",
26
- "rollup": "^2.38.3",
27
- "sirv": "^1.0.11",
28
- "svelte": "^3.35.0",
29
- "tiny-glob": "^0.2.8",
30
- "typescript": "^4.2.2",
31
- "uvu": "^0.5.1",
32
- "vite": "^2.0.4"
33
- },
34
- "peerDependencies": {
35
- "svelte": "^3.32.1",
36
- "vite": "^2.0.4"
37
- },
38
- "bin": {
39
- "svelte-kit": "svelte-kit.js"
40
- },
41
- "files": [
42
- "assets",
43
- "dist",
44
- "client"
45
- ],
46
- "scripts": {
47
- "dev": "rm -rf assets/runtime && rollup -cw",
48
- "build": "rm -rf assets/runtime && rollup -c",
49
- "lint": "eslint --ignore-path .gitignore \"src/**/*.{ts,mjs,js,svelte}\" && npm run check-format",
50
- "check": "npx tsc",
51
- "format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
52
- "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
53
- "prepublishOnly": "npm run build",
54
- "test": "npm run test:unit && npm run test:integration",
55
- "test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\"",
56
- "test:integration": "uvu test test.js"
57
- },
58
- "exports": {
59
- "./ssr": {
60
- "import": "./dist/ssr.js"
61
- }
62
- },
63
- "types": "types.d.ts"
64
- }
2
+ "name": "@sveltejs/kit",
3
+ "version": "1.0.0-next.442",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/sveltejs/kit",
7
+ "directory": "packages/kit"
8
+ },
9
+ "license": "MIT",
10
+ "homepage": "https://kit.svelte.dev",
11
+ "type": "module",
12
+ "dependencies": {
13
+ "@sveltejs/vite-plugin-svelte": "^1.0.1",
14
+ "cookie": "^0.5.0",
15
+ "devalue": "^2.0.1",
16
+ "kleur": "^4.1.4",
17
+ "magic-string": "^0.26.2",
18
+ "mime": "^3.0.0",
19
+ "node-fetch": "^3.2.4",
20
+ "sade": "^1.8.1",
21
+ "set-cookie-parser": "^2.4.8",
22
+ "sirv": "^2.0.2",
23
+ "tiny-glob": "^0.2.9",
24
+ "undici": "^5.8.1"
25
+ },
26
+ "devDependencies": {
27
+ "@playwright/test": "^1.25.0",
28
+ "@types/connect": "^3.4.35",
29
+ "@types/cookie": "^0.5.1",
30
+ "@types/marked": "^4.0.3",
31
+ "@types/mime": "^3.0.0",
32
+ "@types/node": "^16.11.36",
33
+ "@types/sade": "^1.7.4",
34
+ "@types/set-cookie-parser": "^2.4.2",
35
+ "marked": "^4.0.16",
36
+ "rollup": "^2.75.7",
37
+ "svelte": "^3.48.0",
38
+ "svelte-preprocess": "^4.10.6",
39
+ "tiny-glob": "^0.2.9",
40
+ "typescript": "^4.7.4",
41
+ "uvu": "^0.5.3",
42
+ "vite": "^3.0.9"
43
+ },
44
+ "peerDependencies": {
45
+ "svelte": "^3.44.0",
46
+ "vite": "^3.0.0"
47
+ },
48
+ "bin": {
49
+ "svelte-kit": "svelte-kit.js"
50
+ },
51
+ "files": [
52
+ "src",
53
+ "!src/**/*.spec.js",
54
+ "!src/packaging/test",
55
+ "!src/core/**/fixtures",
56
+ "!src/core/sync/create_manifest_data/test",
57
+ "types",
58
+ "svelte-kit.js"
59
+ ],
60
+ "exports": {
61
+ "./package.json": "./package.json",
62
+ ".": {
63
+ "import": "./src/index/index.js",
64
+ "types": "./types/index.d.ts"
65
+ },
66
+ "./node": {
67
+ "import": "./src/node/index.js"
68
+ },
69
+ "./node/polyfills": {
70
+ "import": "./src/node/polyfills.js"
71
+ },
72
+ "./hooks": {
73
+ "import": "./src/hooks.js"
74
+ },
75
+ "./vite": {
76
+ "import": "./src/vite/index.js"
77
+ }
78
+ },
79
+ "types": "types/index.d.ts",
80
+ "engines": {
81
+ "node": ">=16.9"
82
+ },
83
+ "scripts": {
84
+ "build": "npm run types",
85
+ "lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
86
+ "check": "tsc",
87
+ "check:all": "tsc && pnpm -r --filter=\"./**\" check",
88
+ "format": "npm run lint -- --write",
89
+ "test": "npm run test:unit && npm run test:integration",
90
+ "test:integration": "pnpm run -r --workspace-concurrency 1 --filter=\"./test/**\" test",
91
+ "test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\" -i packaging",
92
+ "types": "node scripts/extract-types.js",
93
+ "postinstall": "node svelte-kit.js sync"
94
+ }
95
+ }
package/src/cli.js ADDED
@@ -0,0 +1,112 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import colors from 'kleur';
4
+ import sade from 'sade';
5
+ import { load_config } from './core/config/index.js';
6
+ import { coalesce_to_error } from './utils/error.js';
7
+
8
+ /** @param {unknown} e */
9
+ function handle_error(e) {
10
+ const error = coalesce_to_error(e);
11
+
12
+ if (error.name === 'SyntaxError') throw error;
13
+
14
+ console.error(colors.bold().red(`> ${error.message}`));
15
+ if (error.stack) {
16
+ console.error(colors.gray(error.stack.split('\n').slice(1).join('\n')));
17
+ }
18
+
19
+ process.exit(1);
20
+ }
21
+
22
+ const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
23
+ const prog = sade('svelte-kit').version(pkg.version);
24
+
25
+ prog
26
+ .command('sync')
27
+ .describe('Synchronise generated files')
28
+ .option('--mode', 'Specify a mode for loading environment variables', 'development')
29
+ .action(async ({ mode }) => {
30
+ const event = process.env.npm_lifecycle_event;
31
+
32
+ // TODO remove for 1.0
33
+ if (event === 'prepare') {
34
+ const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
35
+ const message =
36
+ pkg.scripts.prepare === 'svelte-kit sync'
37
+ ? `\`svelte-kit sync\` now runs on "postinstall" — please remove the "prepare" script from your package.json\n`
38
+ : `\`svelte-kit sync\` now runs on "postinstall" — please remove it from your "prepare" script\n`;
39
+
40
+ console.error(colors.bold().red(message));
41
+ return;
42
+ }
43
+
44
+ if (event === 'postinstall' && process.env.INIT_CWD) {
45
+ process.chdir(process.env.INIT_CWD);
46
+ }
47
+
48
+ if (!fs.existsSync('svelte.config.js')) {
49
+ console.warn(`Missing ${path.resolve('svelte.config.js')} — skipping`);
50
+ return;
51
+ }
52
+
53
+ try {
54
+ const config = await load_config();
55
+ const sync = await import('./core/sync/sync.js');
56
+ await sync.all(config, mode);
57
+ } catch (error) {
58
+ handle_error(error);
59
+ }
60
+ });
61
+
62
+ // TODO remove for 1.0
63
+ replace('dev');
64
+ replace('build');
65
+ replace('preview');
66
+ prog
67
+ .command('package')
68
+ .describe('No longer available - use @sveltejs/package instead')
69
+ .action(() => {
70
+ console.error(
71
+ 'svelte-kit package has been removed. It now lives in its own npm package. See the PR on how to migrate: https://github.com/sveltejs/kit/pull/5730'
72
+ );
73
+ });
74
+
75
+ prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
76
+
77
+ /** @param {string} command */
78
+ function replace(command) {
79
+ prog
80
+ .command(command)
81
+ .describe(`No longer available — use vite ${command} instead`)
82
+ .action(async () => {
83
+ const message = `\n> svelte-kit ${command} is no longer available — use vite ${command} instead`;
84
+ console.error(colors.bold().red(message));
85
+
86
+ const steps = [
87
+ 'Install vite as a devDependency with npm/pnpm/etc',
88
+ 'Create a vite.config.js with the @sveltejs/kit/vite plugin (see below)',
89
+ `Update your package.json scripts to reference \`vite ${command}\` instead of \`svelte-kit ${command}\``
90
+ ];
91
+
92
+ steps.forEach((step, i) => {
93
+ console.error(` ${i + 1}. ${colors.cyan(step)}`);
94
+ });
95
+
96
+ console.error(
97
+ `
98
+ ${colors.grey('// vite.config.js')}
99
+ import { sveltekit } from '@sveltejs/kit/vite';
100
+
101
+ /** @type {import('vite').UserConfig} */
102
+ const config = {
103
+ plugins: [sveltekit()]
104
+ };
105
+
106
+ export default config;
107
+
108
+ `.replace(/^\t{4}/gm, '')
109
+ );
110
+ process.exit(1);
111
+ });
112
+ }
@@ -0,0 +1,223 @@
1
+ import glob from 'tiny-glob';
2
+ import zlib from 'zlib';
3
+ import { existsSync, statSync, createReadStream, createWriteStream } from 'fs';
4
+ import { pipeline } from 'stream';
5
+ import { promisify } from 'util';
6
+ import { copy, rimraf, mkdirp } from '../../utils/filesystem.js';
7
+ import { generate_manifest } from '../generate_manifest/index.js';
8
+ import { get_path } from '../../utils/routing.js';
9
+
10
+ /**
11
+ * Creates the Builder which is passed to adapters for building the application.
12
+ * @param {{
13
+ * config: import('types').ValidatedConfig;
14
+ * build_data: import('types').BuildData;
15
+ * prerendered: import('types').Prerendered;
16
+ * log: import('types').Logger;
17
+ * }} opts
18
+ * @returns {import('types').Builder}
19
+ */
20
+ export function create_builder({ config, build_data, prerendered, log }) {
21
+ /** @type {Set<string>} */
22
+ const prerendered_paths = new Set(prerendered.paths);
23
+
24
+ /** @param {import('types').RouteData} route */
25
+ // TODO routes should come pre-filtered
26
+ function not_prerendered(route) {
27
+ const path = route.page && get_path(route.id);
28
+ if (path) {
29
+ return !prerendered_paths.has(path) && !prerendered_paths.has(path + '/');
30
+ }
31
+
32
+ return true;
33
+ }
34
+
35
+ const pipe = promisify(pipeline);
36
+
37
+ /**
38
+ * @param {string} file
39
+ * @param {'gz' | 'br'} format
40
+ */
41
+ async function compress_file(file, format = 'gz') {
42
+ const compress =
43
+ format == 'br'
44
+ ? zlib.createBrotliCompress({
45
+ params: {
46
+ [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
47
+ [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
48
+ [zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
49
+ }
50
+ })
51
+ : zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
52
+
53
+ const source = createReadStream(file);
54
+ const destination = createWriteStream(`${file}.${format}`);
55
+
56
+ await pipe(source, compress, destination);
57
+ }
58
+
59
+ return {
60
+ log,
61
+ rimraf,
62
+ mkdirp,
63
+ copy,
64
+
65
+ config,
66
+ prerendered,
67
+
68
+ async createEntries(fn) {
69
+ const { routes } = build_data.manifest_data;
70
+
71
+ /** @type {import('types').RouteDefinition[]} */
72
+ const facades = routes.map((route) => {
73
+ const methods = new Set();
74
+
75
+ if (route.page) {
76
+ methods.add('SET');
77
+ }
78
+
79
+ if (route.endpoint) {
80
+ for (const method of build_data.server.methods[route.endpoint.file]) {
81
+ methods.add(method);
82
+ }
83
+ }
84
+
85
+ return {
86
+ id: route.id,
87
+ type: route.page ? 'page' : 'endpoint', // TODO change this if support pages+endpoints
88
+ segments: route.id.split('/').map((segment) => ({
89
+ dynamic: segment.includes('['),
90
+ rest: segment.includes('[...'),
91
+ content: segment
92
+ })),
93
+ pattern: route.pattern,
94
+ methods: Array.from(methods)
95
+ };
96
+ });
97
+
98
+ const seen = new Set();
99
+
100
+ for (let i = 0; i < routes.length; i += 1) {
101
+ const route = routes[i];
102
+ const { id, filter, complete } = fn(facades[i]);
103
+
104
+ if (seen.has(id)) continue;
105
+ seen.add(id);
106
+
107
+ const group = [route];
108
+
109
+ // figure out which lower priority routes should be considered fallbacks
110
+ for (let j = i + 1; j < routes.length; j += 1) {
111
+ if (filter(facades[j])) {
112
+ group.push(routes[j]);
113
+ }
114
+ }
115
+
116
+ const filtered = new Set(group.filter(not_prerendered));
117
+
118
+ // heuristic: if /foo/[bar] is included, /foo/[bar].json should
119
+ // also be included, since the page likely needs the endpoint
120
+ // TODO is this still necessary, given the new way of doing things?
121
+ filtered.forEach((route) => {
122
+ if (route.page) {
123
+ const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
124
+
125
+ if (endpoint) {
126
+ filtered.add(endpoint);
127
+ }
128
+ }
129
+ });
130
+
131
+ if (filtered.size > 0) {
132
+ await complete({
133
+ generateManifest: ({ relativePath, format }) =>
134
+ generate_manifest({
135
+ build_data,
136
+ relative_path: relativePath,
137
+ routes: Array.from(filtered),
138
+ format
139
+ })
140
+ });
141
+ }
142
+ }
143
+ },
144
+
145
+ generateManifest: ({ relativePath, format }) => {
146
+ return generate_manifest({
147
+ build_data,
148
+ relative_path: relativePath,
149
+ routes: build_data.manifest_data.routes.filter(not_prerendered),
150
+ format
151
+ });
152
+ },
153
+
154
+ getBuildDirectory(name) {
155
+ return `${config.kit.outDir}/${name}`;
156
+ },
157
+
158
+ getClientDirectory() {
159
+ return `${config.kit.outDir}/output/client`;
160
+ },
161
+
162
+ getServerDirectory() {
163
+ return `${config.kit.outDir}/output/server`;
164
+ },
165
+
166
+ getStaticDirectory() {
167
+ return config.kit.files.assets;
168
+ },
169
+
170
+ writeClient(dest) {
171
+ return [...copy(`${config.kit.outDir}/output/client`, dest)];
172
+ },
173
+
174
+ writePrerendered(dest, { fallback } = {}) {
175
+ const source = `${config.kit.outDir}/output/prerendered`;
176
+ const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
177
+
178
+ if (fallback) {
179
+ files.push(fallback);
180
+ copy(`${source}/fallback.html`, `${dest}/${fallback}`);
181
+ }
182
+
183
+ return files;
184
+ },
185
+
186
+ writeServer(dest) {
187
+ return copy(`${config.kit.outDir}/output/server`, dest);
188
+ },
189
+
190
+ // TODO remove these methods for 1.0
191
+ // @ts-expect-error
192
+ writeStatic() {
193
+ throw new Error(
194
+ `writeStatic has been removed. Please ensure you are using the latest version of ${
195
+ config.kit.adapter.name || 'your adapter'
196
+ }`
197
+ );
198
+ },
199
+
200
+ async compress(directory) {
201
+ if (!existsSync(directory)) {
202
+ return;
203
+ }
204
+
205
+ const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
206
+ cwd: directory,
207
+ dot: true,
208
+ absolute: true,
209
+ filesOnly: true
210
+ });
211
+
212
+ await Promise.all(
213
+ files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
214
+ );
215
+ },
216
+
217
+ async prerender() {
218
+ throw new Error(
219
+ 'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
220
+ );
221
+ }
222
+ };
223
+ }
@@ -0,0 +1,19 @@
1
+ import colors from 'kleur';
2
+ import { create_builder } from './builder.js';
3
+
4
+ /**
5
+ * @param {import('types').ValidatedConfig} config
6
+ * @param {import('types').BuildData} build_data
7
+ * @param {import('types').Prerendered} prerendered
8
+ * @param {{ log: import('types').Logger }} opts
9
+ */
10
+ export async function adapt(config, build_data, prerendered, { log }) {
11
+ const { name, adapt } = config.kit.adapter;
12
+
13
+ console.log(colors.bold().cyan(`\n> Using ${name}`));
14
+
15
+ const builder = create_builder({ config, build_data, prerendered, log });
16
+ await adapt(builder);
17
+
18
+ log.success('done');
19
+ }
@@ -0,0 +1,86 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import * as url from 'url';
4
+ import options from './options.js';
5
+
6
+ /**
7
+ * Loads the template (src/app.html by default) and validates that it has the
8
+ * required content.
9
+ * @param {string} cwd
10
+ * @param {import('types').ValidatedConfig} config
11
+ */
12
+ export function load_template(cwd, config) {
13
+ const { template } = config.kit.files;
14
+ const relative = path.relative(cwd, template);
15
+
16
+ if (fs.existsSync(template)) {
17
+ const contents = fs.readFileSync(template, 'utf8');
18
+
19
+ // TODO remove this for 1.0
20
+ const match = /%svelte\.([a-z]+)%/.exec(contents);
21
+ if (match) {
22
+ throw new Error(
23
+ `%svelte.${match[1]}% in ${relative} should be replaced with %sveltekit.${match[1]}%`
24
+ );
25
+ }
26
+
27
+ const expected_tags = ['%sveltekit.head%', '%sveltekit.body%'];
28
+ expected_tags.forEach((tag) => {
29
+ if (contents.indexOf(tag) === -1) {
30
+ throw new Error(`${relative} is missing ${tag}`);
31
+ }
32
+ });
33
+ } else {
34
+ throw new Error(`${relative} does not exist`);
35
+ }
36
+
37
+ return fs.readFileSync(template, 'utf-8');
38
+ }
39
+
40
+ /**
41
+ * Loads and validates svelte.config.js
42
+ * @param {{ cwd?: string }} options
43
+ * @returns {Promise<import('types').ValidatedConfig>}
44
+ */
45
+ export async function load_config({ cwd = process.cwd() } = {}) {
46
+ const config_file = path.join(cwd, 'svelte.config.js');
47
+
48
+ if (!fs.existsSync(config_file)) {
49
+ return process_config({}, { cwd });
50
+ }
51
+
52
+ const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
53
+
54
+ return process_config(config.default, { cwd });
55
+ }
56
+
57
+ /**
58
+ * @param {import('types').Config} config
59
+ * @returns {import('types').ValidatedConfig}
60
+ */
61
+ function process_config(config, { cwd = process.cwd() } = {}) {
62
+ const validated = validate_config(config);
63
+
64
+ validated.kit.outDir = path.resolve(cwd, validated.kit.outDir);
65
+
66
+ for (const key in validated.kit.files) {
67
+ // @ts-expect-error this is typescript at its stupidest
68
+ validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);
69
+ }
70
+
71
+ return validated;
72
+ }
73
+
74
+ /**
75
+ * @param {import('types').Config} config
76
+ * @returns {import('types').ValidatedConfig}
77
+ */
78
+ export function validate_config(config) {
79
+ if (typeof config !== 'object') {
80
+ throw new Error(
81
+ 'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
82
+ );
83
+ }
84
+
85
+ return options(config, 'config');
86
+ }