Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aradalvand opened this issue Apr 2, 2022 · 3 comments
Labels
feature / enhancement New feature or request
Milestone

Comments

@aradalvand
Copy link
Contributor

aradalvand commented Apr 2, 2022

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

@aradalvand aradalvand changed the title Automatically populate compilterOptions.paths in the generated .svelte-kit/tsconfig.json file based on svelte.config.js Automatically populate compilerOptions.paths in the generated .svelte-kit/tsconfig.json file based on svelte.config.js Apr 2, 2022
@dominikg
Copy link
Member

dominikg commented Apr 2, 2022

@RB-Lab
Copy link

RB-Lab commented Jun 16, 2022

@aradalvand, if it is not unavoidable for you to have alias in vite config, if you move it two levels upward at kit level (as described in doc), Svelte-kit seems to be already auto-populating .svelte-kit/tsconfig.json with those. And it also looks like when you describe aliases in vite section it breaks this behavior for $lib as well. I.e.

kit: {
  preprocess: preprocess(),
  alias: {
    $stores: path.resolve('./src/lib/stores'),
  }
},

Works, adds

"paths": {
  "$lib": [
    "src/lib"
  ],
  "$lib/*": [
    "src/lib/*"
  ],
  "$stores": [
    "src/lib/stores"
  ],
  "$stores/*": [
    "src/lib/stores/*"
  ]
},

to .svelte-kit/tsconfig.json

However

  kit: {
    preprocess: preprocess(),
    vite: {
      resolve: {
        alias: {
          $stores: path.resolve('./src/lib/stores'),
        },
      },
    },
  },

Doesn't works and breaks $lib as well.

@dummdidumm
Copy link
Member

Duplicate of #4734, was solved through #4964

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants