LoginSignup
1
1

Tauri + Svelte + Viteでimportを絶対パスで行いたかった

Last updated at Posted at 2024-01-02

先に結論

vite.config.jsの記述を以下の通りにする

vite.config.js
import { fileURLToPath } from 'url';

export default defineConfig(async () => ({
  plugins: [svelte()],

  // 中略

  resolve: {
    alias: {
      '$lib': fileURLToPath(new URL('./src/lib', import.meta.url)),
    },
  },
}));

試した方法

普段のSvelteのノリでvite.config.js、及びtsconfig.jsonを以下のように記載していたのですがnot workingでした。

vite.config.js
import path from 'path';

export default defineConfig(async () => ({
  plugins: [svelte()],

  // 中略

  resolve: {
    alias: {
      '$lib': path.resolve(__dirname, 'src/lib'),
    },
  },
}));
tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "$lib/*": ["./src/lib/*"],
    }
  }
}
発生したエラー
[plugin:vite:import-analysis] Failed to resolve import "$lib/foo/bar.js" from "src\App.svelte". Does the file exist?

日本語の情報が調べた限りでは無かったため一応記事に残しておきます。。
理由までは調べられていない(というかそんな英語力がない)ので詳しい方がいましたらぜひ補足をば…

参考記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1