@sveltejs/kit 1.2.10 → 1.3.1

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.
@@ -3,6 +3,7 @@ import path from 'path';
3
3
  import colors from 'kleur';
4
4
  import { posixify } from '../../utils/filesystem.js';
5
5
  import { write_if_changed } from './utils.js';
6
+ import { ts } from './ts.js';
6
7
 
7
8
  /**
8
9
  * @param {string} cwd
@@ -34,7 +35,7 @@ function remove_trailing_slashstar(file) {
34
35
  }
35
36
 
36
37
  /**
37
- * Writes the tsconfig that the user's tsconfig inherits from.
38
+ * Generates the tsconfig that the user's tsconfig inherits from.
38
39
  * @param {import('types').ValidatedKitConfig} kit
39
40
  */
40
41
  export function write_tsconfig(kit, cwd = process.cwd()) {
@@ -72,6 +73,15 @@ export function write_tsconfig(kit, cwd = process.cwd()) {
72
73
  }
73
74
  }
74
75
 
76
+ write_if_changed(out, JSON.stringify(get_tsconfig(kit, include_base_url), null, '\t'));
77
+ }
78
+
79
+ /**
80
+ * Generates the tsconfig that the user's tsconfig inherits from.
81
+ * @param {import('types').ValidatedKitConfig} kit
82
+ * @param {boolean} include_base_url
83
+ */
84
+ export function get_tsconfig(kit, include_base_url) {
75
85
  /** @param {string} file */
76
86
  const config_relative = (file) => posixify(path.relative(kit.outDir, file));
77
87
 
@@ -98,40 +108,38 @@ export function write_tsconfig(kit, cwd = process.cwd()) {
98
108
  exclude.push(config_relative(`${kit.files.serviceWorker}.d.ts`));
99
109
  }
100
110
 
101
- write_if_changed(
102
- out,
103
- JSON.stringify(
104
- {
105
- compilerOptions: {
106
- // generated options
107
- baseUrl: include_base_url ? config_relative('.') : undefined,
108
- paths: get_tsconfig_paths(kit, include_base_url),
109
- rootDirs: [config_relative('.'), './types'],
110
-
111
- // essential options
112
- // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
113
- // to enforce using \`import type\` instead of \`import\` for Types.
114
- importsNotUsedAsValues: 'error',
115
- // Vite compiles modules one at a time
116
- isolatedModules: true,
117
- // TypeScript doesn't know about import usages in the template because it only sees the
118
- // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
119
- preserveValueImports: true,
120
-
121
- // This is required for svelte-package to work as expected
122
- // Can be overwritten
123
- lib: ['esnext', 'DOM', 'DOM.Iterable'],
124
- moduleResolution: 'node',
125
- module: 'esnext',
126
- target: 'esnext'
127
- },
128
- include,
129
- exclude
130
- },
131
- null,
132
- '\t'
133
- )
134
- );
111
+ const config = {
112
+ compilerOptions: {
113
+ // generated options
114
+ baseUrl: include_base_url ? config_relative('.') : undefined,
115
+ paths: get_tsconfig_paths(kit, include_base_url),
116
+ rootDirs: [config_relative('.'), './types'],
117
+
118
+ // essential options
119
+ // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
120
+ // to enforce using \`import type\` instead of \`import\` for Types.
121
+ importsNotUsedAsValues: 'error',
122
+ // Vite compiles modules one at a time
123
+ isolatedModules: true,
124
+ // TypeScript doesn't know about import usages in the template because it only sees the
125
+ // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
126
+ preserveValueImports: true,
127
+
128
+ // This is required for svelte-package to work as expected
129
+ // Can be overwritten
130
+ lib: ['esnext', 'DOM', 'DOM.Iterable'],
131
+ moduleResolution: 'node',
132
+ module: 'esnext',
133
+ target: 'esnext',
134
+
135
+ // TODO(v2): use the new flag verbatimModuleSyntax instead (requires support by Vite/Esbuild)
136
+ ignoreDeprecations: ts && Number(ts.version.split('.')[0]) >= 5 ? '5.0' : undefined
137
+ },
138
+ include,
139
+ exclude
140
+ };
141
+
142
+ return kit.typescript.config(config) ?? config;
135
143
  }
