0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeSctipt/vite環境で絶対パス(@)でimportする

Posted at

import

TypeScript での設定を Vite で使い回せるvite-tsconfig-pathsパッケージをimport

npm i vite-tsconfig-paths

ts.config.json更新

ts.config.jsonが、tsconfig.app.jsonを参照するように設定されているので、tsconfig.app.jsonを編集する。

ts.config.json
// ts.config.jsonは何も編集しない。
{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}
tsconfig.app.json
{
  "compilerOptions": {
    ...

+    "baseUrl": ".",
+    "paths": {
+      "@/*": ["./src/*"]
+    },
    
    ...
}

vite.config.ts更新

vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "tailwindcss";
+ import tsconfigPaths from "vite-tsconfig-paths";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
      react(),
+       tsconfigPaths()
    ],
  css: {
    preprocessorOptions: {
      scss: {
        api: "modern-compiler",
      },
    },
    postcss: {
      plugins: [tailwindcss()],
    },
  },
});

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?