How import own js file into vite?

Viewed 273

I use Laravel with Vite and I want to add file with Vanillia JS code. Before I used mix and I have never use Vite before. I tryed add this code into file vite.config.js like below example:

laravel({
    input: [
        'resources/sass/app.scss',
        'resources/js/app.js',
        'resources/js/test.js', //this is my code
    ],
    refresh: true,
}),

but it doesn't work. I need to add one library and code with config that. Could you help me?

1 Answers

Use the @vite('resources/js/test.js') directive: https://legacy.laravel-vite.dev/guide/usage.html#vite

    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    
        <title>Laravel</title>
        @client
        @vite('main')
        @vite('resources/js/some-script.js')
        <!-- 
        In development:
            <script type="module" src="http://localhost:3000/@vite/client"></script>
            <script type="module" src="http://localhost:3000/resources/scripts/main.ts"></script>
            <script type="module" src="http://localhost:3000/resources/js/some-script.js"></script>
        In production:
            <script type="module" src="http://laravel.local/build/assets/main.66e83946.js"></script>
            <script type="module" src="http://laravel.local/build/assets/some-script.6d3515d2.js"></script>
        -->
    </head>
Related