136
144
 
137
145
  /** @param {string} cwd */
@@ -212,7 +220,7 @@ const value_regex = /^(.*?)((\/\*)|(\.\w+))?$/;
212
220
  * @param {import('types').ValidatedKitConfig} config
213
221
  * @param {boolean} include_base_url
214
222
  */
215
- export function get_tsconfig_paths(config, include_base_url) {
223
+ function get_tsconfig_paths(config, include_base_url) {
216
224
  /** @param {string} file */
217
225
  const config_relative = (file) => posixify(path.relative(config.outDir, file));
218
226
 
@@ -3,6 +3,7 @@ import path from 'path';
3
3
  import MagicString from 'magic-string';
4
4
  import { posixify, rimraf, walk } from '../../../utils/filesystem.js';
5
5
  import { compact } from '../../../utils/array.js';
6
+ import { ts } from '../ts.js';
6
7
 
7
8
  /**
8
9
  * @typedef {{
@@ -20,13 +21,6 @@ import { compact } from '../../../utils/array.js';
20
21
  * @typedef {Map<import('types').PageNode, {route: import('types').RouteData, proxies: Proxies}>} RoutesMap
21
22
  */
22
23
 
23
- /** @type {import('typescript')} */
24
- // @ts-ignore
25
- let ts = undefined;
26
- try {
27
- ts = (await import('typescript')).default;
28
- } catch {}
29
-
30
24
  const cwd = process.cwd();
31
25
 
32
26
  /**
@@ -1,61 +1,33 @@
1
1
  import fs from 'node:fs';
2
- import path from 'node:path';
3
- import * as vite from 'vite';
4
- import { mkdirp, posixify } from '../../../utils/filesystem.js';
5
- import { find_deps, is_http_method, resolve_symlinks } from './utils.js';
2
+ import { mkdirp } from '../../../utils/filesystem.js';
3
+ import { find_deps, resolve_symlinks } from './utils.js';
6
4
  import { s } from '../../../utils/misc.js';
7
5
 
8
6
  /**
9
- * @param {{
10
- * config: import('types').ValidatedConfig;
11
- * vite_config: import('vite').ResolvedConfig;
12
- * vite_config_env: import('vite').ConfigEnv;
13
- * manifest_data: import('types').ManifestData;
14
- * output_dir: string;
15
- * }} options
16
- * @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
7
+ * @param {string} out
8
+ * @param {import('types').ValidatedKitConfig} kit
9
+ * @param {import('types').ManifestData} manifest_data
10
+ * @param {import('vite').Manifest} server_manifest
11
+ * @param {import('vite').Manifest | null} client_manifest
12
+ * @param {import('rollup').OutputAsset[] | null} css
17
13
  */
18
- export async function build_server(options, client) {
19
- const { config, vite_config, vite_config_env, manifest_data, output_dir } = options;
20
-
21
- const { output } = /** @type {import('rollup').RollupOutput} */ (
22
- await vite.build({
23
- // CLI args
24
- configFile: vite_config.configFile,
25
- mode: vite_config_env.mode,
26
- logLevel: config.logLevel,
27
- clearScreen: config.clearScreen,
28
- build: {
29
- ssr: true
30
- }
31
- })
32
- );
33
-
34
- const chunks = /** @type {import('rollup').OutputChunk[]} */ (
35
- output.filter((chunk) => chunk.type === 'chunk')
36
- );
37
-
38
- /** @type {import('vite').Manifest} */
39
- const vite_manifest = JSON.parse(
40
- fs.readFileSync(`${output_dir}/server/${vite_config.build.manifest}`, 'utf-8')
41
- );
42
-
43
- mkdirp(`${output_dir}/server/nodes`);
44
- mkdirp(`${output_dir}/server/stylesheets`);
14
+ export function build_server_nodes(out, kit, manifest_data, server_manifest, client_manifest, css) {
15
+ mkdirp(`${out}/server/nodes`);
16
+ mkdirp(`${out}/server/stylesheets`);
45
17
 
46
18
  const stylesheet_lookup = new Map();
47
19
 
48
- client.assets.forEach((asset) => {
49
- if (asset.fileName.endsWith('.css')) {
50
- if (asset.source.length < config.kit.inlineStyleThreshold) {
20
+ if (css) {
21
+ css.forEach((asset) => {
22
+ if (asset.source.length < kit.inlineStyleThreshold) {
51
23
  const index = stylesheet_lookup.size;
52
- const file = `${output_dir}/server/stylesheets/${index}.js`;
24
+ const file = `${out}/server/stylesheets/${index}.js`;
53
25
 
54
26
  fs.writeFileSync(file, `// ${asset.fileName}\nexport default ${s(asset.source)};`);
55
27
  stylesheet_lookup.set(asset.fileName, index);
56
28
  }
57
- }
58
- });
29
+ });
30
+ }
59
31
 
60
32
  manifest_data.nodes.forEach((node, i) => {
61
33
  /** @type {string[]} */
@@ -75,8 +47,8 @@ export async function build_server(options, client) {
75
47
  /** @type {string[]} */
76
48
  const fonts = [];
77
49
 
78
- if (node.component) {
79
- const entry = find_deps(client.vite_manifest, node.component, true);
50
+ if (node.component && client_manifest) {
51
+ const entry = find_deps(client_manifest, node.component, true);
80
52
 
81
53
  imported.push(...entry.imports);
82
54
  stylesheets.push(...entry.stylesheets);
@@ -84,25 +56,27 @@ export async function build_server(options, client) {
84
56
 
85
57
  exports.push(
86
58
  `export const component = async () => (await import('../${
87
- resolve_symlinks(vite_manifest, node.component).chunk.file
59
+ resolve_symlinks(server_manifest, node.component).chunk.file
88
60
  }')).default;`,
89
61
  `export const file = '${entry.file}';` // TODO what is this?
90
62
  );
91
63
  }
92
64
 
93
65
  if (node.universal) {
94
- const entry = find_deps(client.vite_manifest, node.universal, true);
66
+ if (client_manifest) {
67
+ const entry = find_deps(client_manifest, node.universal, true);
95
68
 
96
- imported.push(...entry.imports);
97
- stylesheets.push(...entry.stylesheets);
98
- fonts.push(...entry.fonts);
69
+ imported.push(...entry.imports);
70
+ stylesheets.push(...entry.stylesheets);
71
+ fonts.push(...entry.fonts);
72
+ }
99
73
 
100
- imports.push(`import * as universal from '../${vite_manifest[node.universal].file}';`);
74
+ imports.push(`import * as universal from '../${server_manifest[node.universal].file}';`);
101
75
  exports.push(`export { universal };`);
102
76
  }
103
77
 
104
78
  if (node.server) {
105
- imports.push(`import * as server from '../${vite_manifest[node.server].file}';`);
79
+ imports.push(`import * as server from '../${server_manifest[node.server].file}';`);
106
80
  exports.push(`export { server };`);
107
81
  }
108
82
 
@@ -128,45 +102,9 @@ export async function build_server(options, client) {
128
102
  exports.push(`export const inline_styles = () => ({\n${styles.join(',\n')}\n});`);
129
103
  }
130
104
 
131
- const out = `${output_dir}/server/nodes/${i}.js`;
132
- fs.writeFileSync(out, `${imports.join('\n')}\n\n${exports.join('\n')}\n`);
133
- });
134
-
135
- return {
136
- chunks,
137
- vite_manifest,
138
- methods: get_methods(chunks, manifest_data)
139
- };
140
- }
141
-
142
- /**
143
- * @param {import('rollup').OutputChunk[]} output
144
- * @param {import('types').ManifestData} manifest_data
145
- */
146
- function get_methods(output, manifest_data) {
147
- /** @type {Record<string, string[]>} */
148
- const lookup = {};
149
- output.forEach((chunk) => {
150
- if (!chunk.facadeModuleId) return;
151
- const id = posixify(path.relative('.', chunk.facadeModuleId));
152
- lookup[id] = chunk.exports;
153
- });
154
-
155
- /** @type {Record<string, import('types').HttpMethod[]>} */
156
- const methods = {};
157
- manifest_data.routes.forEach((route) => {
158
- if (route.endpoint) {
159
- if (lookup[route.endpoint.file]) {
160
- methods[route.endpoint.file] = lookup[route.endpoint.file].filter(is_http_method);
161
- }
162
- }
163
-
164
- if (route.leaf?.server) {
165
- if (lookup[route.leaf.server]) {
166
- methods[route.leaf.server] = lookup[route.leaf.server].filter(is_http_method);
167
- }
168
- }
105
+ fs.writeFileSync(
106
+ `${out}/server/nodes/${i}.js`,
107
+ `${imports.join('\n')}\n\n${exports.join('\n')}\n`
108
+ );
169
109
  });
170
-
171
- return methods;
172
110
  }
@@ -5,19 +5,19 @@ import { get_config_aliases } from '../utils.js';
5
5
  import { assets_base } from './utils.js';
6
6
 
7
7
  /**
8
- * @param {{
9
- * config: import('types').ValidatedConfig;
10
- * vite_config: import('vite').ResolvedConfig;
11
- * vite_config_env: import('vite').ConfigEnv;
12
- * manifest_data: import('types').ManifestData;
13
- * output_dir: string;
14
- * }} options
8
+ * @param {string} out
9
+ * @param {import('types').ValidatedKitConfig} kit
10
+ * @param {import('vite').ResolvedConfig} vite_config
11
+ * @param {import('types').ManifestData} manifest_data
15
12
  * @param {string} service_worker_entry_file
16
13
  * @param {import('types').Prerendered} prerendered
17
14
  * @param {import('vite').Manifest} client_manifest
18
15
  */
19
16
  export async function build_service_worker(
20
- { config, vite_config, manifest_data, output_dir },
17
+ out,
18
+ kit,
19
+ vite_config,
20
+ manifest_data,
21
21
  service_worker_entry_file,
22
22
  prerendered,
23
23
  client_manifest
@@ -30,21 +30,21 @@ export async function build_service_worker(
30
30
  assets.forEach((file) => build.add(file));
31
31
  }
32
32
 
33
- const service_worker = `${config.kit.outDir}/generated/service-worker.js`;
33
+ const service_worker = `${kit.outDir}/generated/service-worker.js`;
34
34
 
35
35
  fs.writeFileSync(
36
36
  service_worker,
37
37
  `
38
38
  export const build = [
39
39
  ${Array.from(build)
40
- .map((file) => `${s(`${config.kit.paths.base}/${file}`)}`)
40
+ .map((file) => `${s(`${kit.paths.base}/${file}`)}`)
41
41
  .join(',\n\t\t\t\t')}
42
42
  ];
43
43
 
44
44
  export const files = [
45
45
  ${manifest_data.assets
46
- .filter((asset) => config.kit.serviceWorker.files(asset.file))
47
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
46
+ .filter((asset) => kit.serviceWorker.files(asset.file))
47
+ .map((asset) => `${s(`${kit.paths.base}/${asset.file}`)}`)
48
48
  .join(',\n\t\t\t\t')}
49
49
  ];
50
50
 
@@ -52,14 +52,14 @@ export async function build_service_worker(
52
52
  ${prerendered.paths.map((path) => s(path)).join(',\n\t\t\t\t')}
53
53
  ];
54
54
 
55
- export const version = ${s(config.kit.version.name)};
55
+ export const version = ${s(kit.version.name)};
56
56
  `
57
57
  .replace(/^\t{3}/gm, '')
58
58
  .trim()
59
59
  );
60
60
 
61
61
  await vite.build({
62
- base: assets_base(config.kit),
62
+ base: assets_base(kit),
63
63
  build: {
64
64
  lib: {
65
65
  entry: /** @type {string} */ (service_worker_entry_file),
@@ -71,16 +71,13 @@ export async function build_service_worker(
71
71
  entryFileNames: 'service-worker.js'
72
72
  }
73
73
  },
74
- outDir: `${output_dir}/client`,
74
+ outDir: `${out}/client`,
75
75
  emptyOutDir: false
76
76
  },
77
77
  define: vite_config.define,
78
78
  configFile: false,
79
79
  resolve: {
80
- alias: [
81
- ...get_config_aliases(config.kit),
82
- { find: '$service-worker', replacement: service_worker }
83
- ]
80
+ alias: [...get_config_aliases(kit), { find: '$service-worker', replacement: service_worker }]
84
81
  }
85
82
  });
86
83
  }