Skip to content

Automatically populate compilerOptions.paths in the generated .svelte-kit/tsconfig.json file based on svelte.config.js #4496

@aradalvand

Description

@aradalvand

Describe the problem

Currently, when you want to add aliases you need to add them in two places, namely svelte.config.js and tsconfig.json, and also remember to always keep them in sync.
So, for example:
svelte.config.js:

/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: preprocess(),
    kit: {
        vite: {
            resolve: {
                alias: {
                    $components: path.resolve('./src/lib/components'),
                    $actions: path.resolve('./src/lib/actions'),
                    $common: path.resolve('./src/lib/common'),
                    $stores: path.resolve('./src/lib/stores'),
                },
            },
        },
    },
};

export default config;

You'll have to have:
tsconfig.json:

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "paths": {
            "$components/*": ["src/lib/components/*"],
            "$actions/*": ["src/lib/actions/*"],
            "$common/*": ["src/lib/common/*"],
            "$stores/*": ["src/lib/stores/*"]
        }
    }
}

Actually, you also have to add the default $lib alias to your tsconfig.json as well, since otherwise SvelteKit will display a warning complaining about this. So it'll be more like:
tsconfig.json:

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "paths": {
            "$lib":["src/lib"],
            "$lib/*":["src/lib/*"],
            "$components/*": ["src/lib/components/*"],
            "$actions/*": ["src/lib/actions/*"],
            "$common/*": ["src/lib/common/*"],
            "$stores/*": ["src/lib/stores/*"]
        }
    }
}

Which is not exactly a great developer experience.

Describe the proposed solution

Since we now have a tsconfig.json in the .svelte-kit directory, it would be nice if the aliases defined in svelte.config.js were automatically detected and added to the auto-generated .svelte-kit/tsconfig.json file, so as to eliminate the need for the developer to do this manually.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions