最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jsconfig path alias is not working Sveltekit - Stack Overflow

programmeradmin6浏览0评论

I am using svelte kit (with typescript) and have some created some shortlinks and cannot get the new link "$base" to work. I added the shortlink here

./jsconfig.json

{
  "pilerOptions": {
    "module": "monjs",
    "baseUrl": ".",
    "paths": {
      "$lib":   ["src/lib"],
      "$lib/*": ["src/lib/*"],
      "$base":  ["src/baseApp"],
      "$base/*":["src/baseApp/*"]
    }
  },
  "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}

The is also no intellisense

More details about jsconfig.json here

I also found something about a similar issue with NEXT here

I tried this and it didn't work

In addition to jsconfig.json I tried adding my the paths to my tsconfig.json file also

{
    "extends": "./.svelte-kit/tsconfig.json",
    "pilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true
    },
    "paths": {
        "$lib":   ["src/lib"],
        "$lib/*": ["src/lib/*"],
        "Base":  ["src/baseApp"],
        "Base/*":["src/baseApp/*"]
      }
}

I am using svelte kit (with typescript) and have some created some shortlinks and cannot get the new link "$base" to work. I added the shortlink here

./jsconfig.json

{
  "pilerOptions": {
    "module": "monjs",
    "baseUrl": ".",
    "paths": {
      "$lib":   ["src/lib"],
      "$lib/*": ["src/lib/*"],
      "$base":  ["src/baseApp"],
      "$base/*":["src/baseApp/*"]
    }
  },
  "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}

The is also no intellisense

More details about jsconfig.json here

I also found something about a similar issue with NEXT here

I tried this and it didn't work

In addition to jsconfig.json I tried adding my the paths to my tsconfig.json file also

{
    "extends": "./.svelte-kit/tsconfig.json",
    "pilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true
    },
    "paths": {
        "$lib":   ["src/lib"],
        "$lib/*": ["src/lib/*"],
        "Base":  ["src/baseApp"],
        "Base/*":["src/baseApp/*"]
      }
}

Share Improve this question asked May 31, 2022 at 14:09 Paul APaul A 4176 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try adding your paths to the svelte.config.js file in the root of your project

...
import path from 'path';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    ...
    kit: {
        ...
        vite: {
            resolve: {
                alias: {
                    $lib: path.resolve('./src/lib'),
                    $base: path.resolve('./src/baseApp'),
                }
            }
        }
    }
};

export default config;

Edit: Newer versions of sveltekit uses vite.config.js instead

import { sveltekit } from '@sveltejs/kit/vite';
import path from "path"

const config = {
    resolve: {
        alias: {
            '$lib': path.resolve('./src/lib/'),
            '$base': path.resolve('./src/baseApp'),
        },
    },
    plugins: [sveltekit()]
};

export default config;

This is the latest way of implementing it. No need for vite config file

https://kit.svelte.dev/docs/configuration#alias

//svelte.config.js
import path from 'path';

const config = {
    ...
    kit: {
        ...
        alias: {
            $ponents: path.resolve('./src/ponents')
        }
    }
};

发布评论

评论列表(0)

  1. 暂无评论