Skip to content

Commit 55b541e

Browse files
committed
breaking: remove baseUrl fallback from generated tsconfig
- don't generate baseUrl anymore and don't adjust paths anymore depending on whether or not the user has paths in their tsconfig.json - more strict validation: warn on baseUrl/paths and suggest kit.alias instead closes #11286
1 parent 9439190 commit 55b541e

File tree

6 files changed

+42
-94
lines changed

6 files changed

+42
-94
lines changed

.changeset/dull-eyes-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": major
3+
---
4+
5+
breaking: remove baseUrl fallback from generated tsconfig

.changeset/loud-parrots-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-migrate": minor
3+
---
4+
5+
feat: add sveltekit v2 migration

documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ In SvelteKit 1, those properties included `form` and `data`. These were deprecat
123123

124124
If a form contains an `<input type="file">` but does not have an `enctype="multipart/form-data"` attribute, non-JS submissions will omit the file. SvelteKit 2 will throw an error if it encounters a form like this during a `use:enhance` submission to ensure that your forms work correctly when JavaScript is not present.
125125

126+
## Generated `tsconfig.json` is more strict
127+
128+
Previously, the generated `tsconfig.json` was trying its best to still produce a somewhat valid config when your `tsconfig.json` included `paths` or `baseUrl`. In SvelteKit 2, the validation is more strict and will warn when you use either `paths` or `baseUrl` in your `tsconfig.json`. These settings are used to generate path aliases and you should use [the `alias` config](configuration#alias) option in your `svelte.config.js` instead, to also create a corresponding alias for the bundler.
129+
126130
## Updated dependency requirements
127131

128132
SvelteKit 2 requires Node `18.13` or higher, and the following minimum dependency versions:

packages/kit/src/core/sync/write_tsconfig.js

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -41,46 +41,16 @@ export function write_tsconfig(kit, cwd = process.cwd()) {
4141
const out = path.join(kit.outDir, 'tsconfig.json');
4242

4343
const user_config = load_user_tsconfig(cwd);
44-
if (user_config) validate_user_config(kit, cwd, out, user_config);
45-
46-
// only specify baseUrl if a) the user doesn't specify their own baseUrl
47-
// and b) they have non-relative paths. this causes problems with auto-imports,
48-
// so we print a suggestion that they use relative paths instead
49-
// TODO(v2): never include base URL, and skip the check below
50-
let include_base_url = false;
51-
52-
if (user_config && !user_config.options.compilerOptions?.baseUrl) {
53-
const non_relative_paths = new Set();
54-
for (const paths of Object.values(user_config?.options.compilerOptions?.paths || {})) {
55-
for (const path of paths) {
56-
if (!path.startsWith('.')) non_relative_paths.add(path);
57-
}
58-
}
59-
60-
if (non_relative_paths.size) {
61-
include_base_url = true;
62-
63-
console.log(colors.bold().yellow('Please replace non-relative compilerOptions.paths:\n'));
64-
65-
for (const path of non_relative_paths) {
66-
console.log(` - "${path}" -> "./${path}"`);
67-
}
68-
69-
console.log(
70-
'\nDoing so allows us to omit "baseUrl" — which causes problems with imports — from the generated tsconfig.json. See https://github.com/sveltejs/kit/pull/8437 for more information.'
71-
);
72-
}
73-
}
44+
if (user_config) validate_user_config(cwd, out, user_config);
7445

75-
write_if_changed(out, JSON.stringify(get_tsconfig(kit, include_base_url), null, '\t'));
46+
write_if_changed(out, JSON.stringify(get_tsconfig(kit), null, '\t'));
7647
}
7748

7849
/**
7950
* Generates the tsconfig that the user's tsconfig inherits from.
8051
* @param {import('types').ValidatedKitConfig} kit
81-
* @param {boolean} include_base_url
8252
*/
83-
export function get_tsconfig(kit, include_base_url) {
53+
export function get_tsconfig(kit) {
8454
/** @param {string} file */
8555
const config_relative = (file) => posixify(path.relative(kit.outDir, file));
8656

@@ -122,8 +92,7 @@ export function get_tsconfig(kit, include_base_url) {
12292
const config = {
12393
compilerOptions: {
12494
// generated options
125-
baseUrl: include_base_url ? config_relative('.') : undefined,
126-
paths: get_tsconfig_paths(kit, include_base_url),
95+
paths: get_tsconfig_paths(kit),
12796
rootDirs: [config_relative('.'), './types'],
12897

12998
// essential options
@@ -166,12 +135,11 @@ function load_user_tsconfig(cwd) {
166135
}
167136

168137
/**
169-
* @param {import('types').ValidatedKitConfig} kit
170138
* @param {string} cwd
171139
* @param {string} out
172140
* @param {{ kind: string, options: any }} config
173141
*/
174-
function validate_user_config(kit, cwd, out, config) {
142+
function validate_user_config(cwd, out, config) {
175143
// we need to check that the user's tsconfig extends the framework config
176144
const extend = config.options.extends;
177145
const extends_framework_config =
@@ -184,29 +152,17 @@ function validate_user_config(kit, cwd, out, config) {
184152
const options = config.options.compilerOptions || {};
185153

186154
if (extends_framework_config) {
187-
const { paths: user_paths } = options;
188-
189-
if (user_paths && fs.existsSync(kit.files.lib)) {
190-
/** @type {string[]} */
191-
const lib = user_paths['$lib'] || [];
192-
/** @type {string[]} */
193-
const lib_ = user_paths['$lib/*'] || [];
194-
195-
// TODO(v2): check needs to be adjusted when we remove the base path
196-
const missing_lib_paths =
197-
!lib.some((relative) => path.resolve(cwd, relative) === kit.files.lib) ||
198-
!lib_.some((relative) => path.resolve(cwd, relative) === path.join(kit.files.lib, '/*'));
199-
200-
if (missing_lib_paths) {
201-
console.warn(
202-
colors
203-
.bold()
204-
.yellow(`Your compilerOptions.paths in ${config.kind} should include the following:`)
205-
);
206-
let relative = posixify(path.relative('.', kit.files.lib));
207-
if (!relative.startsWith('.')) relative = `./${relative}`;
208-
console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
209-
}
155+
const { paths, baseUrl } = options;
156+
157+
if (baseUrl || paths) {
158+
console.warn(
159+
colors
160+
.bold()
161+
.yellow(
162+
`You have specified a baseUrl and/or paths in your ${config.kind} which interferes with SvelteKit's auto-generated tsconfig.json. ` +
163+
'Remove it to avoid problems with intellisense. For path aliases, use `kit.alias` instead: https://kit.svelte.dev/docs/configuration#alias'
164+
)
165+
);
210166
}
211167
} else {
212168
let relative = posixify(path.relative('.', out));
@@ -231,9 +187,8 @@ const value_regex = /^(.*?)((\/\*)|(\.\w+))?$/;
231187
* Related to vite alias creation.
232188
*
233189
* @param {import('types').ValidatedKitConfig} config
234-
* @param {boolean} include_base_url
235190
*/
236-
function get_tsconfig_paths(config, include_base_url) {
191+
function get_tsconfig_paths(config) {
237192
/** @param {string} file */
238193
const config_relative = (file) => posixify(path.relative(config.outDir, file));
239194

@@ -252,9 +207,7 @@ function get_tsconfig_paths(config, include_base_url) {
252207
const value_match = value_regex.exec(value);
253208
if (!value_match) throw new Error(`Invalid alias value: ${value}`);
254209

255-
const rel_path = (include_base_url ? project_relative : config_relative)(
256-
remove_trailing_slashstar(value)
257-
);
210+
const rel_path = config_relative(remove_trailing_slashstar(value));
258211
const slashstar = key_match[2];
259212

260213
if (slashstar) {

packages/kit/src/core/sync/write_tsconfig.spec.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('Creates tsconfig path aliases from kit.alias', () => {
1414
}
1515
});
1616

17-
const { compilerOptions } = get_tsconfig(kit, false);
17+
const { compilerOptions } = get_tsconfig(kit);
1818

1919
// $lib isn't part of the outcome because there's a "path exists"
2020
// check in the implementation
@@ -27,31 +27,6 @@ test('Creates tsconfig path aliases from kit.alias', () => {
2727
});
2828
});
2929

30-
test('Creates tsconfig path aliases from kit.alias with existing baseUrl', () => {
31-
const { kit } = validate_config({
32-
kit: {
33-
alias: {
34-
simpleKey: 'simple/value',
35-
key: 'value',
36-
'key/*': 'some/other/value/*',
37-
keyToFile: 'path/to/file.ts'
38-
}
39-
}
40-
});
41-
42-
const { compilerOptions } = get_tsconfig(kit, true);
43-
44-
// $lib isn't part of the outcome because there's a "path exists"
45-
// check in the implementation
46-
expect(compilerOptions.paths).toEqual({
47-
simpleKey: ['simple/value'],
48-
'simpleKey/*': ['simple/value/*'],
49-
key: ['value'],
50-
'key/*': ['some/other/value/*'],
51-
keyToFile: ['path/to/file.ts']
52-
});
53-
});
54-
5530
test('Allows generated tsconfig to be mutated', () => {
5631
const { kit } = validate_config({
5732
kit: {
@@ -63,7 +38,7 @@ test('Allows generated tsconfig to be mutated', () => {
6338
}
6439
});
6540

66-
const config = get_tsconfig(kit, false);
41+
const config = get_tsconfig(kit);
6742

6843
// @ts-expect-error
6944
assert.equal(config.extends, 'some/other/tsconfig.json');
@@ -81,7 +56,7 @@ test('Allows generated tsconfig to be replaced', () => {
8156
}
8257
});
8358

84-
const config = get_tsconfig(kit, false);
59+
const config = get_tsconfig(kit);
8560

8661
// @ts-expect-error
8762
assert.equal(config.extends, 'some/other/tsconfig.json');
@@ -96,7 +71,7 @@ test('Creates tsconfig include from kit.files', () => {
9671
}
9772
});
9873

99-
const { include } = get_tsconfig(kit, false);
74+
const { include } = get_tsconfig(kit);
10075

10176
expect(include).toEqual([
10277
'ambient.d.ts',

packages/migrate/migrations/sveltekit-2/migrate.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export function update_tsconfig_content(content) {
6666
);
6767
}
6868

69+
if (content.includes('"paths":') || content.includes('"baseUrl":')) {
70+
log_migration(
71+
'`paths` and/or `baseUrl` detected in your tsconfig.json - remove it and use `kit.alias` instead: https://kit.svelte.dev/docs/v2-migration-guide#generated-tsconfigjson-is-more-strict'
72+
);
73+
}
74+
6975
return updated;
7076
}
7177

0 commit comments

Comments
 (0)