LoginSignup
2
3

More than 1 year has passed since last update.

Viteでimportパスにエイリアスを使う方法

Last updated at Posted at 2022-08-17

はじめに

2022年6月29日からLaravelのデフォルトだったバンドルツールがwebpackからViteになりました。
新しいプロジェクトで初めてViteを使うので、Importパスでエイリアスを使うために必要なことを備忘録として残しておきたいと思います。

バージョン

  • Laravel v9.21.6
  • Vite 3.0.0
  • laravel-vite-plugin 0.5.0

やりたいこと

// 相対パスで指定していたimportを
import Hoge from '../components/Hoge'

// こんな感じにインポートしたい
import Hoge from '@/components/Hoge'

vite.config.jsに追記

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
    // 下記を追加する
    resolve: {
        alias: {
            '@': '/resources/js',
        },
    },
    // ここまで
});

2
3
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
2
3