Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

csaba_szekely's avatar

Custom JS file import

If I add a new .js file to resources/js then, is this file compiled in with vite ?

Then if want to use this .js file in only own specific blade file then how do I import this file ?

0 likes
7 replies
LaryAI's avatar
Level 58

Yes, the new .js file will be compiled in with Vite. To import the file in your specific blade file, you can use the <script> tag. For example:

<script src="{{ asset('js/your-file.js') }}"></script>

This will include the file in your blade file.

lara286950's avatar
Level 2

@csaba_szekely try the following:

Your vite.config.js

	...
    return {
        plugins: [
            laravel({
                input: [
                    'resources/scss/app.scss',
                    'resources/js/app.js',
					'resources/js/your-file.js'
                ],
                ...
            }),
        ],
        ...
    }

Your layout file

@vite(['resources/sass/app.scss', 'resources/js/app.js', 'resources/js/your-file.js'])

This is is what my vite.config.js looks like:

import { defineConfig, loadEnv } from 'vite';
import laravel, { refreshPaths } from 'laravel-vite-plugin';
import fs from 'fs';
import { resolve } from 'path';
import { homedir } from 'os';

export default defineConfig(({ command, mode }) => {
    // Load current .env-file
    const env = loadEnv(mode, process.cwd(), '')

    // Set the host based on APP_URL
    let host = new URL(env.APP_URL).host
    let homeDir = homedir()
    let serverConfig = {}

    if (homeDir) {
        serverConfig = {
            https: {
                key: fs.readFileSync(
                    resolve(homeDir, `.config/valet/Certificates/${host}.key`),
                ),
                cert: fs.readFileSync(
                    resolve(homeDir, `.config/valet/Certificates/${host}.crt`),
                ),
            },
            hmr: {
                host
            },
            host
        }
    }

    return {
        plugins: [
            laravel({
                input: [
                    'resources/scss/app.scss',
                    'resources/js/app.js',
                ],
                refresh: [
                    ...refreshPaths,
                    'app/Http/Livewire/**',
                ],
            }),
        ],
        server: serverConfig
    }
});

Please note not all of the above is required this file is specified for valet so vite and valet can play nice together.

1 like
lara286950's avatar

If you are not using sass then replace it with css this should work for what you are trying to achieve.

lara286950's avatar

Like wise for livewire and refresh paths remove it if not using it.

Please or to participate in this conversation.